c# 建立快捷方式
阿新 • • 發佈:2020-11-13
之前網上找了幾個方法,建立快捷方式,到windows 7 下竟然報錯,或沒有許可權,今天經過一番搜尋,終於找到一個很簡單的方法:
首先新增以下引用:COM下Windows Script Host Object Model
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; usingSystem.Runtime.InteropServices; using IWshRuntimeLibrary; using System.IO; namespace WindowsFormsTest3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { IWshShell_Class shell= new IWshShell_ClassClass(); var shortcut = shell.CreateShortcut(System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory) + "\\" + new FileInfo(Application.ExecutablePath).Name + ".lnk") as IWshShortcut; shortcut.TargetPath = Application.ExecutablePath; shortcut.Save(); } } }
建立URL快捷方式
private bool createShortcut(string url) { var deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); using (var sw = new StreamWriter(deskDir + "\\網上閱卷系統.url")) { sw.WriteLine("[InternetShortcut]"); sw.WriteLine("URL=" + url); sw.Flush(); } return true; }