c# - 将 CMD 输出复制到剪贴板

标签 c# winforms cmd

我正在尝试将在 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/

相关文章:

c# - 我可以混淆已编译的 .NET 可执行文件/程序集吗?

c# - 当我实现使用 HandlerAttribute 的接口(interface)时,是否可以拦截它?

cmd - 自动热键脚本打开命令提示符

c# - 我可以使用哪些项目模板来创建 winform dll 库?

c# - 使用 anchor 自动缩放大小 - 重叠控件

powershell - 通过 Kudu 命令 API 运行 PowerShell 来编辑文件

c - 返回地址 GDB : . exe 与 .elf 不同?

c# - 使用图形 API 以编程方式在 Azure 事件目录中注册应用程序

c# - 根据文件名中的日期过滤目录中的文件

c# - ComboBox 空值不复制到数据绑定(bind)数据源