【问题标题】:Split executable path and arguments拆分可执行路径和参数
【发布时间】:2011-01-14 23:23:09
【问题描述】:

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

Windows 可以轻松处理以下问题:

"notepad.exe C:\testfile.txt"

"记事本 c:\testfolder\versioninfo.txt"

"C:\Windows\notepad.exe" "C:\test 文件夹\versioninfo.txt"

rundll "C\Windows\somelibrary.dll"

有人有一段代码可以解析这样的字符串吗?

谢谢。

【问题讨论】:

  • 您需要拆分任意字符串还是需要处理提供给应用程序的参数?
  • 我必须处理任意命令。

标签: .net windows path split arguments


【解决方案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

【讨论】:

    猜你喜欢
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 2019-02-11
    • 2015-06-25
    • 1970-01-01
    • 2012-01-24
    相关资源
    最近更新 更多