C# - 用空格解析文件名的最简单方法,例如。"C:\Test\File with space.txt"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1857325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# - Easiest way to parse filename with spaces eg. "C:\Test\File with spaces.txt"
提问by Dominic Bou-Samra
I am trying to pass a full file path to FFMPEG.
我正在尝试将完整的文件路径传递给 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?
但这不起作用。有没有保留空间的技巧?
采纳答案by Michael Petrotta
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"
program.exe "C:\TestFolder\Input\Friends - Season 6 - Gag Reel.avi"
回答by Nescio
回答by Kevin Kibler
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.
如果您正在谈论接受文件路径作为您程序的参数,最简单的方法是要求用户引用路径。这样,args
您的 main 方法的参数将包含作为单个字符串的整个路径。
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();
回答by AceMark
are you using FileInfo
class?
你在使用FileInfo
类吗?
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.
它包含(几乎)关于文件的所有内容,包括属性和文件版本,您不必担心空格或其他不需要的字符。只要文件存在,它就在 FileInfo 上运行良好。
use it asFileInfo f = new FileInfo(fullPathEvenWithSpaces);
用它作为FileInfo f = new FileInfo(fullPathEvenWithSpaces);
回答by Jeff Hornby
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:
所有文件都有一个与 DOS 8.3 兼容的短路径名。获取该文件名的唯一方法是 GetShortPathName。我能找到的最好的解释是在以下地址:
http://www.pinvoke.net/default.aspx/kernel32.GetShortPathName
http://www.pinvoke.net/default.aspx/kernel32.GetShortPathName
回答by user246013
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.
我同意上面的帖子。为方便起见,以下是您需要使用的代码。在 My Humble Opinion 中,唯一好的文件名是无空格的文件名。
Therefor in c# code I have this:
因此在 c# 代码中我有这个:
[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);
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.
注意:我在 UI 中使用它,所以我不介意非线程安全和重用 StringBuider。如果您处于多线程环境中,请确保在您的方法中提取 StringBuilder 分配。