控制台禁止操作
首先是Windows API声明:
#region windows api [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)] static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow); [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll ", EntryPoint = "GetSystemMenu", SetLastError = true)] static extern IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert); [DllImport("user32.dll ", EntryPoint = "RemoveMenu", SetLastError = true)] static extern int RemoveMenu(IntPtr hMenu, int nPos, int flags); #endregion
禁止开启两个进程,并根据配置是否隐藏控制台,或者是显示控制台,但是禁止关闭按钮,且令Ctrl + C作为普通输入;只能输入exit退出。
new Mutex(true, Config.Instance.ServiceNo, out bool ret); if (!ret) return; Console.Title = Config.Instance.ServiceNo; IntPtr intptr = FindWindow("ConsoleWindowClass", Config.Instance.ServiceNo); if (Config.Instance.ShowUi == 0 && intptr != IntPtr.Zero) { ShowWindow(intptr, 0);//隐藏窗口 } else { IntPtr CLOSE_MENU = GetSystemMenu(intptr, IntPtr.Zero); //找关闭按钮 RemoveMenu(CLOSE_MENU, 0xF060, 0x0);//关闭按钮禁用 Console.TreatControlCAsInput = true; }
string key = ""; while (key != null && key.ToLower() != "exit") { key = Console.ReadLine(); }
posted on 2018-11-09 16:18 jonney_wang 阅读(237) 评论(0) 编辑 收藏 举报