首页 > 学院 > 编程应用 > 正文

C# 获取程序安装目录

2019-11-17 02:58:38
字体:
来源:转载
供稿:网友

C# 获取程序安装目录

在网页启动本地程序需要将命令写入注册表,在网页调用命令即可。

首先将注册信息创建一个注册表文件 .reg 格式,以页面启动notepad++ 程序为例
Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT/Webshell][HKEY_CLASSES_ROOT/Webshell/DefaultIcon][HKEY_CLASSES_ROOT/Webshell/shell][HKEY_CLASSES_ROOT/Webshell/shell/open][HKEY_CLASSES_ROOT/Webshell/shell/open/command]@="/"C://PRogram Files (x86)//Notepad++//notepad++.exe/" /"%1/""

在页面调用HTML代码,会有一个外部请求的提示,直接启动应用即可。
<a href='Webshell://'>WebShell启动本地程序</a>    
--------------------------------------分割线-----------------------------------------在项目中,注册脚本不会让用户自己注册,那就需要将注册信息在程序中执行,用C#代码实现。
    private static void Main(string[] args)        {       var notepadPath = @"C:/Program Files (x86)/Notepad++/notepad++.exe";           CreateRegistryKey("Webshell", notepadPath);                       }
        public static void CreateRegistryKey(string shell, string path)        {            RegistryKey key = Registry.ClassesRoot;            key.CreateSubKey(shell);            key.CreateSubKey(string.Format(@"{0}/DefaultIcon", shell));            key.CreateSubKey(string.Format(@"{0}/shell", shell));            key.CreateSubKey(string.Format(@"{0}/shell/open", shell));            var command = key.CreateSubKey(string.Format(@"{0}/shell/open/command", shell));            command.SetValue("", path);            key.Close();        }

在真实的用户环境下,是不能确定某个程序安装在了哪里,所以程序的安装目录不能用固定的。

百度一下,找到一个方法。有的程序是可以找到,但有些程序就找不到了。不知道为什么?

        /// <summary>        /// 获取单个程序的执行目录        /// </summary>        /// <param name="processName"></param>        /// <returns></returns>        public static string GetPath(string processName)        {            var process = Process.GetProcessesByName(processName);            var path = string.Empty;//程序路径            foreach (var p in process.Where(p => p.MainWindowHandle != IntPtr.Zero))            {                path = p.MainModule.FileName;                break;            }            return path;        }
        private static void Main(string[] args)        {         var notepadPath = GetPath("Notepad++");               Console.WriteLine(" 程序名称:Notepad++ /n 安装目录:" + notepadPath);            CreateRegistryKey("Webshell", notepadPath);            }

又百度一下,找到获取所有程序的安装目录方法,只是获取的安装路径,不是完整可用路径。

        /// <summary>        /// 获取所有程序的安装目录        /// </summary>        public static void GetAllProcess()        {            const string Uninstall = @"SOFTWARE/Microsoft/Windows/CurrentVersion/Uninstall";            using (var registryKey = Registry.LocalMachine.OpenSubKey(Uninstall, false))            {                if (registryKey != null)//判断对象存在                {                    foreach (var keyName in registryKey.GetSubKeyNames())//遍历子项名称的字符串数组                    {                        using (var key = registryKey.OpenSubKey(keyName, false))//遍历子项节点                        {                            if (key != null)                            {                                var softwareName = key.GetValue("DisplayName", "").ToString();//获取软件名                                var installLocation = key.GetValue("InstallLocation", "").ToString();//获取安装路径                                if (!string.IsNullOrEmpty(installLocation))                                {                                    Console.WriteLine(softwareName);                                    Console.WriteLine(installLocation);                                    Console.WriteLine();                                }                            }                        }                    }                }            }        }

还是有些应用程序未找到!

--------------------------------------分割线-----------------------------------------

WinFrom小程序:手动指向exe可执行程序

下载:注册安装程序


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表