我见过类似的例子,但找不到与我的问题完全相同的东西.
我需要从C#运行这样的命令:
C:\FOLDER\folder with spaces\OTHER_FOLDER\executable.exe p1=hardCodedv1 p2=v2
Run Code Online (Sandbox Code Playgroud)
我在运行时设置v2,所以我需要能够在调用Process.Start之前修改C#中的字符串.有谁知道如何处理这个,因为我的参数之间有空格?
Say*_*yka 25
即使您使用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);
Run Code Online (Sandbox Code Playgroud)
在这里,我添加了围绕文件名的转义引号,它的工作原理.
Ste*_*eve 21
您可以使用ProcessStartInfo类来分隔您的参数,FileName,WorkingDirectory和参数,而无需担心空格
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);
Run Code Online (Sandbox Code Playgroud)
其中MakeParameter是一个函数,它返回用于p2参数的字符串
小智 7
试试这个
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);
Run Code Online (Sandbox Code Playgroud)
在查看了提供的其他解决方案后,我遇到了一个问题,即我的所有各种论点都捆绑为一个论点。
IE"-setting0=arg0 --subsetting0=arg1"
所以我提出以下建议:
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "\"" + Prefs.CaptureLocation.FullName + "\"";
psi.Arguments = String.Format("-setting0={0} --subsetting0={1}", "\"" + arg0 + "\"", "\"" + arg1+ "\"");
Process.Start(psi);
Run Code Online (Sandbox Code Playgroud)
每个参数都用引号括起来,而不是整个参数集。正如Red_Shadow所指出的,这一切都可以通过单行来完成
Process.Start("\"" + filename + "\"", arguments here)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
52093 次 |
| 最近记录: |