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();
6
  • But you are not using pscp in your cmd.exe as a parameter? You only using cd which is meaningless. Commented Aug 23, 2013 at 8:33
  • Why don't you just create a batch file and run that file using Process.Star("myBatchFile.bat"); Commented Aug 23, 2013 at 8:34
  • how about Process.Start(@"C:\Windows\system32\cmd.exe","pscp.exe"); ? Commented Aug 23, 2013 at 8: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. Commented Aug 23, 2013 at 8:46
  • possible duplicate of Run Command Prompt Commands Commented Aug 23, 2013 at 8:58

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

4 Comments

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.
got it : const string cmdtext = @"/k command1&command2&command3...";Process.Start(@"C:\Windows\system32\cmd.exe", cmdtext);
@RanveerSidhu The following SO link might help modify my code to your needs: stackoverflow.com/questions/437419/…
@RanveerSidhu If my initial answer has helped you please mark it up.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.