search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

C#建立啟動捷徑-設定開機 程式(軟體)自動啟動(Auto Run) – jashliao部落格

C#建立啟動捷徑-設定開機 程式(軟體)自動啟動(Auto Run)

 

資料來源:: http://www.codeproject.com/Articles/146757/Add-Remove-Startup-Folder-Shortcut-to-Your-App

 

using System;

using System.IO;

using System.Windows.Forms;

 

// Using IWshRuntimeLibrary requires a COM reference to ‘Windows Script Host Object Model’

using IWshRuntimeLibrary;

 

// Using Shell32 requires a COM reference to ‘Microsoft Shell Controls and Automation’

using Shell32;

 

namespace StartupFolderShortcut

{

  public partial class Form1 : Form

  {

    public Form1()

    {

      InitializeComponent();

    }

 

    private void buttonCreate_Click(object sender, EventArgs e)

    {

      CreateStartupFolderShortcut();

    }

 

    private void buttonDelete_Click(object sender, EventArgs e)

    {

      DeleteStartupFolderShortcuts(Path.GetFileName(Application.ExecutablePath)) ;

    }

 

    public void CreateStartupFolderShortcut()

    {

      WshShellClass wshShell = new WshShellClass();

      IWshRuntimeLibrary.IWshShortcut shortcut;

      string startUpFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);

 

      // Create the shortcut

      shortcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(startUpFolderPath + “\\” + Application.ProductName + “.lnk”);

 

      shortcut.TargetPath = Application.ExecutablePath;

      shortcut.WorkingDirectory = Application.StartupPath;

      shortcut.Description = “Launch My Application”;

//      shortcut.IconLocation = Application.StartupPath + @”\App.ico”;

      shortcut.Save();

    }

 

    public string GetShortcutTargetFile(string shortcutFilename)

    {

      string pathOnly = Path.GetDirectoryName(shortcutFilename);

      string filenameOnly = Path.GetFileName(shortcutFilename);

 

      Shell32.Shell shell = new Shell32.ShellClass();

      Shell32.Folder folder = shell.NameSpace(pathOnly);

      Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);

      if (folderItem != null)

      {

        Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;

        return link.Path;

      }

 

      return String.Empty; // Not found

    }

 

    public void DeleteStartupFolderShortcuts(string targetExeName)

    {

      string startUpFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);

 

      DirectoryInfo di = new DirectoryInfo(startUpFolderPath);

      FileInfo[] files = di.GetFiles(“*.lnk”);

 

      foreach (FileInfo fi in files)

      {

        string shortcutTargetFile = GetShortcutTargetFile(fi.FullName);

        Console.WriteLine(“{0} -> {1}”, fi.Name, shortcutTargetFile);

 

        if (shortcutTargetFile.EndsWith(targetExeName, StringComparison.InvariantCultureIgnoreCase))

        {

          System.IO.File.Delete(fi.FullName);

        }

      }

    }

 

  }

}

 

 

 


 




熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦