1. 程式人生 > >C# WPF開機自啟動和只允許一個程式執行

C# WPF開機自啟動和只允許一個程式執行

本文出自:https://www.cnblogs.com/2186009311CFF/p/10024949.html

在App.xaml.cs填充一下內容,即可實現只允許一個執行,且不解鎖螢幕的情況下,重啟執行。

 public partial class App : Application
    {
        System.Threading.Mutex mutex;
        public App()
        {
            this.Startup += new StartupEventHandler(App_Startup);
            this
.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(Application_DispatcherUnhandledException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); } private void App_Startup(object sender, StartupEventArgs e) {
bool ret; mutex = new System.Threading.Mutex(true, "TestEXEName", out ret); if (!ret) { MessageBox.Show("已有一個客戶端正在執行,請先結束原來客戶端!"); Environment.Exit(0); } #region 設定程式開機自動執行(+登錄檔項) try { SetSelfStarting(
true, "TestEXEName.exe"); } catch (Exception ex) { WriteLog(ex); } #endregion } private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { if (e.ExceptionObject is Exception) WriteLog(e.ExceptionObject); else WriteLog(e); } private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { if (e.Exception is Exception) WriteLog(e.Exception); else WriteLog(e); } #region 登錄檔開機自啟動 /// <summary> /// 開機自動啟動 /// </summary> /// <param name="started">設定開機啟動,或取消開機啟動</param> /// <param name="exeName">登錄檔中的名稱</param> /// <returns>開啟或停用是否成功</returns> public bool SetSelfStarting(bool started, string exeName) { RegistryKey key = null; try { string exeDir = System.Windows.Forms.Application.ExecutablePath; //RegistryKey HKLM = Registry.CurrentUser; //key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//開啟登錄檔子項 key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//開啟登錄檔子項 if (key == null)//如果該項不存在的話,則建立該子項 { key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"); } if (started) { try { object ob = key.GetValue(exeName, -1); if (!ob.ToString().Equals(exeDir)) { if (!ob.ToString().Equals("-1")) { key.DeleteValue(exeName);//取消開機啟動 } key.SetValue(exeName, exeDir);//設定為開機啟動 } key.Close(); } catch (Exception ex) { WriteLog(ex); return false; } } else { try { key.DeleteValue(exeName);//取消開機啟動 key.Close(); } catch (Exception ex) { WriteLog(ex); return false; } } return true; } catch (Exception ex) { WriteLog(ex); if (key != null) { key.Close(); } return false; } } #endregion private void WriteLog(object exception) { Exception ex = exception as Exception; using (FileStream fs = File.Open(".//ErrorLog.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)) { fs.Seek(0, SeekOrigin.End); byte[] buffer = Encoding.Default.GetBytes("-------------------------------------------------------\r\n"); fs.Write(buffer, 0, buffer.Length); buffer = Encoding.Default.GetBytes(DateTime.Now.ToString() + "\r\n"); fs.Write(buffer, 0, buffer.Length); if (ex != null) { buffer = Encoding.Default.GetBytes("成員名: " + ex.TargetSite + "\r\n"); fs.Write(buffer, 0, buffer.Length); buffer = Encoding.Default.GetBytes("引發異常的類: " + ex.TargetSite.DeclaringType + "\r\n"); fs.Write(buffer, 0, buffer.Length); buffer = Encoding.Default.GetBytes("異常資訊: " + ex.Message + "\r\n"); fs.Write(buffer, 0, buffer.Length); buffer = Encoding.Default.GetBytes("引發異常的程式集或物件: " + ex.Source + "\r\n"); fs.Write(buffer, 0, buffer.Length); buffer = Encoding.Default.GetBytes("棧:" + ex.StackTrace + "\r\n"); fs.Write(buffer, 0, buffer.Length); } else { buffer = Encoding.Default.GetBytes("應用程式錯誤: " + exception.ToString() + "\r\n"); fs.Write(buffer, 0, buffer.Length); } } } }

 

 

 

參考:https://bbs.csdn.net/topics/390951628?page=1

https://www.cnblogs.com/gaobing/p/gaobing.html