0

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();
Ranveer Sidhu
  • 21
  • 1
  • 4
  • But you are not using `pscp` in your `cmd.exe` as a parameter? You only using `cd` which is meaningless. – Soner Gönül Aug 23 '13 at 08:33
  • Why don't you just create a batch file and run that file using Process.Star("myBatchFile.bat"); – VahidNaderi Aug 23 '13 at 08:34
  • how about Process.Start(@"C:\Windows\system32\cmd.exe","pscp.exe"); ? – iceheaven31 Aug 23 '13 at 08:43
  • Can it be that you eventually want to capture the textual output of pscp.exe and put it in a string, which you then display in a textbox for example? You have to take another way with Process.RedirectStandardOutput then. – Ray Aug 23 '13 at 08:46
  • possible duplicate of [Run Command Prompt Commands](http://stackoverflow.com/questions/1469764/run-command-prompt-commands) – Dragos Bobolea Aug 23 '13 at 08:58
  • @RanveerSidhu Process.Start(@"c:\pscp.exe") ? – gsharp Aug 23 '13 at 09:10

2 Answers2

1

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();
MattR
  • 641
  • 3
  • 17
  • 38
  • thanks, i got it, this works well for executing one command i.e. ipconfig but what if i have to execute more than one command in cmd.exe. – Ranveer Sidhu Aug 23 '13 at 08:52
  • got it : const string cmdtext = @"/k command1&command2&command3...";Process.Start(@"C:\Windows\system32\cmd.exe", cmdtext); – Ranveer Sidhu Aug 23 '13 at 09:19
  • @RanveerSidhu The following SO link might help modify my code to your needs: http://stackoverflow.com/questions/437419/execute-multiple-command-lines-with-the-same-process-using-net – MattR Aug 23 '13 at 09:20
  • @RanveerSidhu If my initial answer has helped you please mark it up. – MattR Aug 23 '13 at 09:23
0

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

MattR
  • 641
  • 3
  • 17
  • 38
Aravind
  • 1,521
  • 2
  • 12
  • 23