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

'\' を避けるために @ リテラルと "" を組み合わせて使用​​する必要があります。

この例は私にとってはうまくいきます:

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"

2 つの引用符は一重引用符になるので、文字列の最初と最後の三重引用符は、文字列定義を開始するための引用符であり、次に二重引用符で引用符を作成することを覚えておいてください。おそらく、より紛らわしい文字列形式の 1 つですが、それが機能する方法です。

于 2012-10-25T12:51:39.990 に答える