C# 创建桌面快捷方式的方法

本文介绍了如何使用C#代码在Windows桌面创建一个新的应用程序快捷方式,包括设置路径、目标应用、参数、描述等属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    /// <summary>
    /// 创建桌面图标
    /// </summary>
    public static void CreateShortcutOnDesktop(string LnkName)
    {

        String shortcutPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), LnkName + ".lnk");
        if (!System.IO.File.Exists(shortcutPath))
        {
            string AppName = System.IO.Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().GetName().Name);
            // 获取当前应用程序目录地址
            String exePath = AppDomain.CurrentDomain.BaseDirectory + AppName + ".exe";
            IW.IWshShell shell = new IW.WshShell();
            // 确定是否已经创建的快捷键被改名了
            foreach (var item in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "*.lnk"))
            {
                IW.WshShortcut tempShortcut = (IW.WshShortcut)shell.CreateShortcut(item);
                if (tempShortcut.TargetPath == exePath)
                {
                    return;
                }
            }
            IW.WshShortcut shortcut = (IW.WshShortcut)shell.CreateShortcut(shortcutPath);
            shortcut.TargetPath = exePath;
            shortcut.Arguments = "";// 参数  
            shortcut.Description = AppName + exePath;
            shortcut.WorkingDirectory = Environment.CurrentDirectory;//程序所在文件夹,在快捷方式图标点击右键可以看到此属性  
            shortcut.IconLocation = exePath;//图标,该图标是应用程序的资源文件  
            //shortcut.Hotkey = "CTRL+SHIFT+W";//热键,发现没作用,大概需要注册一下  
            shortcut.WindowStyle = 1;
            shortcut.Save();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值