2

コマンド引数として Cmd コマンドを受け入れるプログラムがあります。

基本的には、次のように呼び出します。C:\MyProgram.exe del C:\test.txt

上記のコマンドは正常に機能します。ただし、xcopy コマンドを実行しようとすると失敗します。

C:\MyProgram.exe xcopy C:\test.txt C:\Temp\Test2.txt

プログラムのコード:

class Program
{
    static void Main(string[] args)
    {
        string command = GetCommandLineArugments(args);

        // /c tells cmd that we want it to execute the command that follows and then exit.
        System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", @"/D /c " + command);

        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.UseShellExecute = false;

        // Do not create the black window.
        procStartInfo.CreateNoWindow = true;
        procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

        System.Diagnostics.Process process = new System.Diagnostics.Process();
        process.StartInfo = procStartInfo;
        process.Start();
    }

    private static string GetCommandLineArugments(string[] args)
    {
        string retVal = string.Empty;

        foreach (string arg in args)
            retVal += " " + arg;

        return retVal;
    }
}
4

2 に答える 2

6

アプリケーションが機能していると思うので、コマンドを試してみたところ、stdinから次の入力プロンプトが表示されました。

C:\>xcopy C:\temp\test.html C:\temp\test2.html
Does C:\temp\test2.html specify a file name
or directory name on the target
(F = file, D = directory)?

stdinが関連付けられておらず、アプリが実行からリターンコードを返しているだけなので、おそらく爆撃です。

于 2010-08-06T18:22:01.580 に答える
2

ジミー・ホッファの答えは正しいと思います。それを解決するには、コマンドの冒頭で「開始」を連結することができます。

xcopy C:\temp\test.html C:\temp\test2.html は、必要に応じてプロンプトを表示するウィンドウを表示します。

于 2010-08-06T18:27:30.023 に答える