Reputation: 11
I'm using the following code to execute shell commands in a c# application:
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);
}
It works if I enter commands like 'ipconfig' or 'whoami'. But when I enter for example 'dir' I get a:
System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
Any idea why? What's the trick here?
Upvotes: 1
Views: 414
Reputation: 1904
Since dir is a command within cmd.exe you can't start it by it self , but you can execute it like this.
System.Diagnostics.Process.Start("CMD.exe","dir");
Upvotes: 4