0

我正在尝试使用 String.Format 创建以下字符串

2MSFX.exe "C:\Users\Avidan\Documents\Visual Studio 2010\Projects\DefferedRenderer\DummyGame\DummyGameContent\Shaders\Clear.fx" "C:\Users\Avidan\Documents\Visual Studio 2010\Projects\DefferedRenderer\DummyGame \DummyGameContent\Shaders\Clear.mxfb"

所以我正在尝试使用String.Format,但由于某种原因我无法理解它:|

代码是(最后 2 个参数是 String.Empty):

 String outputFile = Path.Combine(destDir, Path.ChangeExtension(Path.GetFileName(fxFile), "mgxf"));
 String command = String.Format("\"{0}\" \"{1}\" \"{2}\" \"{3}\"",  Path.GetFullPath(fxFile),  Path.GetFullPath(outputFile), DX11Support, DebugSupport);

                var proc = new Process
                {
                    StartInfo = new ProcessStartInfo
                    {
                        FileName = MGFXApp,
                        Arguments = command,
                        UseShellExecute = false,
                        RedirectStandardOutput = true,
                        CreateNoWindow = true
                    }
                };

但这似乎给了我 \"C:\Users\Avidan\Documents\Visual Studio 2010\Projects\DefferedRenderer\DummyGame\DummyGameContent\Shaders\ClearGBuffer.fx\" \"C:\Users\Avidan\Documents\Visual Studio 2010\Projects\DefferedRenderer\DummyGame\DummyGameContent\Shaders\MGFX\ClearGBuffer.mgxf\"\"\"\"\"

如果我使用逐字字符串,我将无法创建我想要的字符串。

有任何想法吗?谢谢。

4

3 回答 3

3

更新

你应该使用String.Concat().

String.Concat("\"", Path.GetFullPath(fxFile), "\" " , Path.GetFullPath(outputFile), "\" " DX11Support,"\" " ,DebugSupport, "\"")
于 2012-10-25T12:48:27.753 回答
2

对于像这样的简单情况,我认为没有必要,但您可以创建一个扩展方法来自动在字符串周围加上引号。

public static class StringExtensions
{
    public static string Quotify(this string s)
    {
        return string.Format("\"{0}\"", s);
    }
}

然后您的命令格式如下所示:

String command = String.Join(" ",
    Path.GetFullPath(fxFile).Quotify(),
    Path.GetFullPath(outputFile).Quotify(),
    DX11Support.Quotify(), DebugSupport.Quotify());
于 2012-10-25T12:52:45.513 回答
0

您需要使用 @ 文字的组合来避免'\' squirlyness,并使用 ""'s 来制作 "'s

这个例子对我有用:

string s = @"""C:\Test Dir\file.fx"" ""C:\Test Dir\SubDir\input.dat""";
Console.WriteLine(s);

控制台输出如下所示:

"C:\Test Dir\file.fx" "C:\Test Dir\SubDir\input.dat"

只要记住两个引号组成一个单引号,所以字符串开头和结尾的三引号是开始字符串定义的引号,然后是双引号来做引号。可能是最令人困惑的字符串格式之一,但这就是它的工作原理。

于 2012-10-25T12:51:39.990 回答