Got To Figure
Got To Figure

Reputation: 422

Runs fine In cmd, Exception in C#

I have the following code:

public void Run(string command) {
   System.Diagnostics.Process.Start("C:\\Windows\\System32\\cmd.exe /c " + command);
   //textBox1.Text = "C:\\Windows\\System32\\cmd.exe /c " + command;
}

In visual studio it tells me:

"An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll Additional information: The system cannot find the file specified

I copy the textBox1.Text as it is in cmd and works fine.

Upvotes: 0

Views: 134

Answers (2)

Got To Figure
Got To Figure

Reputation: 422

So I found a solution by making a process description to run the process. I would prefer the shorter version. So I will not accept this answer.

public void Run(string command) {
        System.Diagnostics.ProcessStartInfo to_run = new System.Diagnostics.ProcessStartInfo();
        to_run.FileName =  "cmd";
        to_run.Arguments = "/c "+ command;
        to_run.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Hidden cmd

        //Start a process based on to_run description
        System.Diagnostics.Process executing = System.Diagnostics.Process.Start(to_run); 
        executing.WaitForExit(); //Don't go further until this function is finished
}

Upvotes: 1

Orel Eraki
Orel Eraki

Reputation: 12196

The filename and parameter must be split and enter accordingly.

Process.Start Method (String, String) accepts only filename, and arguments should be passed by another parameter.

System.Diagnostics.Process.Start(@"C:\Windows\System32\cmd.exe", "/c " + command);

Upvotes: 2

Related Questions