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

旧城冷巷雨未停 提交于 2019-11-30 16:57:19

问题


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?


回答1:


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"




回答2:


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




回答3:


are you using FileInfo class?

it contains (almost) everything about a file, including attributes and file versions, and you don't have to worry about spaces or other unwanted characters. As long as the file exists, it works well on FileInfo.

use it as
FileInfo f = new FileInfo(fullPathEvenWithSpaces);




回答4:


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();



回答5:


I agree with the above post. To make it easier, here's the code you need to use. In My Humble Opinion, the only good file name is a space-free file name.

Therefor in c# code I have this:

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern uint GetShortPathName(
   [MarshalAs(UnmanagedType.LPTStr)]
    string lpszLongPath,
   [MarshalAs(UnmanagedType.LPTStr)]
    StringBuilder lpszShortPath,
   uint cchBuffer);

public static StringBuilder shortNameBuffer = new StringBuilder(256);
public static string ToShortPathName(string longName)
{
  uint result = GetShortPathName(longName, shortNameBuffer, 256);
  return shortNameBuffer.ToString();
}

That adds a method to your class that can be used like this:

String goodFileName = ToShortPathName(evilFileName);

NOTE: I'm using this in a UI so I don't mind being non-thread safe and reusing the StringBuider. If you're in a multi-threaded environment, make sure to pull the StringBuilder allocation inside your method.




回答6:


All files have a short pathname that is DOS 8.3 compatible. The only way to get that filename is GetShortPathName. The best explanation I can find is at the following address:

http://www.pinvoke.net/default.aspx/kernel32.GetShortPathName



来源:https://stackoverflow.com/questions/1857325/c-sharp-easiest-way-to-parse-filename-with-spaces-eg-c-test-file-with-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!