2016-09-19 1 views
0
私はC#アプリケーションでシェルコマンドを実行するには、次のコードを使用してい

ProcessStartInfoとあるUseShellExecuteの使用

私は「IPCONFIG」または「whoamiは」のようなコマンドを入力すると、それが動作
try 
{ 
    Process prc = new Process(); 
    prc.StartInfo = new ProcessStartInfo(); 
    prc.StartInfo.FileName = filename; 
    prc.StartInfo.Arguments = arg; 
    prc.StartInfo.UseShellExecute = false; 
    prc.StartInfo.RedirectStandardOutput = true; 
    prc.Start(); 
    prc.StandardOutput.BaseStream.CopyTo(stream); 
    prc.WaitForExit(); 
    } catch (Exception e){ 
     Console.WriteLine("{0} Exception caught.", e); 
} 

。私は例えば「DIR」を入力すると、しかし、私は得る:

System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified

任意のアイデアなぜですか?ここのトリックは何ですか?

答えて

4

dirはcmd.exe内のコマンドなので、自分で起動することはできませんが、このように実行できます。

System.Diagnostics.Process.Start("CMD.exe","dir"); 
+0

は、実行可能ファイルまたはcmd.exe内のコマンドですか? whoamと同じです... – idontcare

+0

@idontcareはい、これらは両方とも '%Windir%\ System32 \ 'にある' exe'ファイルです。コマンドラインから(例えば): 'where ipconfig'、' where whoami'を使って簡単に確認できます。この場合、 'where dir'はファイルが見つからないことを伝えます – Jcl

関連する問題