C# winform窗体创建快捷方式

winfrom创建快捷方式

### 2025-4-2 创建快捷方式
调用:
```
    //var lnkPath = Path.Combine(@"C:\Users\Administrator\Desktop\", "数智.lnk");
    var lnkPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "数智.lnk");
    ShortcutHelper.CreateShortcut(lnkPath);
```
```
    /// <summary>
    /// 由于没有打算做安装包,直接以绿色版运行,为了方便调用需要在系统的桌面创建快捷方式,顺便整理一下创建快捷方式的工具类;因为不想引用多余的dll所以放弃采用com组件的实现方式,
    /// 用WScript.shell来实现; WScript是一个基于windows系统脚本宿主对象的根对象,Wscript能够实现模拟键盘,
    /// 向激活的窗口发送字符串和键盘字符,弹出定时对话框,读写注册表,启动程序,创建快捷方式等功能。
    /// </summary>
    public class ShortcutHelper
    {
        /// <summary>
        /// 给当前程序创建快捷方式
        /// </summary>
        /// <param name="lnkFullPath">快捷方式绝对路径</param>
        /// <param name="startupArgs">快捷方式启动参数</param>
        public static void CreateShortcut(string lnkFullPath, string startupArgs = "")
        {
            var shellType = Type.GetTypeFromProgID("WScript.Shell");
            dynamic shell = Activator.CreateInstance(shellType);
            var shortcut = shell.CreateShortcut(lnkFullPath);
            var exeName = $"{Assembly.GetExecutingAssembly().GetName().Name}.exe";
            var exeDir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            // 工作目录和目标路径可以自由指定,注意TargetPath必须是exe的绝对路径
            shortcut.WorkingDirectory = exeDir;
            shortcut.TargetPath = Path.Combine(exeDir, exeName);
            shortcut.Arguments = startupArgs;
            shortcut.Save();
        }
    }


    /// <summary>
    /// 由于没有打算做安装包,直接以绿色版运行,为了方便调用需要在系统的桌面创建快捷方式,顺便整理一下创建快捷方式的工具类;因为不想引用多余的dll所以放弃采用com组件的实现方式,
    /// 用WScript.shell来实现; WScript是一个基于windows系统脚本宿主对象的根对象,Wscript能够实现模拟键盘,
    /// 向激活的窗口发送字符串和键盘字符,弹出定时对话框,读写注册表,启动程序,创建快捷方式等功能。
    /// </summary>
    public class ShortcutHelper
    {
        /// <summary>
        /// 给当前程序创建快捷方式
        /// </summary>
        /// <param name="lnkFullPath">快捷方式绝对路径</param>
        /// <param name="startupArgs">快捷方式启动参数</param>
        public static void CreateShortcut(string lnkFullPath, string startupArgs = "")
        {
            var shellType = Type.GetTypeFromProgID("WScript.Shell");
            dynamic shell = Activator.CreateInstance(shellType);

            //启动程序快捷方式
            var lnkPath = lnkFullPath ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "数智.lnk");
            var shortcut = shell.CreateShortcut(lnkPath);
            var exeName = $"{Assembly.GetExecutingAssembly().GetName().Name}.exe";
            var exeDir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            // 工作目录和目标路径可以自由指定,注意TargetPath必须是exe的绝对路径
            shortcut.WorkingDirectory = exeDir;
            shortcut.TargetPath = Path.Combine(exeDir, exeName);
            shortcut.Arguments = startupArgs;
            shortcut.Save();

            //指定文件夹快捷方式
            var shortcut01 = shell.CreateShortcut(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "PDF文档.lnk"));
            //var exeName01 = $"{Assembly.GetExecutingAssembly().GetName().Name}.exe";
            var exeDir01 = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            // 工作目录和目标路径可以自由指定,注意TargetPath必须是exe的绝对路径
            shortcut01.WorkingDirectory = exeDir01;
            string targetPath = Path.Combine(exeDir01, "PDF文档");
            if (!Directory.Exists(targetPath))
            {
                Directory.CreateDirectory(targetPath);
            }
            shortcut01.TargetPath = targetPath;
            shortcut01.Arguments = startupArgs;
            shortcut01.Save();
        }


        #region IWshShortcut 类来至于命名空间 IWshRuntimeLibrary。在你的Visual Studio项目中,右键点击“引用”->“添加引用”。在弹出的对话框中,选择“COM”选项卡。找到并勾选“Microsoft Windows Script Host Object Model”,然后点击“确定”。
        /// <summary>
        /// 创建快捷方式文件
        /// </summary>
        /// <param name="directory">快捷方式所处的文件夹</param>
        /// <param name="shortcutName">快捷方式名称</param>
        /// <param name="targetPath">目标路径</param>
        /// <param name="description">描述</param>
        /// <param name="iconLocation">图标路径,格式为"可执行文件或DLL路径, 图标编号",
        /// 例如System.Environment.SystemDirectory + "\\" + "shell32.dll, 165"</param>
        /// <remarks></remarks>
        public static void CreateShortcut(string directory, string shortcutName, string targetPath, string description, string iconLocation)
        {
            //文件夹不存在,则创建它
            if (!System.IO.Directory.Exists(directory))
            {
                System.IO.Directory.CreateDirectory(directory);
            }
            string shortcutPath = $"{directory}\\{shortcutName}.lnk";
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);//创建快捷方式对象
            shortcut.TargetPath = targetPath;//指定目标路径
            shortcut.WorkingDirectory = System.IO.Path.GetDirectoryName(targetPath);//设置起始位置
            shortcut.WindowStyle = 1;//设置运行方式,状态分为普通、最大化、最小化【1,3,7】
            shortcut.Description = description;//设置备注
            shortcut.IconLocation = string.IsNullOrEmpty(iconLocation) ? targetPath : iconLocation;//设置图标路径
            shortcut.Save();//保存快捷方式
        }

        #endregion


        #region 调用windows自带的的C++接口CreateHardLink(),它引用于系统动态库"Kernel32.dll"。
        [DllImport("Kernel32.dll", CharSet = CharSet.Unicode)]
        public extern static bool CreateHardLink(string lpFileName, string lpExistingFileName, IntPtr lpSecurityAttributes);

        /// <summary>
        /// 创建硬链接文件
        /// </summary>
        /// <param name="linkNamePath">链接路径</param>
        /// <param name="sourceNamePath">源文件路径</param>
        public static bool CreateHardLinkFile(string linkNamePath, string sourceNamePath)
        {
            bool result = false;
            // 删除目标文件(如果存在)
            if (System.IO.File.Exists(linkNamePath))
            {
                System.IO.File.Delete(linkNamePath);
            }
            try
            {
                //创建硬链接文件,句柄设置为0
                result = CreateHardLink(linkNamePath, sourceNamePath, IntPtr.Zero);
            }
            catch (Exception ex)
            {
                Console.WriteLine("CreateHardLinkFile error " + ex.ToString());
            }

            if (result)
            {
                Console.WriteLine($"{linkNamePath}硬链接创建成功!");
            }
            else
            {
                Console.WriteLine($"{linkNamePath}硬链接创建失败!");
            }
            return result;
        }

        #endregion

    }
```

 

posted @ 2025-04-14 09:40  龙骑科技  阅读(78)  评论(0)    收藏  举报