WPF修改桌面快捷方式图标

 /// <summary>
        /// 更改快捷方式文件图标
        /// </summary>
        /// <param name="initialSource"></param>
        /// <param name="iconLocation"></param>
        public async static void ChangeLinkIcon(string initialSource, string iconLocation)
        {
            // initialSource = @"C:\Users\Public\Desktop\微信.lnk"; //获取要更改的快捷方式路径
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(initialSource);    //获取快捷方式对象
            //shortcut.Description = "这是修改后的备注信息!";      //修改备注,鼠标放在图标上提示改文字  (右键快捷方式的备注(O))
            shortcut.IconLocation = iconLocation;// @"C:\Users\Administrator\Desktop\2.ico";     
                                                 //其他属性也是如此
            shortcut.Save();
            await Task.Delay(500);

        }
        /// <summary>
        /// 获取快捷方式路径
        /// </summary>
        /// <param name="shortcutFilePath"></param>
        /// <returns></returns>
        static string GetShortcutTargetPath(string shortcutFilePath)
        {
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutFilePath);
            string targetPath = shortcut.TargetPath; // 获取快捷方式的目标路径  
            return targetPath;
        }
     
        private void SelectShortcut_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "快捷方式文件|*.lnk"; // 仅显示快捷方式文件  
            if (openFileDialog.ShowDialog() == true)
            {
                string shortcutPath = openFileDialog.FileName;
                string targetPath = GetShortcutTargetPath(shortcutPath);
                Console.WriteLine("快捷方式的目标路径是: " + targetPath);

                TbkExePath.Text = shortcutPath;
            }
        }

        private void SelectShortcutIcon_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog dlg = new OpenFileDialog();

                dlg.Filter = "ico files (*.ico)|*.ico|All Files(*.*) | *.* ";

                dlg.RestoreDirectory = true;

                if (dlg.ShowDialog() == true)
                {

                    string selectedFileName = dlg.FileName;

                    string saftFileName = dlg.SafeFileName;

                    TbkIconPath.Text = selectedFileName;
                }
                else return;
            }
            catch (Exception ex)
            {

            }
        }

        private void SetShortcutIcon_Click(object sender, RoutedEventArgs e)
        {
            ChangeLinkIcon(TbkExePath.Text, TbkIconPath.Text);
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值