拆分可执行路径和参数

时间:2010-02-09 19:37:59

标签: .net windows path split arguments

我需要能够在命令中拆分可执行路径和参数。

Windows轻松处理以下内容:

“notepad.exe C:\ testfile.txt”

“notepad c:\ testfolder \ versioninfo.txt”

“C:\ Windows \ notepad.exe”“C:\ test folder \ versioninfo.txt”

rundll“C \ Windows \ somelibrary.dll”

任何人都有一段代码来解析这些字符串吗?

感谢。

1 个答案:

答案 0 :(得分:0)

我过去曾经使用过这样的东西:

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