简体   繁体   English

将参数传递给命令通过C#代码在cmd中运行

[英]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. 有一个命令“ admin setserver systempw”用于设置密码。 On clicking 'Enter' after typing that command in cmd, It will prompt for user input. 在cmd中键入该命令后单击“ Enter”,它将提示用户输入。 We have to enter a string and hit 'Enter', which will set that string as password for the server mentioned in the command. 我们必须输入一个字符串,然后单击“ Enter”,这会将该字符串设置为命令中提到的服务器的密码。 Now I have to automate that execution with c# code. 现在,我必须使用C#代码自动执行该执行。 The screen should have 2 input text boxes and and a Button. 屏幕上应有2个输入文本框和一个按钮。 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' C:/> admin setserver systempw'单击Enter'请输入密码:Sai @ 45678'单击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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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