How can i safely close google chrome via C#? I can kill chrome process, but in this case Google Chrome will report appcrash on next run.
close(); being executed? If you are developing a chrome extension then you can use the chrome API to close a specific tab which will should work in every case. You can also redirect to some other site or a random URL and have a script close that.
Click the “≡” button in the upper right corner of the Chrome browser window. Select the Exit button.
Open cmd with "Run as Administrator." option (Use right-click to get the menu). Use the command tasklist to list all processes. Use the command taskkill /F /IM "chrome.exe" /T to terminate all its processes.
You could use the little known UI Automation API, like this:
static void CloseAllChromeBrowsers()
{
foreach (Process process in Process.GetProcessesByName("chrome"))
{
if (process.MainWindowHandle == IntPtr.Zero) // some have no UI
continue;
AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
if (element != null)
{
((WindowPattern)element.GetCurrentPattern(WindowPattern.Pattern)).Close();
}
}
}
You could try to determine the window handle(s) using the Process
class and send a WM_CLOSE
message to the window.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With