我正在尝试将在 CMD 提示符下运行的程序的输出复制到 Windows 剪贴板。
private void button1_Click(object sender, EventArgs e)
{
/*Relevant Code*/
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = String.Format("/k cd {0} && backdoor -rt -on -s{1} -p{2}", backdoorDir, pSN, sPPC);
p.Start();
p.WaitForExit();
string result = p.StandardOutput.ReadToEnd();
System.Windows.Forms.Clipboard.SetText(result);
}
如果我将其直接输入到 CMD 中,它将如下所示:
第一个命令(更改目录):
cd C:\users\chris\appdata\roaming\backdoor
第二个命令(启动后门,一个 cmd 工具。后面是参数。):
backdoor -rt -on -sCCDXE -p14453
当通过 CMD 执行此操作时,我得到以下结果:
The backdoor password is: 34765
C:\users\chris\appdata\roaming\backdoor>
但是,当运行我的 C# 代码时,这是唯一添加到我的剪贴板的东西:
C:\users\chris\appdata\roaming\backdoor>
为什么不捕获“后门密码是:34765?”这就像 p.StandardOutput.ReadToEnd()
没有读取所有内容。
最佳答案
在 WaitForExit
之前调用 ReadToEnd
克里斯的代码:
private void button1_Click(object sender, EventArgs e)
{
/*Relevant Code*/
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = String.Format("/k cd {0} && backdoor -rt -on -s{1} -p{2}", backdoorDir, pSN, sPPC);
p.Start();
string result = p.StandardOutput.ReadToEnd();
p.WaitForExit();
System.Windows.Forms.Clipboard.SetText(result);
}
示例控制台应用程序代码:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/C dir";
p.Start();
string result = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(result);
Console.ReadLine();
- argument
/C
执行命令,然后终止cmd进程。这是此代码工作所必需的。否则,它将永远等待。
关于c# - 将 CMD 输出复制到剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30858148/