2009-03-27 17 views
4
System.Diagnostics.Process proc0 = new System.Diagnostics.Process(); 
proc0.StartInfo.FileName = "cmd"; 
proc0.StartInfo.WorkingDirectory = Path.Combine(curpath, "snd"); 
proc0.StartInfo.Arguments = omgwut; 

そして今、いくつかの背景で....NET経由でcmdコマンドを実行していますか?

string curpath = System.IO.Path.GetDirectoryName(Application.ExecutablePath); 

omgwutは次のようなものです:

コピー/ B a.wav + b.wav + ... + y.wav + z.wav output.wav

何も起こりません。だから明らかに何かが間違っている。私も実行可能ファイルとして "コピー"を試みたが、それは動作しません。

答えて

16

を効果的

/C <command>

を使用して 文字列で指定されたコマンドを実行し、その後

を終了cmd.exe /?によるとcmd /C copy /b t.wav ...

を言って、/CとCMDに接頭辞あなたの引数を試してみてください

あなたのコードの場合

、それは

// .. 
proc0.StartInfo.Arguments = "/C " + omgwut; 

ノートのようなものに見えるかもしれません:あなたのコマンドが動作するように起こっているかどうかをテストするために

  • 良い方法は、実際にコマンドプロンプトからそれを試してみることですが。 cmd.exe copy ...を実行しようとすると、コピーが行われないことがわかります。
  • 引数として渡すことができる引数の長さには制限があります。 MSDNより: "最大文字列の長さは、.NET Frameworkアプリケーションでは2,003文字、.NET Compact Frameworkアプリケーションでは488文字です。"
  • System.IOクラスを使用してファイルを開き、それらを手動で連結することによって、シェルリングをコマンドに渡すことを回避できます。
0

Daniels cmd/cのアイデアが有効です。あなたのケースではおそらくコマンドラインの長さに8kという制限があることに注意してください。詳細はthisを参照してください。

とにかく.Netアプリになっているので、File.Copyはこのアプローチよりもはるかに簡単で簡単です。

+0

彼は複数のファイルを追加しようとしています。もしあなたがFile.Copyでそれを行うことができるかどうか確かではない –

4

これを試してみてください。私のコードを使って作業しています。

System.Diagnostics.ProcessStartInfo procStartInfo = 
    new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command); 

// The following commands are needed to redirect the standard output. 
// This means that it will be redirected to the Process.StandardOutput StreamReader. 
procStartInfo.RedirectStandardOutput = true; 
procStartInfo.UseShellExecute = false; 
// Do not create the black window. 
procStartInfo.CreateNoWindow = true; 
// Now we create a process, assign its ProcessStartInfo and start it 
System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
proc.StartInfo = procStartInfo; 
proc.Start(); 
// Get the output into a string 
string result = proc.StandardOutput.ReadToEnd(); 
// Display the command output. 
Console.WriteLine(result); 
    } 
    catch (Exception objException) 
    { 
    // Log the exception 
    } 
+0

このコードがプログラムのトップレベルにない限り、例外を記録した後に 'throw;'を追加するべきです。これにより、例外が発信者に伝播されます。 –

1

でも、これを試すことができます。これはさらに優れています。

System.Diagnostics.Process proc = new System.Diagnostics.Process(); 

proc.EnableRaisingEvents=false; 
proc.StartInfo.FileName="iexplore"; 
proc.StartInfo.Arguments="http://www.microsoft.com"; 

proc.Start(); 

proc.WaitForExit(); 

MessageBox.Show("You have just visited " + proc.StartInfo.Arguments); 
関連する問題