I am using Process.Start(url) to launch a URL in the default web browser and then I plan to close it using Process.Kill().
The problem is finding the default browser to know which process to kill. Suggestions?
Taken from: 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
There you can get the default browser. Then you can loop through the running process and kill the browser.
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
Related
I'm unable to perform click by cssselector or even xpath or even send keys space or enter.
I think youtube is blocking it. Any help appreciate.
My Code:
Public Function OpenVideo(video As String)
Try
Form1.UpdateStatus("Opening video ...")
If (Driver Is Nothing) Then
Return ""
End If
_Status = True
Driver.Navigate().GoToUrl(video)
'Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5)
'TESTING
' Driver.SwitchTo.Frame(0)
Dim element = Driver.FindElementByCssSelector("#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-left-controls > span:nth-child(2) > button[aria-label='Play (k)'")
element.click()
ViewsByVideo = 0
d = DateTime.Now
If (clockTimer Is Nothing) Then
Dim autoEvent As New AutoResetEvent(False)
clockTimer = New Timer(AddressOf ClockFunction, autoEvent, 0, 1000)
Else
clockTimer.Change(0, 1000)
End If
IsPlaying = True
Catch ex As Exception
LogManager.AddLog(ex.Message)
End Try
_Status = False
End Function
If possible I would like to instead of clicking elements or button, just "HIT SPACE" and this way I can be sure it always press play as space is the same like play.
Thanks
google blocks automated traffic
try using cssSelector at final step.
if that didn't work then might be there is DOM issue with page or automated script is blocked.
I made a program that allows a user to login with a WebBrowser control to our CRM system (ConnectWise)
The cookies would then be moved a Web Client control, where it would parse a url which came back with a .csv file. The Webclient would then download the csv to "C:\Temp\unassigned.csv" and I would then use this file to do other things in the program.
This worked great for months, until there was an update to Connectwise which brought in the use of HTTPOnly Cookies, of which I cannot extract the cookies from the Web Browser Control. The WebBrowser1.Document.Cookie string is now blank.
Here is the code:
Private Sub Internetr()
If WebBrowser1.Url.ToString.StartsWith("REDACTED") Then 'if connectwise is logged in
Dim _url As String = "REDACTED"
Dim _filename As String = ("C:\Temp\unassigned.csv")
WebBrowser1.Visible = False 'Hide the webbrowser
Refresh.Enabled = True 'Enable the ticker
Dim csv As WebClient = New WebClient 'new download webclient
'THIS IS WHERE I MOVED THE COOKIES FROM WEB BROWSER TO WEBCLIENT BEFORE
Try
csv.DownloadFile(_url, _filename)
load.Text = "Updating List..."
csv.Dispose()
DoUnassigned()
Catch ex As Exception
load.Text = "Failed to update list, Last updated at: " & gettime
debugbtn.Visible = True
exception = ex.ToString
Failed()
End Try
Else
WebBrowser1.Visible = True 'if not logged in, show the webbrowser
End If
End Sub
Does anyone have any ideas please?
I am using the latest GeckoFX-45 Browser for vb.net.
My problem:
I can not get the HTML of the Iframe.
Here is my code:
'Open Recaptcha2 test site
Browser.Navigate("http://patrickhlauke.github.io/recaptcha/")
'Wait for loading (all ways I know)
Browser.NavigateFinishedNotifier.BlockUntilNavigationFinished()
Dim maxTimeouttime As DateTime = DateTime.Now.AddSeconds(4)
While DateTime.Now < maxTimeouttime
System.Windows.Forms.Application.DoEvents()
System.Threading.Thread.Sleep(100)
End While
While Browser.IsBusy()
System.Windows.Forms.Application.DoEvents()
System.Threading.Thread.Sleep(100)
End While
'Getting the HTML of the Iframe is always empty:
For Each _E As GeckoIFrameElement In Browser.DomDocument.GetElementsByTagName("iframe")
If _E.GetAttribute("title") = "recaptcha widget" Then
Dim html = _E.ContentDocument '-> is empty
Dim html2 = _E.OuterHtml '-> HTML does not include the content of the Iframe
Exit For
End If
Next
The site is displayed very well in the Browser and the recaptcha2 iframe is loaded completly and ready but how can I access it programatically?
Thank you very much
Use ContentWindow.Document instead of ContentDocument.
Your code shoud be:
Dim iFr_dom As GeckoDomDocument
Dim iFr_html As String
For Each iFr As GeckoIFrameElement In Browser.DomDocument.GetElementsByTagName("iframe")
If iFr.GetAttribute("title") = "recaptcha widget" Then
iFr_dom = iFr.ContentWindow.Document
'DomDocument has no OuterHtml property so we use
iFr_html = iFr_dom.GetElementsByTagName("BODY")(0).OuterHtml
'Or if the above not work, use the code below
'iFr_html = iFr_doc.GetElementsByTagName("body")(0).OuterHtml
Exit For
End If
Next
This code gets all software that has an Uninstall Entry and it app path. However, given I have the app path, I do not know the name of the main .exe of the software. What ways are there to find the main .exe of a found application?
'Declare the string to hold the list:
Dim Software As String = Nothing
'The registry key:
Dim SoftwareKey As String = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
Using rk As RegistryKey = Registry.LocalMachine.OpenSubKey(SoftwareKey)
'Let's go through the registry keys and get the info we need:
Dim j As Integer = 0
For Each skName As String In rk.GetSubKeyNames()
Using sk As RegistryKey = rk.OpenSubKey(skName)
Try
'If the key has value, continue, if not, skip it:
If Not (sk.GetValue("DisplayName") Is Nothing) And Not (sk.GetValue("InstallLocation") Is Nothing) And
Not (sk.GetValue("InstallLocation") = "") Then
Dim instanceremoved As Boolean = False
For Each item As ListViewItem In ListInstalled.Items
If item.SubItems.Item(1).Text = sk.GetValue("InstallLocation") Then
item.Remove()
instanceremoved = True
End If
Next
If instanceremoved = False Then
Dim itemAdd As ListViewItem = ListInstalled.Items.Add(sk.GetValue("DisplayName"))
itemAdd.SubItems.Add(sk.GetValue("InstallLocation"))
End If
End If
'No, that exception is not getting away... :P
Catch ex As Exception
MsgBox(ex)
End Try
End Using
j = j + 1
Next
End Using
The main Exe is not stored in the registry the uninstall command is stored in the UninstallString key.
I want to terminate an application using the full file path via vb.net,so i am using this code snippet
Public Sub forceCopy()
Try
'Dim strDatabasePath As String = My.Computer.FileSystem.CombinePath(Application.UserAppDataPath, "LIC.mdf")
'Dim strdbLogPath As String = My.Computer.FileSystem.CombinePath(Application.UserAppDataPath, "LIC_log.ldf")
Dim strDatabasePath As String = My.Computer.FileSystem.CombinePath(My.Application.Info.DirectoryPath, "LIC.mdf")
Dim strdbLogPath As String = My.Computer.FileSystem.CombinePath(My.Application.Info.DirectoryPath, "LIC_log.ldf")
Dim path As String = My.Computer.FileSystem.CombinePath(My.Application.Info.DirectoryPath, "LIC.mdf")
Dim matchingProcesses = New List(Of Process)
For Each process As Process In process.GetProcesses()
For Each m As ProcessModule In process.Modules
If String.Compare(m.FileName, path, StringComparison.InvariantCultureIgnoreCase) = 0 Then
matchingProcesses.Add(process)
Exit For
End If
Next
Next
For Each p As Process In matchingProcesses
p.Kill()
Next
My.Computer.FileSystem.CopyFile(strDatabasePath, "c:\backup\LIC.mdf", True)
My.Computer.FileSystem.CopyFile(strdbLogPath, "c:\backup\LIC_log.ldf", True)
MessageBox.Show("Backup taken successfully")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
I get an exception "Access Is Denied" .Any idea why?
EDIT: I get the error at this line : For Each m As ProcessModule In process.Modules
You'll need to wrap the If String.Compare(m.FileName, ...) block with Try/Catch. There are several fake and privileged processes whose FileName property you cannot access.
Killing SQL Server like this is otherwise a Really Bad Idea. Ask nicely with the ServiceController class. You will need UAC elevation to do so.
Only elevated processes can enumerate modules loaded by processes that you do not own.