diff --git a/docs/CodeDoc/Atc/Atc.Helpers.md b/docs/CodeDoc/Atc/Atc.Helpers.md index 8b367f57..f5914457 100644 --- a/docs/CodeDoc/Atc/Atc.Helpers.md +++ b/docs/CodeDoc/Atc/Atc.Helpers.md @@ -1195,6 +1195,114 @@ FileHelper. ### Static Methods +#### CloseMainWindowOnAllRunningInternetBrowsers +>```csharp +>void CloseMainWindowOnAllRunningInternetBrowsers() +>``` +#### CloseMainWindowOnRunningBraveInstances +>```csharp +>void CloseMainWindowOnRunningBraveInstances() +>``` +#### CloseMainWindowOnRunningFirefoxInstances +>```csharp +>void CloseMainWindowOnRunningFirefoxInstances() +>``` +#### CloseMainWindowOnRunningGhostBrowserInstances +>```csharp +>void CloseMainWindowOnRunningGhostBrowserInstances() +>``` +#### CloseMainWindowOnRunningGoogleChromeInstances +>```csharp +>void CloseMainWindowOnRunningGoogleChromeInstances() +>``` +#### CloseMainWindowOnRunningMicrosoftEdgeInstances +>```csharp +>void CloseMainWindowOnRunningMicrosoftEdgeInstances() +>``` +#### CloseMainWindowOnRunningMicrosoftInternetExplorerInstances +>```csharp +>void CloseMainWindowOnRunningMicrosoftInternetExplorerInstances() +>``` +#### CloseMainWindowOnRunningOperaInstances +>```csharp +>void CloseMainWindowOnRunningOperaInstances() +>``` +#### CloseMainWindowOnRunningSafariInstances +>```csharp +>void CloseMainWindowOnRunningSafariInstances() +>``` +#### GetRunningInternetBrowsers +>```csharp +>IList GetRunningInternetBrowsers() +>``` +#### IsBraveRunning +>```csharp +>bool IsBraveRunning() +>``` +#### IsFirefoxRunning +>```csharp +>bool IsFirefoxRunning() +>``` +#### IsGhostBrowserRunning +>```csharp +>bool IsGhostBrowserRunning() +>``` +#### IsGoogleChromeRunning +>```csharp +>bool IsGoogleChromeRunning() +>``` +#### IsMicrosoftEdgeRunning +>```csharp +>bool IsMicrosoftEdgeRunning() +>``` +#### IsMicrosoftInternetExplorerRunning +>```csharp +>bool IsMicrosoftInternetExplorerRunning() +>``` +#### IsOperaRunning +>```csharp +>bool IsOperaRunning() +>``` +#### IsSafariRunning +>```csharp +>bool IsSafariRunning() +>``` +#### KillAllRunningInternetBrowsers +>```csharp +>void KillAllRunningInternetBrowsers() +>``` +#### KillRunningBraveInstances +>```csharp +>void KillRunningBraveInstances() +>``` +#### KillRunningFirefoxInstances +>```csharp +>void KillRunningFirefoxInstances() +>``` +#### KillRunningGhostBrowserInstances +>```csharp +>void KillRunningGhostBrowserInstances() +>``` +#### KillRunningGoogleChromeInstances +>```csharp +>void KillRunningGoogleChromeInstances() +>``` +#### KillRunningMicrosoftEdgeInstances +>```csharp +>void KillRunningMicrosoftEdgeInstances() +>``` +#### KillRunningMicrosoftInternetExplorerInstances +>```csharp +>void KillRunningMicrosoftInternetExplorerInstances() +>``` +#### KillRunningOperaInstances +>```csharp +>void KillRunningOperaInstances() +>``` +#### KillRunningSafariInstances +>```csharp +>void KillRunningSafariInstances() +>``` #### OpenUrl >```csharp >bool OpenUrl(string url) @@ -1204,6 +1312,8 @@ FileHelper. >Parameters:
>     `url`  -  The URL.
> +>Returns: `true` if the url is started in a browser; otherwise, `false`. +> >Remarks: Only url with the http or https protocol is supported. #### OpenUrl >```csharp @@ -1214,6 +1324,8 @@ FileHelper. >Parameters:
>     `url`  -  The URL.
> +>Returns: `true` if the url is started in a browser; otherwise, `false`. +> >Remarks: Only url with the http or https protocol is supported.
diff --git a/docs/CodeDoc/Atc/IndexExtended.md b/docs/CodeDoc/Atc/IndexExtended.md index 50069d26..de075ef2 100644 --- a/docs/CodeDoc/Atc/IndexExtended.md +++ b/docs/CodeDoc/Atc/IndexExtended.md @@ -4528,6 +4528,33 @@ - WriteModelToJsonFileAsync(FileInfo fileInfo, T model) - [InternetBrowserHelper](Atc.Helpers.md#internetbrowserhelper) - Static Methods + - CloseMainWindowOnAllRunningInternetBrowsers() + - CloseMainWindowOnRunningBraveInstances() + - CloseMainWindowOnRunningFirefoxInstances() + - CloseMainWindowOnRunningGhostBrowserInstances() + - CloseMainWindowOnRunningGoogleChromeInstances() + - CloseMainWindowOnRunningMicrosoftEdgeInstances() + - CloseMainWindowOnRunningMicrosoftInternetExplorerInstances() + - CloseMainWindowOnRunningOperaInstances() + - CloseMainWindowOnRunningSafariInstances() + - GetRunningInternetBrowsers() + - IsBraveRunning() + - IsFirefoxRunning() + - IsGhostBrowserRunning() + - IsGoogleChromeRunning() + - IsMicrosoftEdgeRunning() + - IsMicrosoftInternetExplorerRunning() + - IsOperaRunning() + - IsSafariRunning() + - KillAllRunningInternetBrowsers() + - KillRunningBraveInstances() + - KillRunningFirefoxInstances() + - KillRunningGhostBrowserInstances() + - KillRunningGoogleChromeInstances() + - KillRunningMicrosoftEdgeInstances() + - KillRunningMicrosoftInternetExplorerInstances() + - KillRunningOperaInstances() + - KillRunningSafariInstances() - OpenUrl(Uri uri) - OpenUrl(string url) - [MathHelper](Atc.Helpers.md#mathhelper) diff --git a/src/Atc/Helpers/InternetBrowserHelper.cs b/src/Atc/Helpers/InternetBrowserHelper.cs index ea88c8b4..72e7a832 100644 --- a/src/Atc/Helpers/InternetBrowserHelper.cs +++ b/src/Atc/Helpers/InternetBrowserHelper.cs @@ -2,6 +2,158 @@ namespace Atc.Helpers; public static class InternetBrowserHelper { + private const string ProcessNameBrave = "brave"; + private const string ProcessNameFirefox = "firefox"; + private const string ProcessNameGhostBrowser = "ghost"; + private const string ProcessNameChrome = "chrome"; + private const string ProcessNameMicrosoftEdge = "msedge"; + private const string ProcessNameMicrosoftInternetExplorer = "iexplore"; + private const string ProcessNameOpera = "opera"; + private const string ProcessNameSafari = "safari"; + + public static IList GetRunningInternetBrowsers() + { + var result = new List(); + + if (IsBraveRunning()) + { + result.Add("Brave"); + } + + if (IsFirefoxRunning()) + { + result.Add("Firefox"); + } + + if (IsGhostBrowserRunning()) + { + result.Add("Ghost Browser"); + } + + if (IsGoogleChromeRunning()) + { + result.Add("Google Chrome"); + } + + if (IsMicrosoftEdgeRunning()) + { + result.Add("Microsoft Edge"); + } + + if (IsMicrosoftInternetExplorerRunning()) + { + result.Add("Microsoft Internet Explorer"); + } + + if (IsOperaRunning()) + { + result.Add("Opera"); + } + + if (IsSafariRunning()) + { + result.Add("Safari"); + } + + return result; + } + + public static void CloseMainWindowOnAllRunningInternetBrowsers() + { + CloseMainWindowOnRunningBraveInstances(); + CloseMainWindowOnRunningFirefoxInstances(); + CloseMainWindowOnRunningGhostBrowserInstances(); + CloseMainWindowOnRunningGoogleChromeInstances(); + CloseMainWindowOnRunningMicrosoftEdgeInstances(); + CloseMainWindowOnRunningMicrosoftInternetExplorerInstances(); + CloseMainWindowOnRunningOperaInstances(); + CloseMainWindowOnRunningSafariInstances(); + } + + public static void KillAllRunningInternetBrowsers() + { + KillRunningBraveInstances(); + KillRunningFirefoxInstances(); + KillRunningGhostBrowserInstances(); + KillRunningGoogleChromeInstances(); + KillRunningMicrosoftEdgeInstances(); + KillRunningMicrosoftInternetExplorerInstances(); + KillRunningOperaInstances(); + KillRunningSafariInstances(); + } + + public static bool IsBraveRunning() + => IsProcessRunning(ProcessNameBrave); + + public static bool IsFirefoxRunning() + => IsProcessRunning(ProcessNameFirefox); + + public static bool IsGhostBrowserRunning() + => IsProcessRunning(ProcessNameGhostBrowser); + + public static bool IsGoogleChromeRunning() + => IsProcessRunning(ProcessNameChrome); + + public static bool IsMicrosoftEdgeRunning() + => IsProcessRunning(ProcessNameMicrosoftEdge); + + public static bool IsMicrosoftInternetExplorerRunning() + => IsProcessRunning(ProcessNameMicrosoftInternetExplorer); + + public static bool IsOperaRunning() + => IsProcessRunning(ProcessNameOpera); + + public static bool IsSafariRunning() + => IsProcessRunning(ProcessNameSafari); + + public static void CloseMainWindowOnRunningBraveInstances() + => CloseMainWindowOnRunningInstances(ProcessNameBrave); + + public static void CloseMainWindowOnRunningFirefoxInstances() + => CloseMainWindowOnRunningInstances(ProcessNameFirefox); + + public static void CloseMainWindowOnRunningGhostBrowserInstances() + => CloseMainWindowOnRunningInstances(ProcessNameGhostBrowser); + + public static void CloseMainWindowOnRunningGoogleChromeInstances() + => CloseMainWindowOnRunningInstances(ProcessNameChrome); + + public static void CloseMainWindowOnRunningMicrosoftEdgeInstances() + => CloseMainWindowOnRunningInstances(ProcessNameMicrosoftEdge); + + public static void CloseMainWindowOnRunningMicrosoftInternetExplorerInstances() + => CloseMainWindowOnRunningInstances(ProcessNameMicrosoftInternetExplorer); + + public static void CloseMainWindowOnRunningOperaInstances() + => CloseMainWindowOnRunningInstances(ProcessNameOpera); + + public static void CloseMainWindowOnRunningSafariInstances() + => CloseMainWindowOnRunningInstances(ProcessNameSafari); + + public static void KillRunningBraveInstances() + => KillRunningInstances(ProcessNameBrave); + + public static void KillRunningFirefoxInstances() + => KillRunningInstances(ProcessNameFirefox); + + public static void KillRunningGhostBrowserInstances() + => KillRunningInstances(ProcessNameGhostBrowser); + + public static void KillRunningGoogleChromeInstances() + => KillRunningInstances(ProcessNameChrome); + + public static void KillRunningMicrosoftEdgeInstances() + => KillRunningInstances(ProcessNameMicrosoftEdge); + + public static void KillRunningMicrosoftInternetExplorerInstances() + => KillRunningInstances(ProcessNameMicrosoftInternetExplorer); + + public static void KillRunningOperaInstances() + => KillRunningInstances(ProcessNameOpera); + + public static void KillRunningSafariInstances() + => KillRunningInstances(ProcessNameSafari); + /// /// Open the given url in the default browser on the machine. /// @@ -9,7 +161,11 @@ public static class InternetBrowserHelper /// /// Only url with the http or https protocol is supported. /// - public static bool OpenUrl(string url) + /// + /// true if the url is started in a browser; otherwise, false. + /// + public static bool OpenUrl( + string url) { if (url is null) { @@ -26,7 +182,11 @@ public static bool OpenUrl(string url) /// /// Only url with the http or https protocol is supported. /// - public static bool OpenUrl(Uri uri) + /// + /// true if the url is started in a browser; otherwise, false. + /// + public static bool OpenUrl( + Uri uri) { if (uri is null) { @@ -37,7 +197,8 @@ public static bool OpenUrl(Uri uri) } [SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "OK.")] - private static bool ProcessStartOpenUrl(Uri uri) + private static bool ProcessStartOpenUrl( + Uri uri) { if (uri is null) { @@ -76,4 +237,28 @@ private static bool ProcessStartOpenUrl(Uri uri) return false; } } + + private static bool IsProcessRunning( + string processName) + => Process.GetProcessesByName(processName).Length > 0; + + private static void CloseMainWindowOnRunningInstances( + string processName) + { + var instances = Process.GetProcessesByName(processName); + foreach (var instance in instances) + { + instance.CloseMainWindow(); + } + } + + private static void KillRunningInstances( + string processName) + { + var instances = Process.GetProcessesByName(processName); + foreach (var instance in instances) + { + instance.Kill(); + } + } } \ No newline at end of file