【问题标题】:C# execute cmd commandsC#执行cmd命令
【发布时间】:2014-12-03 20:23:47
【问题描述】:

我正在尝试通过 c# 执行 cmd 命令,一切正常。 如何执行包含密码的“runas”cmd,或者至少如何打开 cmd 提示以插入密码。我的代码执行效果很好,这是我似乎无法解决的唯一问题。 我什至尝试过使用 echo 但它似乎不起作用。

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;
        }
    }
}

我需要在远程电脑上以具有管理权限的域用户身份运行 runas 命令。

【问题讨论】:

标签: c#


【解决方案1】:

您可以尝试添加应用程序清单文件,如讨论 here:

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

【讨论】:

  • 问题是我需要在远程机器上以域管理员身份运行,而不是以本地管理员身份运行。
【解决方案2】:

这就是您从 C# 运行 shell 命令所要做的全部工作

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-19
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 2016-06-17
    相关资源
    最近更新 更多