2015-06-16 301 views
4

我试图将运行出CMD提示的程序的输出复制到Windows剪贴板。将CMD输出复制到剪贴板

 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()没有读取所有内容。

+1

尝试使用'p.StartInfo.RedirectStandardError = true'和'p.StandardError.ReadToEnd()'代替 – Icemanind

+0

这样做......谢谢。 –

回答

2

呼叫ReadToEndWaitForExit

克里斯代码:

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(); 
  • 参数/C执行该命令,然后终止CMD过程。这是代码工作所必需的。否则,它将永远等待。
+0

感谢您的快速响应。在“阅读”之后,我通过加入“等待”来改变你的建议。当我点击按钮在我的程序中执行时,我得到一个CMD窗口,显示如下:'后门密码是:34765'。但它仍然在我的剪贴板中写入'C:\ users \ chris \ appdata \ roaming \ backdoor>',还有我添加的Console.WriteLine()以排除任何潜在的剪贴板问题。 –

+1

这一切都是正确的。我只需要使用RedirectStandardError。谢谢! –

+0

好找!我更新了答案 –

2

一个振振有辞可以的情况是,该计划是实际写入StdOut,而是直接在屏幕上。

测试此通过管道输出到一个文件:

backdoor -rt -on -sCCDXE -p14453 > c:\text.txt 

如果新文件不包含任何输出然后你被卡住,可能需要寻找到屏幕抓取..