console 程序随系统启动及隐藏当前程序窗口

应业务需求,程序需要与系统启动而自动运行,故加入以下代码,保存成 .bat文件 ,双击执行即可

reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run" /v HuazhuPushRecord /t reg_sz /d "your app path/app.exe" /f


echo "成功添加开机启动 you app.exe"

your app.exe

 

 

有些时候,窗口程序需要隐藏,防止误关闭导致程序不运行,故添加以下代码,可避免这种情况

     [DllImport("User32.dll", EntryPoint = "FindWindow")]
         private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
 
         [DllImport("user32.dll", EntryPoint = "FindWindowEx")]   //找子窗体   
         private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
 
         [DllImport("User32.dll", EntryPoint = "SendMessage")]   //用于发送信息给窗体   
         private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
 
         [DllImport("User32.dll", EntryPoint = "ShowWindow")]   //
         private static extern bool ShowWindow(IntPtr hWnd, int type);


        public static void WindowHide()
        {
             Console.Title = "youappname";
             IntPtr ParenthWnd = new IntPtr(0);
             IntPtr et = new IntPtr(0);
             ParenthWnd = FindWindow(null, "youappname");

             ShowWindow(ParenthWnd, 0);//隐藏本dos窗体, 0: 后台执行;1:正常启动;2:最小化到任务栏;3:最大化
        }