国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 編程應用 > 正文

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可執行程序

下載:注冊安裝程序


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 建始县| 黔江区| 濉溪县| 正蓝旗| 两当县| 长宁区| 六盘水市| 平潭县| 碌曲县| 新化县| 财经| 柘荣县| 蓬莱市| 平远县| 项城市| 揭阳市| 乌拉特后旗| 丹巴县| 分宜县| 新巴尔虎右旗| 类乌齐县| 略阳县| 彭山县| 余庆县| 宁武县| 博罗县| 蓝田县| 轮台县| 同江市| 水富县| 曲靖市| 来凤县| 丹巴县| 临沂市| 墨脱县| 霸州市| 珠海市| 囊谦县| 新竹市| 闻喜县| 抚顺县|