对网站建设起到计划和指导的作用,手机百度下载安装,长沙简单的网站建设公司,温州敎玩具网站建设咨询区 Stefan Steiger我有一个 Console 程序#xff0c;它主要用来重启 IIS 以及删除临时文件#xff0c;我现在期望它启动后隐藏自身#xff0c;我在网上找了下面这段代码做了隐藏。static void Main(string[] args)
{var currentProcess System.Diagnostics.Process.Get… 咨询区 Stefan Steiger我有一个 Console 程序它主要用来重启 IIS 以及删除临时文件我现在期望它启动后隐藏自身我在网上找了下面这段代码做了隐藏。static void Main(string[] args)
{var currentProcess System.Diagnostics.Process.GetCurrentProcess();Console.WriteLine(currentProcess.MainWindowTitle);IntPtr hWnd currentProcess.MainWindowHandle;//FindWindow(null, Your console windows caption); //put your console window caption hereif (hWnd ! IntPtr.Zero){//Hide the windowShowWindow(hWnd, 0); // 0 SW_HIDE}
}但这段代码还是有点问题在启动时会看到 窗口 一瞬间从显示到隐藏这不是我想要的效果我希望它能够实现完全隐藏请问我该如何实现回答区 Richard如果你的 Console 不需要诸如 Console.WriteLine() 这类输出流我建议你直接在构建应用程序的时候将 程序类型 改成 Windows Application截图如下接下来可以使用如下 C# 代码。static void Main(string[] args){for (int i 0; i int.MaxValue; i){File.AppendAllText(C:\\SosexUst.log, $i{i} {Environment.NewLine});Thread.Sleep(1000);}}可以发现log文件已经在不断的写入了。它的本质就是修改了 .exe 的PE头这样应用程序启动后就不按照 Console 默认的会话走了。abatishchev可以调用 win32 api 实现参考如下代码[DllImport(kernel32.dll, CharSet CharSet.Unicode, CallingConvention CallingConvention.StdCall, SetLastError true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle(IntPtr handle);[DllImport(kernel32.dll, CharSet CharSet.Unicode, CallingConvention CallingConvention.StdCall, SetLastError true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool FreeConsole();[DllImport(kernel32.dll, CharSet CharSet.Unicode, CallingConvention CallingConvention.StdCall, SetLastError true)]
private static extern IntPtr GetStdHandle([MarshalAs(UnmanagedType.I4)]int nStdHandle);// see the comment below
private enum StdHandle
{StdIn -10,StdOut -11,StdErr -12
};void HideConsole()
{var ptr GetStdHandle((int)StdHandle.StdOut);if (!CloseHandle(ptr))throw new Win32Exception();ptr IntPtr.Zero;if (!FreeConsole())throw new Win32Exception();
}可以参考 githubhttps://github.com/abatishchev/reg2run/blob/master/ManualConsole.cs 了解更多和 console 有关的 api 接口。点评区 Richard 大佬这种方案挺好的对了要想看 PE 头可以用 PPEE.exe 小工具查看。可以看到其实指的是 PE 头中的 SubSystem 字段。