c# 更改註冊表開機啓動+mutex(互斥,同一時間同一臺電腦只能運行一個程序)

1.更改註冊表,使開機啓動

在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run下增加一個鍵,鍵值爲程序路徑即可,代碼如下:

    string strName = Application.ExecutablePath;
            string strnewName = strName.Substring(strName.LastIndexOf("\\") + 1);
            if (!checkBox_start.Checked)
            {
                //修改註冊表,使程序開機時不自動執行。  
                Microsoft.Win32.RegistryKey Rkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
                Rkey.DeleteValue(strnewName, false);
                MessageBox.Show("程序設置完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                if (!File.Exists(strName))//指定文件是否存在  
                    return;
                Microsoft.Win32.RegistryKey Rkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                if (Rkey == null)
                    Rkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
                Rkey.SetValue(strnewName, strName);//修改註冊表,使程序開機時自動執行。 
                MessageBox.Show("程序設置完成,重新啓動計算機後即可生效!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

2.通過使用mutex(互斥),確保當前電腦裏只有一個該程序在運行(需引用命名空間:using System.Threading;,或者代替mutex爲System.Threading.Mutex):

 互斥進程(程序), 簡單點說,就是在系統中只能有該程序的一個實例運行 要實現程序的互斥,通常有三中方式,下面用 C#  語言來實現:

實現方式一: 使用線程互斥變量. 通過定義互斥變量來判斷是否已運行實例.C#實現如下:

    把program.cs文件裏的Main()函數改爲如下代碼:

        static void Main()
        {
            bool runone;
            Mutex run = new Mutex(true, @"Global\TaskPublish", out  runone);
            if (runone)
            {
              run.ReleaseMutex();
              Application.EnableVisualStyles();
              Application.SetCompatibleTextRenderingDefault(false);
              Application.Run(new Form1());
            }
            else
            {
                MessageBox.Show("已經運行了一個實例了。");
            }
        }

  說明:程序中通過語句 System.Threading.Mutex run = new System.Threading.Mutex(true, "xinbiao_a_test", out runone);來申明一個互斥體變量run,其中"xinbiao_a_test"爲互斥體名,布爾變量runone用來保存是否已經運行了該程序事例.

實現方式二:   採用判斷進程的方式,我們在運行程序前,查找進程中是否有同名的進程,同時運行位置也相同程,如是沒有運行該程序,如果有就就不運行.在C#中應用System.Diagnostics名字空間中的Process類來實現,主要代碼如下:

        1,在program.cs文件中添加函數如下:

        public static System.Diagnostics.Process RunningInstance() 
        {
            System.Diagnostics.Process current = System.Diagnostics.Process.GetCurrentProcess();
            System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process process in processes) //查找相同名稱的進程
            {
                if (process.Id != current.Id)  //忽略當前進程
                { //確認相同進程的程序運行位置是否一樣. 
                    if (System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", @"\") == current.MainModule.FileName) 
                    { //Return the other process instance.                       
                        return process;
                    } 
                } 
            } //No other instance was found, return null. 
            return null; 
        }  

        2,把Main ()函數改爲如下代碼:

        static void Main()
        {
            if(RunningInstance()==null)
            { 
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            else
            {
                MessageBox.Show("已經運行了一個實例了。");
            }
        }

實現方式三:全局原子法,創建程序前,先檢查全局原子表中看是否存在特定原子A(創建時添加的),存在時停止創建,說明該程序已運行了一個實例;不存在則運行程序並想全局原子表中添加特定原子A;退出程序時要記得釋放特定的原子A哦,不然要到關機纔會釋放。C#實現如下:

1、申明WinAPI函數接口:

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]

        public static extern UInt32 GlobalAddAtom(String lpString);  //添加原子

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]

        public static extern UInt32 GlobalFindAtom(String lpString);  //查找原子

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]

        public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom);  //刪除原子

2、修改Main()函數如下:

        static void Main()
        {
            if (GlobalFindAtom("xinbiao_test") == 77856768) //沒找到原子"xinbiao_test"
            {
                GlobalAddAtom("xinbiao_test");  //添加原子"xinbiao_test"
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            else
            {
                MessageBox.Show("已經運行了一個實例了。");
            }                        
        }


3、在FormClosed事件中添加如下代碼:

       GlobalDeleteAtom(GlobalFindAtom("xinbiao_test"));//刪除原子"xinbiao_test"

以上爲創建互斥程序(進程)的基本通用的思想,個人認爲,第一種方法最好。以上所有代碼都在VS.NET2005 中測試通過。

互斥進程(程序), 簡單點說,就是在系統中只能有該程序的一個實例運行. 現在很多軟件都有這功能,如Maxthon 可以設置爲"只允許打開一個窗體",還有Bitcomet等. 我也是看到這些軟件的這個功能纔來研究這個問題的.  要實現程序的互斥,通常有三中方式,下面用 C#  語言來實現:

實現方式一: 使用線程互斥變量. 通過定義互斥變量來判斷是否已運行實例.C#實現如下:

    把program.cs文件裏的Main()函數改爲如下代碼:

        static void Main()
        {
            bool runone;
            System.Threading.Mutex run = new System.Threading.Mutex(true, "xinbiao_a_test", out runone);
            if (runone)
            {
              run.ReleaseMutex();
              Application.EnableVisualStyles();
              Application.SetCompatibleTextRenderingDefault(false);
              Application.Run(new Form1());
            }
            else
            {
                MessageBox.Show("已經運行了一個實例了。");
            }
        }

  說明:程序中通過語句 System.Threading.Mutex run = new System.Threading.Mutex(true, "xinbiao_a_test", out runone);來申明一個互斥體變量run,其中"xinbiao_a_test"爲互斥體名,布爾變量runone用來保存是否已經運行了該程序事例.

實現方式二:   採用判斷進程的方式,我們在運行程序前,查找進程中是否有同名的進程,同時運行位置也相同程,如是沒有運行該程序,如果有就就不運行.在C#中應用System.Diagnostics名字空間中的Process類來實現,主要代碼如下:

        1,在program.cs文件中添加函數如下:

        public static System.Diagnostics.Process RunningInstance() 
        {
            System.Diagnostics.Process current = System.Diagnostics.Process.GetCurrentProcess();
            System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process process in processes) //查找相同名稱的進程
            {
                if (process.Id != current.Id)  //忽略當前進程
                { //確認相同進程的程序運行位置是否一樣. 
                    if (System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", @"\") == current.MainModule.FileName) 
                    { //Return the other process instance.                       
                        return process;
                    } 
                } 
            } //No other instance was found, return null. 
            return null; 
        }  

        2,把Main ()函數改爲如下代碼:

        static void Main()
        {
            if(RunningInstance()==null)
            { 
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            else
            {
                MessageBox.Show("已經運行了一個實例了。");
            }
        }

實現方式三:全局原子法,創建程序前,先檢查全局原子表中看是否存在特定原子A(創建時添加的),存在時停止創建,說明該程序已運行了一個實例;不存在則運行程序並想全局原子表中添加特定原子A;退出程序時要記得釋放特定的原子A哦,不然要到關機纔會釋放。C#實現如下:

1、申明WinAPI函數接口:

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]

        public static extern UInt32 GlobalAddAtom(String lpString);  //添加原子

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]

        public static extern UInt32 GlobalFindAtom(String lpString);  //查找原子

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]

        public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom);  //刪除原子

2、修改Main()函數如下:

        static void Main()
        {
            if (GlobalFindAtom("xinbiao_test") == 77856768) //沒找到原子"xinbiao_test"
            {
                GlobalAddAtom("xinbiao_test");  //添加原子"xinbiao_test"
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            else
            {
                MessageBox.Show("已經運行了一個實例了。");
            }                        
        }


3、在FormClosed事件中添加如下代碼:

       GlobalDeleteAtom(GlobalFindAtom("xinbiao_test"));//刪除原子"xinbiao_test"

以上爲創建互斥程序(進程)的基本通用的思想,個人認爲,第一種方法最好。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章