C# 在winform C的cmd.exe中输入参数和密码

C# 在winform C的cmd.exe中输入参数和密码,c#,winforms,ssh,passwords,C#,Winforms,Ssh,Passwords,我必须使用cmd.exe建立ssh连接。我正在使用winform应用程序中的一个按钮来执行此过程。在传递ssh连接的命令后,cmd.exe会提示输入密码。除了传递ssh-p之外,我如何传递密码作为参数root@localhost用于建立连接的命令?我必须将cmd.exe作为后台进程运行。请帮忙。多谢各位 我是c新手,我尝试过的代码之一: private void button2_Click(object sender, EventArgs e) { try

我必须使用cmd.exe建立ssh连接。我正在使用winform应用程序中的一个按钮来执行此过程。在传递ssh连接的命令后,cmd.exe会提示输入密码。除了传递ssh-p之外,我如何传递密码作为参数root@localhost用于建立连接的命令?我必须将cmd.exe作为后台进程运行。请帮忙。多谢各位

我是c新手,我尝试过的代码之一:

    private void button2_Click(object sender, EventArgs e)
    {
         try
         {
             System.Diagnostics.Process process = new System.Diagnostics.Process();
             System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
             startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
             startInfo.FileName = "cmd.exe"; 
             startInfo.RedirectStandardInput = true;
             startInfo.UseShellExecute = false;

             using (StreamWriter sw = process.StandardInput)
             {
                if (sw.BaseStream.CanWrite)
                {
                    sw.WriteLine("/c ssh -p 2022 root@localhost"); //first comand i need to enter
                    sw.WriteLine("/c alpine");//command to be typed as password in response to 1st cmd's output
                    sw.WriteLine("/c mount.sh");//command to be typed nest in response to 2nd cmd's next output
                }
             }
        }
        catch {}
    }
你需要开始你的过程

在此之前,您需要将startinfo分配给您的流程

此外,如果不想打开窗口,则应使用CreateNowInow,而不是将WindowsStyle设置为Hidden

我更改了您的代码,如下所示:

        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "cmd.exe";
        startInfo.RedirectStandardInput = true;
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = true;
        Process process = new Process();
        process.StartInfo = startInfo;
        process.Start();

        using (StreamWriter sw = process.StandardInput)
        {
            if (sw.BaseStream.CanWrite)
            {
                sw.WriteLine("/c ssh -p 2022 root@localhost"); //first comand i need to enter
                sw.WriteLine("/c alpine");//command to be typed as password in response to 1st cmd's output
                sw.WriteLine("/c mount.sh");//command to be typed nest in response to 2nd cmd's next output
            }
        }

请显示您的代码,以便我们给出适合的答案。添加了我的代码。问题是我尝试使用的代码无法传递密码。此代码也无法提供输出。问题是我尝试使用的代码无法传递密码。谢谢你的回答