【问题标题】:C# How to use Directory White Spaces into process.arguements?C#如何在process.arguments中使用目录空白?
【发布时间】:2011-05-25 09:09:49
【问题描述】:

创建的程序利用第 3 方工具生成日志文件。

但是,为该工具提供的参数需要来自目录位置的各种文件作为生成日志的一部分。因此,@"-r C:\test\ftk\ntuser.dat -d C:\System Volume Information\" + restoreFolder.Name + " -p runmru"; 的主要参数将用于生成日志。

有人可以建议如何让系统处理“C:\System Volume Information\”的参数并放置空格?谢谢!

代码:

            Process process = new Process();
            process.StartInfo.FileName = @"C:\test\ftk\ripxp\ripxp.exe";
            process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d C:\System Volume Information\" + restoreFolder.Name + " -p runmru";
            process.StartInfo.CreateNoWindow = false;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardError = true;
            process.Start();

【问题讨论】:

    标签: c# process directory arguments


    【解决方案1】:

    您需要通过将\ 附加到它们(\")来转义" - 对于普通字符串,或者将它们加倍("")对于逐字字符串文字(以@ 开头):

    process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d ""C:\System Volume Information\" + restoreFolder.Name + @""" -p runmru";
    

    【讨论】:

    • restoreFolder.Name 是一个系统名称,我想也许你在错误的区域放置了一个额外的 " 或 \?即使上面的代码已经显示错误。
    • @JavaNoob - 是的。您将逐字字符串与非逐字字符串混合在一起......每个都需要不同的转义。已更正...
    【解决方案2】:

    用双引号将该路径括起来:

    process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d ""C:\System Volume Information\" + restoreFolder.Name + @""" -p runmru";
    

    【讨论】:

    • 系统显示三元组“”附近有一个未闭合的“”。
    【解决方案3】:

    也许

    process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d C:\System Volume Information\" + restoreFolder.Name + " -p runmru";
    

    应该是

    process.StartInfo.Arguments = @"-r ""C:\test\ftk\ntuser.dat"" -d ""C:\System Volume Information\""" + restoreFolder.Name + " -p runmru";
    

    【讨论】:

    • 代表回答,但尽量不要使用过多的“”,以免造成混乱。
    【解决方案4】:

    如果我正确理解了这个问题,您可以用引号将名称括起来:

    "... \"C:\System Volume Information\" + restoreFolder.Name + "\"..."
    

    【讨论】:

      【解决方案5】:

      您确实需要将 string.Format 与 Path 类一起使用:

      process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d C:\System Volume Information\" + restoreFolder.Name + " -p runmru"
      

      可以改写为更简洁如下:

      string ntuser = @"C:\test\ftk\ntuser.dat";
      var args = Path.Combine(@"C:\System Volume Information\", "restoreFolder.Name");
      
      var outs = string.Format("-r {0} -d {1} -p runmru", ntuser, args);
      outs.Dump();
      

      【讨论】:

        猜你喜欢
        • 2019-10-05
        • 1970-01-01
        • 1970-01-01
        • 2017-01-26
        • 2011-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多