简体   繁体   English

Process.Start参数不起作用

[英]Process.Start with arguments not working

I run this application in command line and get the desired results 我在命令行中运行此应用程序并获得所需的结果

 Helpdesk-02.exe /department it

but my C# code (below) appears to ignore the argument but launches the app without the command line switches 但是我的C#代码(下面)似乎忽略了参数,但是在没有命令行开关的情况下启动应用程序

 ProcessStartInfo psi = new ProcessStartInfo();
 psi.FileName = @"Y:\Helpdesk-02.exe";
 psi.Arguments = @"/department it";
 psi.UseShellExecute = true;
 Process.Start(psi).WaitForExit();

The @ character is a special quoted string so it behaves differently than a standard string. @字符是一个特殊的引用字符串,因此它的行为与标准字符串不同。 Essentially what was happening is the process was being started with what would look like this from the command line: 从本质上讲,正在发生的过程是从命令行看起来像这样的过程:

>  Helpdesk-02.exe "/department it"

Or one argument. 或者一个论点。 Removing the @ symbol forces C# to interpret the string as expected: 删除@符号会强制C#按预期解释字符串:

> Helpdesk-02.exe /department it

A subtle, but critical difference. 一个微妙但关键的区别。

The @ operator was designed to make it easier to work with paths that have embedded spaces, backslashes, and other characters that have to be escaped in standard strings. @运算符旨在使更容易处理具有嵌入空格,反斜杠和必须在标准字符串中转义的其他字符的路径。 Essentially, it does the character escaping for you. 从本质上讲,它会为你逃脱角色。 The two declarations are equivalent: 这两个声明是等价的:

 string pathToExplorer = @"C:\Program Files\Internet Explorer\iexplore.exe";
 string escaped = "\"C:\\Program Files\\Internet Explorer\\iexplore.exe\"";

It's best to only use the @ operator when you are working with paths to files, and use the normal way when dealing with the parameters. 在处理文件路径时最好只使用@运算符,并在处理参数时使用常规方法。

The documentation for ProcessStartInfo states: ProcessStartInfo的文档指出:

spaces are interpreted as a separator between multiple arguments. 空格被解释为多个参数之间的分隔符。 A single argument that includes spaces must be surrounded by quotation marks, but those quotation marks are not carried through to the target application. 包含空格的单个参数必须用引号括起来,但这些引号不会传递给目标应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM