简体   繁体   English

C#执行cmd命令

[英]C# execute cmd commands

Im trying to execute cmd commands through c# and everything works well. 我试图通过C#执行cmd命令,并且一切正常。 How can i execute a "runas" cmd including the password, or at least how can i open the cmd prompt for inserting the password. 我如何执行包含密码的“ runas” cmd,或者至少如何打开用于输入密码的cmd提示符。 My code execution works well, this is the only thing that i cant seem to solve. 我的代码执行效果很好,这是我似乎无法解决的唯一问题。 I've tried even with echo but it doesnt seem to work. 我什至尝试了回声,但似乎没有用。

private void btnStart_Click(object sender, EventArgs e)
{

    txtResult.Text = string.Empty;
    if (txtExecutable.Text.Trim() != string.Empty)
    {
        StreamReader outputReader = null;
        StreamReader errorReader = null;
        try
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.CreateNoWindow = true;
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = @"/C echo " + txtpass.Text + " | runas /user:" + utente.Text + " wmic.exe /node:" + txtExecutable.Text + " ComputerSystem Get UserName && tasklist /s " + txtExecutable.Text + @" /fi ""imagename eq EmpirumRCHost.exe"" && msinfo32.exe \\" + txtParameter.Text;
            startInfo.ErrorDialog = false;
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardError = true;
            startInfo.RedirectStandardInput = true;
            startInfo.RedirectStandardOutput = true;
            process.StartInfo = startInfo;
            process.Start();

            bool processStarted = process.Start();
            if (processStarted)
            {
                //Get the output stream
                outputReader = process.StandardOutput;
                errorReader = process.StandardError;
                process.WaitForExit();

                //Display the result
                string displayText = "Output" + Environment.NewLine + "==============" + Environment.NewLine;
                displayText += outputReader.ReadToEnd();
                displayText += Environment.NewLine + "Error" + Environment.NewLine + "==============" +
                               Environment.NewLine;
                displayText += errorReader.ReadToEnd();
                txtResult.Text = displayText;
            }
        }
        finally
        {
            if (outputReader != null)
            {
                outputReader.Close();
            }
            if (errorReader != null)
            {
                errorReader.Close();
            }
            btnStart.Enabled = true;
        }
    }
}

I need to run the runas command as a domain user with administrative rights on a remote pc. 我需要以具有管理权限的域用户身份在远程PC上运行runas命令。

您可以通过添加应用程序清单文件尝试为讨论在这里

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

this is all you have to do run shell commands from C# 这就是从C#运行shell命令所要做的全部

string strCmdText;
strCmdText= "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);

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

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