【发布时间】:2015-07-01 20:25:58
【问题描述】:
我一直在做一些谷歌搜索,但没有找到任何解决方案。路径参数组合的最常见情况有引号,如
"C:\Program Files\example.exe" -argument --argument -argument "argument argument"
"C:\Program Files\example.exe" /argument /argument /argument "argument argument"
他们只是遍历整个事情,寻找第二个引用,然后将其后的所有内容都视为一个论点。
.
我发现的第二个解决方案(see here) 不带引号但仅适用于不带空格的路径。见下文。
这可行:C:\Windows\System32\Sample.exe -args -args -args "argument argument"
这不起作用:C:\Program Files\Sample.exe -argument "arg arg" --arg-arg
这以相同的方式工作。他们查找第一个空格,然后将其后面的所有内容都视为参数,这不适用于某些/大多数程序(程序文件文件夹名称有空格)。
.
有解决办法吗?我尝试过使用和调整许多 sn-ps,甚至尝试制作自己的正则表达式语句,但它们都失败了。代码 sn-ps 甚至库都会派上用场。
提前致谢!
编辑:我根据要求找到的 sn-ps
片段 1:
char* lpCmdLine = ...;
char* lpArgs = lpCmdLine;
// skip leading spaces
while(isspace(*lpArgs))
lpArgs++;
if(*lpArgs == '\"')
{
// executable is quoted; skip to first space after matching quote
lpArgs++;
int quotes = 1;
while(*lpArgs)
{
if(isspace(*lpArgs) && !quotes)
break;
if(*lpArgs == '\"')
quotes = !quotes;
}
}
else
{
// executable is not quoted; skip to first space
while(*lpArgs && !isspace(*lpArgs))
lpArgs++;
}
// TODO: skip any spaces before the first arg
来源 2:here 中的几乎所有内容
来源 3:各种阴暗的博客
【问题讨论】:
-
您的示例包含带空格且不带引号的路径在 Windows 中也不起作用,因此除非您证明这是个问题,否则我不会担心。
-
只是一个问题:我可以只使用一个字符串 (Process.Start) 来启动一个进程吗?我知道我必须将文件路径和参数分成两个字符串/部分。 (processStarterName.Arguments = "-args -args "参数")
标签: c# regex command-line split arguments