在網頁啟動本地程序需要將命令寫入注冊表,在網頁調用命令即可。
首先將注冊信息創建一個注冊表文件 .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可執行程序

下載:注冊安裝程序
新聞熱點
疑難解答