Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Easiest way to parse filename with spaces eg. "C:\Test\File with spaces.txt"

I am trying to pass a full file path to FFMPEG.

C:\TestFolder\Input\Friends - Season 6 - Gag Reel.avi

and it's obviously not liking the fact the path has spaces in it, erroring like so:

C:\TestFolder\Input\Friends: no such file or directory

So what's the easiest way to use filenames with spaces in them? Should I just replace all whitespaces with ~ characters or is there a better way? I have tried escaping the string with various characters:

@"C:\TestFolder\Input\Friends - Season 6 - Gag Reel.avi";

But this doesn't work. Is there a trick to preserving spaces?

like image 867
Dominic Bou-Samra Avatar asked Dec 07 '09 01:12

Dominic Bou-Samra


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


3 Answers

System.IO.Path provides several methods for manipulating filenames...

like image 188
Nescio Avatar answered Sep 21 '22 11:09

Nescio


In general, file paths passed as arguments on the command line require the path to be surrounded with quotation marks.

If you're talking about accepting file paths as an argument to your program, it's easiest to require users to quote paths. That way, the args argument to your main method will contain the whole path as a single string.

If you're calling other programs and passing arguments, file paths with spaces must be quoted.

Process p = new Process();
p.StartInfo.FileName = "notepad.exe";
p.StartInfo.Arguments = string.Format("\"{0}\"", filePath);
p.Start();
like image 41
Kevin Kibler Avatar answered Sep 20 '22 11:09

Kevin Kibler


The only time you'll typically have to do any special handing with filenames that have spaces is when you're passing them to external tools or formats. If you're constructing an argument list for an external executable, for instance, all you have to do is quote the path:

string path = @"C:\TestFolder\Input\Friends - Season 6 - Gag Reel.avi";
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "program.exe";
psi.Arguments = "\"" + path + "\""; // escape the quotes

...which will result in this commandline:

program.exe "C:\TestFolder\Input\Friends - Season 6 - Gag Reel.avi"

like image 44
Michael Petrotta Avatar answered Sep 22 '22 11:09

Michael Petrotta