代码之家  ›  专栏  ›  技术社区  ›  Cute

如何用引号将存储在变量中的路径括起来?

  •  1
  • Cute  · 技术社区  · 17 年前

    让我们走一条路

    C:\Program Files\TestFolder
    

    这条路径是通过编程得到的,并存储在一个可变的dirpath中(例如)
    现在我已经确定了弦

    dirpath=getInstallationpath()+"\\ test.dll /codebase /tlb";
    

    那么dirpath就变成了

    C:\Program Files\TestFolder\test.dll /codebase /tlb
    

    但我的问题是我已经用双引号把路径括起来了

    "C:\Program Files\TestFolder\test.dll"
    

    因为当我在createprocess()中直接将dirpath作为regasm的命令行传递时,它应该只接受c:\program,因为有空格。所以我尝试了很多类似的特技

    dirpath="\ "+getInstallationPath()+" \test.dll /codebase /tlb "
    

    像那样,但没用…

    所以请在这方面帮助我…

    事先谢谢…

    3 回复  |  直到 17 年前
        1
  •  2
  •   Eric    17 年前

    我可以看到这条线有两个问题。首先,您需要转义test.dll前面的反斜杠。其次,用引号包装路径要求您也要转义引号。

    在这些更改之后,应该是这样的:

    dirpath="\""+getInstallationPath()+"\\test.dll\" /codebase /tlb "
    

    编辑:

    按照马丁的要求完成了任务。忘记了第一个字符串的右引号!

        2
  •  2
  •   A.Rashad    17 年前

    我相信你忘了在test.dll之后的第二个

        3
  •  2
  •   Loki Astari    17 年前

    对于构建复杂的字符串,使用字符串流通常更容易(也更有效)。

    // Note the character(") and the character(\)
    // will need to be escaped when used inside a string
    std::stringstream  stuff;
    suff << "\"" 
         << getInstallationPath() << "\\test.dll" 
         << "\""
         << "/codebase /tlb";
                                                    //
    dirpath = stuff.str();