C# 擷取程式安裝目錄

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   檔案   for   

在網頁啟動本地程式需要將命令寫入註冊表,在網頁調用命令即可。

 首先將註冊資訊建立一個註冊表檔案 .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();                                }                            }                        }                    }                }            }        }

 

最後,我是向大家請教問題的:怎麼擷取某個應用程式的安裝路徑,只通過程式名找?

 

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.