我正在使用Process.Start(url)在默认Web浏览器中启动URL,然后我计划使用Process.Kill()关闭它。
问题是找到默认浏览器以了解要杀死的进程。建议?
答案 0 :(得分:1)
取自:Opening default web browser
Private Function getDefaultBrowser() As String
Dim browser As String = String.Empty
Dim key As RegistryKey = Nothing
Try
key = Registry.ClassesRoot.OpenSubKey("HTTP\shell\open\command", False)
'trim off quotes
browser = key.GetValue(Nothing).ToString().ToLower().Replace("""", "")
If Not browser.EndsWith("exe") Then
'get rid of everything after the ".exe"
browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4)
End If
Finally
If key IsNot Nothing Then
key.Close()
End If
End Try
Return browser
End Function
在那里你可以获得默认浏览器。然后,您可以遍历正在运行的进程并终止浏览器。
Dim browser As String
browser = getDefaultBrowser()
For Each p As Process In Process.GetProcesses
If p.ProcessName = browser Then
p.Kill()
Exit For
End If
Next