C# 创建快捷方式、获取快捷方式链接地址

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Sci
{
    // 添加引用 -> com组件 -> Windows Script Host Object Model
    // Interop.IWshRuntimeLibrary.dll
    class ShotCutTool
    {
        private static IWshRuntimeLibrary.WshShell shell;

        /// 
        /// 为srcFile文件创建快捷方式
        /// 
        /// 待创建快捷方式的文件
        /// 快捷方式保存完整路径
        /// 快捷方式传递的参数信息
        /// 描述
        /// 系统热键
        /// 快捷方式图标路径
        public static void CreateShotCut(string srcFile, string linkPath = null, string arguments = null, string description = null, string hotkey = null, string iconLocation = null)
        {
            if (linkPath == null) linkPath = srcFile;
            linkPath += ".lnk";
            if (File.Exists(linkPath)) File.Delete(linkPath);               // 删除原有的lnk文件

            if(shell == null) shell = new IWshRuntimeLibrary.WshShell();
            IWshRuntimeLibrary.IWshShortcut shotcut = shell.CreateShortcut(linkPath);   // 创建一个指定名称路径的lnk
            shotcut.TargetPath = srcFile;                                               // 待创建链接的原文件

            if(arguments != null) shotcut.Arguments = arguments;            // 传递参数
            if (description != null) shotcut.Description = description;     // 链接描述
            if (hotkey != null) shotcut.Hotkey = hotkey;                    // 全局热键, 如:"CTRL+SHIFT+N"
            if (iconLocation != null) shotcut.IconLocation = iconLocation;  // 设置Icon图标

            shotcut.Save();                     // 保存link
        }

        /// 
        /// 获取快捷方式的链接地址
        /// 
        /// 快捷方式路径
        /// 
        public static string GetTargetPath(string linkPath)
        {
            if (shell == null) shell = new IWshRuntimeLibrary.WshShell();
            IWshRuntimeLibrary.IWshShortcut shotcut = shell.CreateShortcut(linkPath);   // 创建一个指定名称路径的lnk

            string targetPath = shotcut.TargetPath;
            if (targetPath == null) targetPath = "";
            return targetPath;
        }
    }
}

你可能感兴趣的:(C#,创建快捷方式)