1. 程式人生 > >設置程序開機運行並添加快捷方式

設置程序開機運行並添加快捷方式

except cati span cut erro tex 顯示 信息 value

//判斷程序是否在運行,如果未運行就啟動該程序
        Timer timer = new Timer();
        protected override void OnStart(string[] args)
        {
            timer.Interval = 15 * 1000;
            timer.Elapsed += OnTimedEvent;
            timer.Enabled = true;
            timer.AutoReset = true;
            timer.Start();
        }
        
        
public void OnTimedEvent(object sender, ElapsedEventArgs e) { const string fileName = @"D:\project\pss\YGPS.PSS\PrintService\bin\Debug\PrintService.exe"; string programName = System.IO.Path.GetFileNameWithoutExtension(fileName);// fileName.Replace(ext, ""); var
isAlive = IsProcessStarted(programName); if (!isAlive) { //聲明一個程序信息類 ProcessStartInfo procInfo = new ProcessStartInfo(); //設置外部程序名 procInfo.FileName = fileName; //procInfo.WindowStyle = ProcessWindowStyle.Normal;
//設置外部程序的啟動參數(命令行參數) procInfo.Arguments = @"D:\"; //procInfo.CreateNoWindow = true; //設置外部程序工作目錄為 C:\ procInfo.WorkingDirectory = @"D:\project\pss\YGPS.PSS\PrintService\bin\Debug"; //設置啟動動作,確保以管理員身份運行 procInfo.Verb = "runas"; //啟動外部程序 //Process proc = Process.Start(procInfo); Process.Start(procInfo); } } /// <summary> /// 此函數用於判斷某一外部進程是否打開 /// </summary> /// <param name="processName">參數為進程名</param> /// <returns>如果打開了,就返回true,沒打開,就返回false</returns> private bool IsProcessStarted(string processName) { try { Process[] temp = Process.GetProcessesByName(processName); if (temp.Length > 0) { return true; } else { return false; } } catch (Exception) { return false; } } private static string CmdPath = @"C:\Windows\System32\cmd.exe"; /// <summary> /// 執行cmd命令 /// 多命令請使用批處理命令連接符: /// <![CDATA[ /// &:同時執行兩個命令 /// |:將上一個命令的輸出,作為下一個命令的輸入 /// &&:當&&前的命令成功時,才執行&&後的命令 /// ||:當||前的命令失敗時,才執行||後的命令]]> /// 其他請百度 /// </summary> /// <param name="cmd"></param> public static void RunCmd(string cmd) { cmd = cmd.Trim().TrimEnd(&) + "&exit";//說明:不管命令是否成功均執行exit命令,否則當調用ReadToEnd()方法時,會處於假死狀態 using (Process p = new Process()) { p.StartInfo.FileName = CmdPath; p.StartInfo.UseShellExecute = false; //是否使用操作系統shell啟動 p.StartInfo.RedirectStandardInput = true; //接受來自調用程序的輸入信息 p.StartInfo.RedirectStandardOutput = true; //由調用程序獲取輸出信息 p.StartInfo.RedirectStandardError = true; //重定向標準錯誤輸出 p.StartInfo.CreateNoWindow = true; //不顯示程序窗口 p.StartInfo.Verb = "runas"; p.Start();//啟動程序 //向cmd窗口寫入命令 p.StandardInput.WriteLine(cmd); p.StandardInput.AutoFlush = true; ////獲取cmd窗口的輸出信息 //output = p.StandardOutput.ReadToEnd(); //p.WaitForExit();//等待程序執行完退出進程 p.Close(); } } #region 設置應用程序開機自動運行 string startup = Application.ExecutablePath; //class Micosoft.Win32.RegistryKey. 表示Window註冊表中項級節點,此類是註冊表裝 RegistryKey rKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); if(rKey==null) rKey= Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); rKey.SetValue("PrintService", startup); rKey.Close(); #endregion #region 開始啟動菜單添加應用快捷方式 //獲取當前系統用戶啟動目錄 string startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); //獲取當前系統用戶桌面目錄 string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); FileInfo fileStartup = new FileInfo(startupPath + "\\PrintService.lnk"); if (!fileStartup.Exists) { WshShell shell = new WshShell(); IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(desktopPath + "\\PrintService.lnk"); string exeDir = Application.StartupPath + "\\PrintService.lnk"; //把程序快捷方式復制到啟動目錄 //System.IO.File.Copy(exeDir, startupPath + "\\PrintService.lnk", true); #endregion

設置程序開機運行並添加快捷方式