C# 使用 Process.Start 参数和路径中的空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17321289/
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
Use Process.Start with parameters AND spaces in path
提问by WEFX
I've seen similar examples, but can't find something exactly like my problem.
我看过类似的例子,但找不到与我的问题完全一样的东西。
I need to run a command like this from C#:
我需要从 C# 运行这样的命令:
C:\FOLDER\folder with spaces\OTHER_FOLDER\executable.exe p1=hardCodedv1 p2=v2
I'm setting v2 at runtime, so I need to be able to modify the string in C# before calling Process.Start. Does anyone know how to handle this, since I have spaces between my parameters?
我在运行时设置 v2,所以我需要能够在调用 Process.Start 之前修改 C# 中的字符串。有谁知道如何处理这个问题,因为我的参数之间有空格?
采纳答案by Steve
You can use the ProcessStartInfoclass to separate your arguments, FileName, WorkingDirectory and arguments without worry for spaces
您可以使用ProcessStartInfo类来分隔参数、文件名、工作目录和参数,而无需担心空格
string fullPath = @"C:\FOLDER\folder with spaces\OTHER_FOLDER\executable.exe"
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = Path.GetFileName(fullPath);
psi.WorkingDirectory = Path.GetDirectoryName(fullPath);
psi.Arguments = "p1=hardCodedv1 p2=" + MakeParameter();
Process.Start(psi);
where MakeParameter is a function that returns the string to be used for the p2 parameter
其中 MakeParameter 是一个函数,它返回要用于 p2 参数的字符串
回答by Swapnil
Try this
尝试这个
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "\"C:\FOLDER\folder with spaces\OTHER_FOLDER\executable.exe\"";
startInfo.Arguments = "p1=hardCodedv1 p2=v2";
Process.Start(startInfo);
回答by Sayka
Even when you use the ProcessStartInfo Class, if you have to add spaces for arguments, then the above answers won't solve the problem. There's a simple solution. Just add quotes around arguments. That's all.
即使您使用 ProcessStartInfo 类,如果您必须为参数添加空格,那么上述答案也无法解决问题。有一个简单的解决方案。只需在参数周围添加引号。就这样。
string fileName = @"D:\Company Accounts\Auditing Sep-2014 Reports.xlsx";
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = "Excel.exe";
startInfo.Arguments = "\"" + fileName + "\"";
System.Diagnostics.Process.Start(startInfo);
Here I've added escaped quotes around filename, and it works.
在这里,我在文件名周围添加了转义引号,并且它有效。
回答by Peet
After looking at the other solutions provided I ran into the issue where all my various arguments were bundled into one argument.
在查看了提供的其他解决方案后,我遇到了一个问题,即我所有的各种论点都捆绑在一个论点中。
i.e. "-setting0=arg0 --subsetting0=arg1"
IE "-setting0=arg0 --subsetting0=arg1"
So I would propose the following:
所以我会提出以下建议:
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "\"" + Prefs.CaptureLocation.FullName + "\"";
psi.Arguments = String.Format("-setting0={0} --subsetting0={1}", "\"" + arg0 + "\"", "\"" + arg1+ "\"");
Process.Start(psi);
With the quotes around each argument, instead of around the entire set of arguments. And as pointed out by Red_Shadowthis can all be done with the single line
在每个参数周围加上引号,而不是在整个参数集周围。正如Red_Shadow所指出的,这一切都可以用单行完成
Process.Start("\"" + filename + "\"", arguments here)

