【问题标题】:Want to hide the cmd prompt screen想隐藏cmd提示画面
【发布时间】:2012-12-25 19:27:24
【问题描述】:

我开发了一个实用程序,可以获取列表中所有服务器的时间。

System.Diagnostics.Process p;
string server_name = "";
string[] output;
p = new System.Diagnostics.Process();
p.StartInfo.FileName = "net";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StandardOutput.ReadLine().ToString()

在执行此代码时。 Cmd 提示屏幕即将到来。我想对用户隐藏它。我能为它做些什么?

【问题讨论】:

    标签: c# .net command cmd


    【解决方案1】:

    您可以告诉进程不使用窗口或将其最小化:

    // don't execute on shell
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.CreateNoWindow = true;
    
    // don't show window
    p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
    

    使用UseShellExecute = false,您可以重定向输出:

    // redirect standard output as well as errors
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    

    当你这样做时,你应该使用输出缓冲区的异步读取来避免由于缓冲区溢出导致的死锁:

    StringBuilder outputString = new StringBuilder();
    StringBuilder errorString = new StringBuilder();
    
    p.OutputDataReceived += (sender, e) =>
                {
                    if (e.Data != null)
                    {
                        outputString.AppendLine("Info " + e.Data);
                    }
                };
    
    p.ErrorDataReceived += (sender, e) =>
                {
                    if (e.Data != null)
                    {
                        errorString.AppendLine("EEEE " + e.Data);
                    }
                };
    

    【讨论】:

    • UseShellExecute = false 还允许您将 STDOUT 和 STDERR 重定向到有用的流中。
    • @SrikanthVenugopalan 我把它漏掉了,因为 OP 已经做到了。
    • 是的,你是对的,我的错。虽然,我个人觉得 RedirectStandardError 更重要。
    【解决方案2】:

    试试ProcessWindowStyle 这样的枚举;

    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.CreateNoWindow = true;
    

    隐藏的窗口样式。窗口可以是可见的,也可以是隐藏的。这 系统通过不绘制它来显示一个隐藏的窗口。如果一个窗口是 隐藏,它被有效地禁用。一个隐藏的窗口可以处理 来自系统或其他窗口的消息,但无法处理 来自用户的输入或显示输出。通常,应用程序可能 在自定义窗口外观时隐藏新窗口, 然后使窗口样式正常。使用 ProcessWindowStyle.HiddenProcessStartInfo.UseShellExecute 属性必须为 false

    【讨论】:

      【解决方案3】:

      两个都试试

      p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
      

      或者也检查一下

      要在没有任何窗口的情况下运行子进程,

      使用 CreateNoWindow 属性并设置 UseShellExecute。

      ProcessStartInfo info = new ProcessStartInfo(fileName, arg); 
      info.CreateNoWindow = true; 
      info.UseShellExecute = false;
      Process processChild = Process.Start(info); 
      

      我建议你仔细阅读 MSDN 的这篇文章:How to start a console app in a new window, the parent's window, or no window

      【讨论】:

        【解决方案4】:

        添加系统引用。

        using System.Diagnostics;
        

        然后使用此代码在隐藏的 CMD 窗口中运行您的命令。

        Process cmd = new Process();
        cmd.StartInfo.FileName = "cmd.exe";
        cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        cmd.StartInfo.Arguments = "Enter your command here";
        cmd.Start();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-12-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-29
          • 1970-01-01
          • 2021-07-08
          • 2012-02-25
          相关资源
          最近更新 更多