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.
You could try to determine the window handle(s) using the Process
class and send a WM_CLOSE
message to the window.
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();
}
}
}