我试图通过c#执行cmd命令,一切正常。 我如何执行包含密码的“runas”cmd,或者至少如何打开用于插入密码的cmd提示符。我的代码执行效果很好,这是我无法解决的唯一问题。 我试过甚至与回声,但它似乎并没有工作。C#执行cmd命令
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命令作为在远程pc上具有管理权限的域用户。
你不能运行你的应用程序作为管理员? 如果不是我认为你会在这里找到解决方案:http://stackoverflow.com/questions/8690552/run-elevated-process – Jontatas 2014-12-03 11:45:25
我认为你需要重写你的问题,如果你的问题是关于如何管道输入在孩子的过程的标准输入,请尝试http://www.codeproject.com/Articles/18577/How-to-redirect-Standard-Input-Output-of-an-applic – BlueTrin 2014-12-03 11:47:45
事情是我需要runas作为远程计算机上的域管理员,而不是本地管理员。 – 2014-12-03 12:00:39