利用C#实现动态替换桌面快捷方式对应的应用程序

公司有一个特殊的业务可能会用到这个,至于什么业务就不展开了。本文的内容作为备用方案。

实现思路:

1   获取当前exe程序运行的全路径

2  获取桌面的所有快捷方式

3  遍历快捷方式,获取快捷键方式对应程序的运行路径,并与当前exe程序运行的全路径比较,如果相等,则表示拿到当前exe程序运行的快捷方式

4  把新的程序的运行路径赋值给快捷方式

测试环境:

.net framework 3.5 

vistual studio 2017

测试步骤如下:

1  新建控制台程序,名为ApplicationQuickLink

2  添加COM引用:Window  Script Host Object  Model

利用C#实现动态替换桌面快捷方式对应的应用程序_第1张图片

 3  在Debug目录下新建记事本文件,名为"新建文本文档.txt",并编辑内容如下:

利用C#实现动态替换桌面快捷方式对应的应用程序_第2张图片

 4  新建类,名为"ShortCutHelper" ,并编辑如下:

using IWshRuntimeLibrary;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ApplicationQuickLink
{
    public class ShortCutHelper
    {
        /// 
        /// 设置当前程序的快捷方式
        /// 
        /// 新的程序名称
        public static bool SetApplicationNewQuickNewPath(string newAppName)
        {
            IWshRuntimeLibrary.IWshShortcut  shortcut= GetCurrentApplicationQuickPath();
            if (shortcut!=null&& !string.IsNullOrEmpty(shortcut.TargetPath))
            {
                shortcut.TargetPath = GetExecuteAppPath(shortcut.TargetPath)+ "\\" + newAppName;
                shortcut.WindowStyle = 1;//设置运行方式,默认为常规窗口
                shortcut.Save();
                return true;
            }
            return false;
        }
        /// 获取程序的运行路径
        /// 
        /// 
        /// 
        private static string GetExecuteAppPath(string executeFullPath)
        {
            string []pathArray = executeFullPath.Split('\\');
            if (pathArray.Length >= 2)
            {
                return executeFullPath.Replace(pathArray[pathArray.Length - 1],"");
            }
            return null;
        }     
        /// 
        /// 获取当前应用程序快捷方式
        /// 
        /// 
        private static IWshRuntimeLibrary.IWshShortcut GetCurrentApplicationQuickPath()
        {
            string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            string[] desktopAllLinkArray = Directory.GetFiles(desktopPath, "*.lnk");
            if (desktopAllLinkArray != null && desktopAllLinkArray.Length > 0)
            {
                foreach (var item in desktopAllLinkArray)
                {
                    IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShellClass();
                    IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(item);
                    string currentExcutePath= GetCurrentExecuteFullPath();
                    if (currentExcutePath.Equals(shortcut.TargetPath))
                    {
                        return shortcut;
                    }
                }
            }
            return null;
        }
        /// 
        /// 获取当前程序运行的全路径
        /// 
        /// 
        private static string GetCurrentExecuteFullPath()
        {
           return Application.ExecutablePath;
        }
    }
}

5  编辑主程序如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ApplicationQuickLink
{
    class Program
    {
        static void Main(string[] args)
        {
            bool setResult= ShortCutHelper.SetApplicationNewQuickNewPath("新建文本文档.txt");
            if (setResult)
            {
                Console.WriteLine("设置成功");
            }
            else
            {
                Console.WriteLine("设置失败");
            }
            Console.ReadLine();
        }
    }
}

6  生成程序,并为Debug目录下的运行程序ApplicationQuickLink.exe创建桌面快捷方式,如下图:

利用C#实现动态替换桌面快捷方式对应的应用程序_第3张图片

 

7  点击桌面的快捷方式实现运行程序,运行结果如下图:

利用C#实现动态替换桌面快捷方式对应的应用程序_第4张图片

桌面的快捷方式对应的目标程序如下:

利用C#实现动态替换桌面快捷方式对应的应用程序_第5张图片

 点击桌面的快捷方式,可以成功打开Debug目录下的记事本文件"新建文本文档.txt",如下图:

利用C#实现动态替换桌面快捷方式对应的应用程序_第6张图片

 

 好了,本文的内容到此结束。

你可能感兴趣的:(C#编程,c#,快捷方式,桌面,lnk,动态)