c# 创建快捷方式

之前网上找了几个方法,创建快捷方式,到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;
using System.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;
        }
原文地址:https://www.cnblogs.com/nanfei/p/2963498.html