[AaronYang]C#人爱学不学16-创建快捷方式

引用COM

Windows Script Host Object Model 

然后把IWshRuntimeLibrary的引用-属性,嵌入互操作类型,改为false

参考代码如下

private static void CreateDesktopLnk()
        {

            string DesktopPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);//得到桌面文件夹 
            IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShellClass();


            IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(DesktopPath + "\\QQ软件.lnk");
            shortcut.TargetPath = @"C:\Program Files\Tencent\QQ\Bin\QQ.exe";
            shortcut.Arguments = "";// 参数 
            shortcut.Description = "自建的QQ快捷方式";
            shortcut.WorkingDirectory = @"C:\Program Files\Tencent\QQ\Bin\";//程序所在文件夹,在快捷方式图标点击右键可以看到此属性 
            shortcut.IconLocation = @"C:\Program Files\Tencent\QQ\Bin\QQ.exe,0";//图标 
            //shortcut.Hotkey = "CTRL+SHIFT+X";//热键 
            shortcut.WindowStyle = 1;
            shortcut.Save();


            /** 网站的快捷方式
             */
            IWshRuntimeLibrary.IWshShortcut shortcutWeb = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(DesktopPath + "\\www.lnk");
            shortcutWeb.TargetPath = @"%HOMEDRIVE%/Program Files\Internet Explorer\IEXPLORE.EXE";
            shortcutWeb.Arguments = "http://sosoft.cnblogs.com";// 参数 
            shortcutWeb.Description = "sosoft.cnblogs.com";
            shortcutWeb.WorkingDirectory = Application.StartupPath;//程序所在文件夹,在快捷方式图标点击右键可以看到此属性 
            shortcutWeb.IconLocation = @"%HOMEDRIVE%/Program Files\Internet Explorer\IEXPLORE.EXE, 0";//图标 
            shortcutWeb.Hotkey = "CTRL+SHIFT+Z";//热键 
            shortcutWeb.WindowStyle = 1;
            shortcutWeb.Save();

        }

当然我自己封装了一个类,方便调用

public static class MsiHelper
    {
    

        private static RegistryKey OpenRegistryPath(RegistryKey regKey, string regPath)
        {
            RegistryKey registryKey = regKey;
            string text = regPath.Remove(0, 1) + "\\";
            while (text.IndexOf("\\") != -1)
            {
                registryKey = registryKey.OpenSubKey(text.Substring(0, text.IndexOf("\\")));
                text = text.Remove(0, text.IndexOf("\\") + 1);
            }
            return registryKey;
        }

        public static string GetCommonDesktopPath()
        {
            string regPath = "\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders";
            object value = OpenRegistryPath(Registry.LocalMachine, regPath).GetValue("Common Desktop");
            return (value != null) ? value.ToString() : string.Empty;
        }

        public static void CreateDesktopLink(string shortcutName, string targetPath, string workingDirectory, string arguments, string description)
        {
            try
            {
                string commonDesktopPath = GetCommonDesktopPath();
                string text = string.Format("{0}\\{1}.lnk", commonDesktopPath, shortcutName);
                if (!System.IO.File.Exists(text))
                {
                    WshShell wshShell = new WshShellClass();
                    IWshShortcut wshShortcut = (IWshShortcut)wshShell.CreateShortcut(text);
                    wshShortcut.TargetPath = targetPath;
                    wshShortcut.Arguments = arguments;
                    wshShortcut.Description = description;
                    wshShortcut.WorkingDirectory = workingDirectory;
                    wshShortcut.IconLocation = string.Format("{0},0", targetPath);
                    wshShortcut.WindowStyle = 1;
                    wshShortcut.Save();
                }
            }
            catch (Exception ex)
            {
              
            }
        }
        
        public static void CreateDesktopLink(string shortcutName, string targetPath, string workingDirectory, string arguments, string description,string IconLocation)
        {
            try
            {
                string commonDesktopPath = GetCommonDesktopPath();
                string text = string.Format("{0}\\{1}.lnk", commonDesktopPath, shortcutName);
                if (!System.IO.File.Exists(text))
                {
                    WshShell wshShell = new WshShellClass();
                    IWshShortcut wshShortcut = (IWshShortcut)wshShell.CreateShortcut(text);
                    wshShortcut.TargetPath = targetPath;
                    wshShortcut.Arguments = arguments;
                    wshShortcut.Description = description;
                    wshShortcut.WorkingDirectory = workingDirectory;
                    wshShortcut.IconLocation = IconLocation;
                    wshShortcut.WindowStyle = 1;
                    wshShortcut.Save();
                }
            }
            catch (Exception ex)
            {

            }
        }

      
    }

调用示例

MsiHelper.CreateDesktopLink(你的文本, string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.FriendlyName), string.Format("{0}", AppDomain.CurrentDomain.BaseDirectory), 启动参数, desc);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值