How to hide a console application in C#

前端 未结 9 1849
刺人心
刺人心 2020-12-06 09:27

I have a console application in C#, and I want that the user won\'t be able to see it.

How can I do that?

相关标签:
9条回答
  • 2020-12-06 10:02

    You can Pinvoke a call to FindWindow() to get a handle to your window and then call ShowWindow() to hide the window OR Start your application from another one using ProcessStartInfo.CreateNoWindow

    0 讨论(0)
  • 2020-12-06 10:05

    Compile it as a Windows Forms application. Then it won't display any UI, if you do not explicitly open any Windows.

    0 讨论(0)
  • 2020-12-06 10:10

    Create a console application "MyAppProxy" with following code, and put MyAppProxy in start up dir,

    public static void main(string[] args)
    {
       Process p = new Process("MyApp");
       ProcessStartUpInfo pinfo = new ProcessStartUpInfo();
       p.StartupInfo = pinfo;
       pinfo.CreateNoWindow = true;
       pinfo.ShellExecute = false;
    
       p.RaiseEvents = true;
    
       AutoResetEvent wait = new AutoResetEvent(false);
       p.ProcessExit += (s,e)=>{ wait.Set(); };
    
       p.Start();
       wait.WaitOne();
    }
    

    You may need to fix certain items here as I didnt check correctness of the code, it may not compile because some property names may be different, but hope you get the idea.

    0 讨论(0)
  • 2020-12-06 10:11

    The best way is to start the process without window.

            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.Arguments = "echo Hello!";
            //either..
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            //or..
            p.StartInfo.CreateNoWindow = true;
            p.Start();
    

    See other probable solutions -

    Toggle Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden at runtime

    and,

    Bring another processes Window to foreground when it has ShowInTaskbar = false

    0 讨论(0)
  • 2020-12-06 10:13

    Create a wcf service and host it as per your need.

    0 讨论(0)
  • 2020-12-06 10:19

    On ProjectProperties set Output Type as Windows Application.

    0 讨论(0)
提交回复
热议问题