当前位置: 代码迷 >> 综合 >> C# 创建快捷方式、获取快捷方式链接地址
  详细解决方案

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

热度:70   发布时间:2024-01-10 07:04:24.0
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.dllclass ShotCutTool{private static IWshRuntimeLibrary.WshShell shell;/// <summary>/// 为srcFile文件创建快捷方式/// </summary>/// <param name="srcFile">待创建快捷方式的文件</param>/// <param name="linkPath">快捷方式保存完整路径</param>/// <param name="arguments">快捷方式传递的参数信息</param>/// <param name="description">描述</param>/// <param name="hotkey">系统热键</param>/// <param name="iconLocation">快捷方式图标路径</param>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);   // 创建一个指定名称路径的lnkshotcut.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}/// <summary>/// 获取快捷方式的链接地址/// </summary>/// <param name="linkPath">快捷方式路径</param>/// <returns></returns>public static string GetTargetPath(string linkPath){if (shell == null) shell = new IWshRuntimeLibrary.WshShell();IWshRuntimeLibrary.IWshShortcut shotcut = shell.CreateShortcut(linkPath);   // 创建一个指定名称路径的lnkstring targetPath = shotcut.TargetPath;if (targetPath == null) targetPath = "";return targetPath;}}
}