標籤: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(); } } } } } } }
最後,我是向大家請教問題的:怎麼擷取某個應用程式的安裝路徑,只通過程式名找?