如何在C#中隐藏控制台应用程序

我在C#中有一个控制台应用程序,我想用户将无法看到它。

我怎样才能做到这一点?

将其编译为Windows窗体应用程序。 如果您没有显式打开任何Windows,它将不会显示任何UI。

在ProjectProperties上将输出类型设置为Windows应用程序。

听起来你不想要一个控制台应用程序,而是一个不打开(可见)窗口的Windows GUI应用程序。

使用以下代码创建一个控制台应用程序“MyAppProxy”,并将MyAppProxy放入启动目录,

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(); } 

您可能需要修复某些项目,因为我没有检查代码的正确性,它可能无法编译,因为某些属性名称可能不同,但希望您明白这一点。

最好的方法是在没有窗口的情况下启动该过程。

  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(); 

查看其他可能的解决方案 –

在运行时切换Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden

和,

当ShowInTaskbar = false时,将另一个进程窗口置于前台

要在C#中隐藏控制台应用程序,当没有其他工作时使用此代码:

 [DllImport("kernel32.dll")] public static extern bool FreeConsole(); 

将FreeConsole()放在代码中的任何位置,我将它放在Init()中,并隐藏命令行。

您可以Pinvoke调用FindWindow()以获取窗口的句柄,然后调用ShowWindow()来隐藏窗口或使用ProcessStartInfo.CreateNoWindow从另一个窗口启动应用程序

创建一个wcf服务并根据您的需要托管它。