Winform 創建桌面快捷方式並開機啓動

文章一:

快捷方式實質上是一個擴展名爲 .LNK 的文件

方法如下:

首先要添加引用 (如圖)

 

 

就是那個Windows Script Host Object Model的類庫....

 

然後在程序中引入命名空間

using  IWshRuntimeLibrary;

 有一些文件操作,所有要引入

using  System.IO;

關鍵方法如下:

///   <summary>
///  創建桌面快捷方式並開機啓動的方法
///   </summary>
private   void  ShortcutAndStartup()
{
      // 獲取當前系統用戶啓動目錄
      string  startupPath  =  Environment.GetFolderPath(Environment.SpecialFolder.Startup);
      // 獲取當前系統用戶桌面目錄
      string  desktopPath  =  Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

     FileInfo fileStartup  =   new  FileInfo(startupPath  +   " //億掌通.lnk " );
     FileInfo fileDesktop  =   new  FileInfo(desktopPath  +   " //億掌通.lnk " );

      if  ( ! fileDesktop.Exists)
     {
           WshShell shell  =   new  WshShell();
           IWshShortcut shortcut  =  (IWshShortcut)shell.CreateShortcut(
                 Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)  +
                   " // "   +   " 億掌通.lnk "
                  );
           shortcut.TargetPath  =  Application.StartupPath  +   " // "   +   " Upgrade.exe " ; // 啓動更新程序★
           shortcut.WorkingDirectory  =  System.Environment.CurrentDirectory;
           shortcut.WindowStyle  =   1 ;
           shortcut.Description  =   " 億掌通 " ;
           shortcut.IconLocation  =  Application.ExecutablePath;
           shortcut.Save();
      }

       if  ( ! fileStartup.Exists)
      {
             // 獲取可執行文件快捷方式的全部路徑
             string  exeDir  =  desktopPath  +   " //億掌通.lnk " ;
             // 把程序快捷方式複製到啓動目錄
            System.IO.File.Copy(exeDir, startupPath  +   " //億掌通.lnk " ,  true );
      }
}

 

 

文章二:

創建桌面快捷方式+設置開機啓動代碼[C#、WinForm]

 

注意:

1.創建桌面快捷方式需要引用IWshRuntimeLibrary類庫

2.設置開機啓動需要使用Microsoft.Win32的Registry類

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//-----------------------
using System.IO;
using Microsoft.Win32;
using IWshRuntimeLibrary;
namespace OneApp
{
    public partial class FormOption : Form
    {
        public FormOption()
        {
            InitializeComponent();
        }
        private void FormOption_Load(object sender, EventArgs e)
        {
            //檢查是否開機啓動
            RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run", true);//打開註冊表子項
            if (key == null)
            {
                key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run");
            }
            checkBox1.Checked = (key.GetValue("發貨審覈") != null);
            key.Close();
            //檢查是否建立桌面快捷方式
            bool b = System.IO.File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) +
                    "//" + "發貨審覈.lnk");
            checkBox2.Checked = b; 
        }
        private void buttonOption_Click(object sender, EventArgs e)
        {
            //設置爲開機啓動
            RunWhenStart(checkBox1.Checked, "發貨審覈", Application.ExecutablePath);
            //創建桌面快捷方式
            CreateDesktopLink(checkBox2.Checked);
        }
        private void RunWhenStart(bool Started, string name, string path)
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run", true);//打開註冊表子項
            if (key == null)
            {
                key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run");
            }
            if (Started == true)//設置開機啓動
            {
                try
                {
                    key.SetValue(name, path);
                    key.Close();
                }
                catch (Exception Err)
                {
                    MessageBox.Show(Err.Message);
                }
            }
            else//取消開機啓動
            {
                try
                {
                    if (key.GetValue(name) != null)
                    {
                        key.DeleteValue(name, false);
                        key.Close();
                    }
                }
                catch (Exception Ex)
                {
                    MessageBox.Show(Ex.Message);
                }
            }
        }
        private void CreateDesktopLink(bool Created)
        {
            if (Created == true)
            {
                //先判斷是否存在
                if (!System.IO.File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) +
                    "//" + "發貨審覈.lnk"))
                {
                    WshShell shell = new WshShell();
                    IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(
                        Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) +
                        "//" + "發貨審覈.lnk"
                        );
                    shortcut.TargetPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
                    shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
                    shortcut.WindowStyle = 1; //Normal window 
                    shortcut.Description = "發貨審覈插件";
                    //shortcut.IconLocation = System.Environment.SystemDirectory + "//" + "shell32.dll, 165";
                    shortcut.Save();
                }
            }
            else
            {
                //先判斷是否存在
                if (System.IO.File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) +
                    "//" + "發貨審覈.lnk"))
                {
                    System.IO.File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) +
                    "//" + "發貨審覈.lnk");
                }
            }
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章