Ranveer Sidhu
Ranveer Sidhu

Reputation: 21

Executing Windows cmd commands via C#

i wanted to automate some of commands that runs on my windows cmd.exe.

Commands taht i wanted to execute :

cd\

pscp.exe

I am unable to execute , however so far i am able to open cmd.exe via my code.

My code :

 string cd = @"C:\>cd\";
    string pscp = @"C:\>pscp.exe";
    ProcessStartInfo startinfo = new ProcessStartInfo();
    Process.Start(@"C:\Windows\system32\cmd.exe",pscp);
    Console.ReadLine();

Upvotes: 0

Views: 3221

Answers (2)

Aravind
Aravind

Reputation: 1549

Better Option to do this

Do this Via Powershell Commands.. Create a powershell project and Create a new Custom Commandlet (Cmdlet) and do this actions simply there....google it "Powershell Cmdlet"

http://msdn.microsoft.com/en-us/library/windows/desktop/dd878294(v=vs.85).aspx

Upvotes: 0

MattR
MattR

Reputation: 641

You need to set the Arguements property. E.g. to open CMD and start IPCONFIG:

        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = @"C:\Windows\system32\cmd.exe";
        startInfo.Arguments = "/k ipconfig";
        Process myProcess = new Process();
        myProcess.StartInfo = startInfo;
        myProcess.Start();

Upvotes: 1

Related Questions