I’ve created a WinForms application with a button. When a user presses that button I want my application to detect if a web browser is open such as Chrome, IE or Firefox and if it is, I want a message to show, like in
Message.Show("Browser Was Detected As Open",
"Success",
MessageBoxButtons.OK,
MessageBoxIcon.None);
And for an “else if” browser isn’t detected open an Error Box saying “Please Open Browser”.
I’m new to C# it’s probably a pretty simple few lines of Code.
If anyone can answer this thank you so much! Please include the #using statements!
Since you wrote you're using WinForms, I'm assuming you're on Windows. AFAIK there are 2 methods to do this - using the
Processclass or using WinAPI.I'm not gonna cover the WinAPI variant, because it's longer and more complicated, also I don't really use it (most tasks can be accomplished using pure C#).
You can call this method in your
button.Clickevent handler:BrowserDetector class:
Notes
I'm not sure whether all browsers have their name in the window title, I just checked those that I have available.
For adding other browsers you would need to find the process name in Task Manager, Details tab - e.g. for Firefox the entry is "firefox.exe", so the process name is "firefox". If there are different process names, like "MicrosoftEdge" and "MicrosoftEdgeCP", you're gonna need to experiment a bit to figure out which process has the main window.