在C#.NET控制台应用程序中,当这些参数用双引号引起来时string[] args
,Main
它已经处理了引号参数。例如,以下命令行:
MyProgram.exe A "B C D" E
...只会在中产生3个条目args
。但是,当使用单引号代替时:
MyProgram.exe A 'B C D' E
...然后args
将有5个条目。单引号没有变成B C D
单个参数。
是否有办法让.NET还将单引号也视为有效的命令行参数引号字符,或者这是否需要特殊的命令行参数解析库?
您可以通过以下方式访问整行
Environment.CommandLine
并手动处理单引号
仅提取CommandArg-Line的一种方法可能是:
private static string getCommandLineArgLine()
{
string fullCommandLine = Environment.CommandLine;
int applicationDoubleQuoteEnds = fullCommandLine.IndexOf("\"", 1, StringComparison.InvariantCulture);
string commandArgLine = fullCommandLine.Substring(applicationDoubleQuoteEnds + 1).TrimStart();
return commandArgLine;
}
提示:System.Reflection.Assembly.GetExecutingAssembly().Location
如果启用了vs-host进程,请不要在调试环境中使用-在这种情况下,不会返回正确的位置(这就是为什么我读最后一个“”)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句