C#-解析带有空格的文件名的最简单方法,例如"C:\ Test \带有spaces.txt的文件" [英] C# - Easiest way to parse filename with spaces eg. "C:\Test\File with spaces.txt"

查看:176
本文介绍了C#-解析带有空格的文件名的最简单方法,例如"C:\ Test \带有spaces.txt的文件"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将完整的文件路径传递给FFMPEG.

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?

推荐答案

通常只有当您将带有空格的文件名传递给外部工具或格式时,才需要进行特殊处理.例如,如果要为外部可执行文件构造一个参数列表,那么您要做的就是引用路径:

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-季节 6-Gag Reel.avi"

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

这篇关于C#-解析带有空格的文件名的最简单方法,例如"C:\ Test \带有spaces.txt的文件"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆