1. 程式人生 > >C#設定開機啟動的方法

C#設定開機啟動的方法

       public void SetSetupWindowOpenRun(bool _bool)
        {
            if (_bool)
            {
                string setupPath = this.GetType().Assembly.Location;
                string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + "aaaaaaaaaaaa" + ".lnk";
                if (System.IO.File.Exists(desktop))
                    System.IO.File.Delete(desktop);
                IWshRuntimeLibrary.WshShell shell;
                IWshRuntimeLibrary.IWshShortcut shortcut;
                try
                {
                    shell = new IWshRuntimeLibrary.WshShell();
                    shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(desktop);
                    shortcut.TargetPath = setupPath;//程式路徑
                    shortcut.Arguments = "";//引數
                    shortcut.Description = "aaaaaaaaaaaa";//描述
                    shortcut.WorkingDirectory = System.IO.Path.GetDirectoryName(setupPath);//程式所在目錄
                    shortcut.IconLocation = setupPath;//圖示   
                    shortcut.WindowStyle = 1;
                    shortcut.Save();
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message, "友情提示");
                }
                finally
                {
                    shell = null;
                    shortcut = null;
                }
            }
            else
            {
                System.IO.File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + "aaaaaaaaaaaa" + ".lnk");
            }
        }