搜索本机是否安装指定程序

添加引用 Interop.IWshRuntimeLibrary.dll

第一步:判断指定程序是否正在运行A.exe;

第二部:查找(B.exe)1、通过查找注册表(xp系统)查找程序的安装路径;2、通过查找注册表(win7,10等)、桌面快捷方式查找程序的安装目录;3、通过开始菜单快捷方式查找安装目录

第三部:查找到B.exe,通过注册表查找A.exe,找到则开启 Process.Start(path); 相关代码如下:


        /// <summary>
        ///     根据注册表key读取内容
        /// </summary>
        public static string GetValue(string keyPath, string keyName)
        {
            RegistryKey key = Registry.LocalMachine;
            try
            {
                RegistryKey myreg = key.OpenSubKey(keyPath);
                if (!Equals(myreg, null))
                    return myreg.GetValue(keyName).ToString();
                return string.Empty;
            }
            finally
            {
                key.Close();
            }
        }


using IWshRuntimeLibrary;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class CommonUtil
    {
        /// <summary>
        /// 检测指定程序是否正在运行
        /// </summary>
        public static bool IsRunning(string filename)
        {
            var ls = Process.GetProcessesByName(filename).ToList();
            if (ls.Count != 0) return true;
            return false;
        }


        /// <summary>
        /// Opens the registry path.
        /// </summary>
        /// <param name="root">The root.</param>
        /// <param name="s">The s.</param>
        /// <returns></returns>
        private static RegistryKey OpenRegistryPath(RegistryKey root, string s)
        {
            s = s.Remove(0, 1) + @"/";
            while (s.IndexOf(@"/", StringComparison.Ordinal) != -1)
            {
                if (root != null)
                {
                    root = root.OpenSubKey(s.Substring(0, s.IndexOf(@"/", StringComparison.Ordinal)));
                    s = s.Remove(0, s.IndexOf(@"/", StringComparison.Ordinal) + 1);
                }
            }
            return root;
        }


        /// <summary>
        /// 获取公用桌面路径
        /// </summary>
        /// <returns></returns>
        public static string GetAllUsersDesktopFolderPath()
        {
            RegistryKey folders = OpenRegistryPath(Registry.LocalMachine, @"/software/microsoft/windows/currentversion/explorer/shell folders");
            string desktopPath = folders.GetValue("Common Desktop").ToString();
            return desktopPath;
        }


        /// <summary>
        /// 通过快捷方式获取安装文件
        /// </summary>
        /// <param name="lnkPath">The LNK path.</param>
        /// <returns></returns>
        public static string GetAppinstallFileByShortcut(string lnkPath)
        {
            string taxAppInstallPath = string.Empty;
            WshShell wshShellClass = new WshShellClass();
            IWshShortcut wshShortcut = (IWshShortcut)wshShellClass.CreateShortcut(lnkPath);
            if (!Equals(wshShortcut, null))
            {
                if (!string.IsNullOrEmpty(wshShortcut.TargetPath))
                {
                    taxAppInstallPath = wshShortcut.TargetPath;
                }
            }
            return taxAppInstallPath;
        }


        /// <summary>
        /// 通过快捷方式获取安装目录
        /// </summary>
        /// <param name="lnkPath">The LNK path.</param>
        /// <returns></returns>
        public static string GetAppinstallPathByShortcut(string lnkPath)
        {
            string taxAppInstallPath = string.Empty;
            WshShell wshShellClass = new WshShellClass();
            IWshShortcut wshShortcut = (IWshShortcut)wshShellClass.CreateShortcut(lnkPath);
            if (!Equals(wshShortcut, null))
            {
                if (!string.IsNullOrEmpty(wshShortcut.TargetPath))
                {
                    taxAppInstallPath = wshShortcut.TargetPath;
                }
            }
            if (!string.IsNullOrEmpty(taxAppInstallPath) && taxAppInstallPath.EndsWith(".exe", StringComparison.Ordinal))
            {
                taxAppInstallPath = taxAppInstallPath.Substring(0, taxAppInstallPath.LastIndexOf("\\", StringComparison.Ordinal) + 1);
            }
            return taxAppInstallPath;
        }


        /// <summary>
        ///通过快捷方式获取目录
        /// </summary>
        /// <param name="linkName">快捷方式名称</param>
        /// <param name="IsContain">是否包含文件名称</param>
        /// <returns></returns>
        public static string GetAppInstallPathByLink(string linkName, bool IsContain = false)
        {
            string taxAppInstallPath = string.Empty;
            try
            {
                //获取所有用户桌面目录
                string allUsersDesktopFolderPath = GetAllUsersDesktopFolderPath();
                //获取当前系统用户桌面目录
                string currentUserDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
                if (string.IsNullOrEmpty(taxAppInstallPath))
                {
                    string lnkPath = Path.Combine(allUsersDesktopFolderPath, linkName);
                    if (System.IO.File.Exists(lnkPath))
                    {
                        if (IsContain)
                        {
                            taxAppInstallPath = GetAppinstallFileByShortcut(lnkPath);
                        }
                        else
                        {
                            taxAppInstallPath = GetAppinstallPathByShortcut(lnkPath);
                        }


                    }
                }
                if (string.IsNullOrEmpty(taxAppInstallPath))
                {
                    string lnkPath = Path.Combine(currentUserDesktopPath, linkName);
                    if (System.IO.File.Exists(lnkPath.Trim()))
                    {
                        if (IsContain)
                        {
                            taxAppInstallPath = GetAppinstallFileByShortcut(lnkPath);
                        }
                        else
                        {
                            taxAppInstallPath = GetAppinstallPathByShortcut(lnkPath);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Log.Error("安装目录出现错误:" + exception.Message);
            }
            return taxAppInstallPath;
        }


        /// <summary>
        /// 通过注册表获取安装目录
        /// </summary>
        /// <param name="regKeyPath">The reg key path.</param>
        /// <param name="regKeyName">Name of the reg key.</param>
        /// <returns></returns>
        public static string GetAppInstallPathByReg(string regKeyPath, string regKeyName)
        {
            string taxAppInstallPath = string.Empty;
            if (string.IsNullOrEmpty(regKeyPath))
            {
                return taxAppInstallPath;
            }
            try
            {
                string keyValue = RegistryHelper.GetValue(regKeyPath, regKeyName);
                taxAppInstallPath = keyValue;
            }
            catch (Exception exception)
            {
                Log.Error("安装目录出现错误:" + exception.Message);
            }
            return taxAppInstallPath;
        }




        /// <summary>
        /// 通过注册表,通过快捷方式获取安装目录
        /// </summary>
        /// <param name="regKeyPath">注册表目录</param>
        /// <param name="regKeyName">键</param>
        /// <param name="linkName">快捷方式名称</param>
        /// <returns></returns>
        public static string GetTaxAppInstallPath(string regKeyPath, string regKeyName, string linkName)
        {
            string taxAppInstallPath = string.Empty;
            if (!string.IsNullOrEmpty(regKeyPath) && !string.IsNullOrEmpty(regKeyName))
            {
                taxAppInstallPath = GetAppInstallPathByReg(regKeyPath, regKeyName);
            }
            if (string.IsNullOrEmpty(taxAppInstallPath))
            {
                taxAppInstallPath = GetAppInstallPathByLink(linkName);
            }
            if (taxAppInstallPath.EndsWith(".exe", StringComparison.Ordinal))
            {
                taxAppInstallPath = taxAppInstallPath.Substring(0, taxAppInstallPath.LastIndexOf("\\", StringComparison.Ordinal) + 1);
            }
            return taxAppInstallPath;
        }




        /// <summary>
        /// 通过快捷方式获取目录
        /// </summary>
        /// <param name="programName">Name of the program.</param>
        /// <param name="linkName">快捷方式名称</param>
        /// <param name="isContain">是否包含文件名称</param>
        /// <returns></returns>
        public static string GetAppInstallPathByProgram(string programName, string linkName, bool isContain = false)
        {
            string taxAppInstallPath = string.Empty;
            try
            {
                string currentProgramFolderPathPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
                if (string.IsNullOrEmpty(taxAppInstallPath))
                {
                    string lnkPath = Path.Combine(currentProgramFolderPathPath, programName + "/" + linkName);
                    if (System.IO.File.Exists(lnkPath))
                    {
                        taxAppInstallPath = isContain ? GetAppinstallFileByShortcut(lnkPath) : GetAppinstallPathByShortcut(lnkPath);
                    }
                }
            }
            catch
            {
                taxAppInstallPath = string.Empty;
            }
            return taxAppInstallPath;
        }




        /// <summary>
        /// 获取USBShare Manager安装目录
        /// </summary>
        /// <returns></returns>
        public static string GetTaxAppInstallPathUsbShare()
        {
            string taxAppInstallPath = GetAppInstallPathByReg(@"SOFTWARE\MBInfoSofts\UsbsDevM.exe", "");
            if (string.IsNullOrEmpty(taxAppInstallPath))
            {
                taxAppInstallPath = GetTaxAppInstallPath(@"SOFTWARE\WOW6432Node\MBInfoSofts\UsbsDevM.exe", "", "USBShare Manager.lnk");
            }
            if (string.IsNullOrEmpty(taxAppInstallPath))
            {
                taxAppInstallPath = GetAppInstallPathByProgram("USBShare Manager", "USBShare Manager.lnk");
            }
            return taxAppInstallPath;
        }


        /// <summary>
        /// 获取USBShare服务安装目录
        /// </summary>
        /// <returns></returns>
        public static string GetTaxAppInstallPathUsbService()
        {
            string taxAppInstallPath = GetAppInstallPathByReg(@"SOFTWARE\MBInfoSofts\usbshsrv.exe", "");
            if (string.IsNullOrEmpty(taxAppInstallPath))
            {
                taxAppInstallPath = GetAppInstallPathByReg(@"SOFTWARE\WOW6432Node\MBInfoSofts\usbshsrv.exe", "");
            }
            return taxAppInstallPath;
        }
    }

猜你喜欢

转载自blog.csdn.net/vs920079469vs/article/details/79262740