简体   繁体   中英

Passing argument to the command ran in cmd through c# code

Here is the explanation.

There is a command "admin setserver systempw " which is used to set the password. On clicking 'Enter' after typing that command in cmd, It will prompt for user input. We have to enter a string and hit 'Enter', which will set that string as password for the server mentioned in the command. Now I have to automate that execution with c# code. The screen should have 2 input text boxes and and a Button. The inputs are the server name and password. On clicking that button, It should execute the command mentioned on the top associating the server name and password entered as inputs to the command. Using the tutorials I could create a Process which will run the first command. But, I am unable to associate the password. How can I associate that password to the Prompt the command I have mentioned does.

C:/> admin setserver systempw 'clicking Enter' Please enter password: Sai@45678 'Clicking Enter'

Password have been set successfully.

This is the piece of code I am trying to write.

        string servername = TextBox1.Text;

        ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c admin setserver systempw  " + servername );
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.UseShellExecute = false;

        procStartInfo.CreateNoWindow = true;

        procStartInfo.WorkingDirectory = @"C:/";

        Process proc = new Process();
        proc.StartInfo = procStartInfo;
        proc.Start();

        string result = proc.StandardOutput.ReadToEnd();

        MessageBox.Show("Done! " + result);

How to associate the second text box value (password) to the process as a argument. How it is possible to link the password to the prompt it makes "Please enter password: ".

Please explain.

Have you tried writing to the standard input?

procStartInfo.RedirectStandardInput = true;
...
proc.StandardInput.WriteLine(password);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM