diff --git a/dotnet/src/webdriver/Internal/ResourceUtilities.cs b/dotnet/src/webdriver/Internal/ResourceUtilities.cs index 6449d17b5385e..3e756b91233f2 100644 --- a/dotnet/src/webdriver/Internal/ResourceUtilities.cs +++ b/dotnet/src/webdriver/Internal/ResourceUtilities.cs @@ -17,8 +17,9 @@ // under the License. // +#if NET462 using System.Diagnostics; -using System.Globalization; +#endif using System.Reflection; using System.Runtime.InteropServices; @@ -29,88 +30,24 @@ namespace OpenQA.Selenium.Internal; /// internal static partial class ResourceUtilities { - private static string? productVersion; - private static string? platformFamily; - - /// - /// Gets a string representing the informational version of the Selenium product. - /// - public static string ProductVersion + private static readonly Lazy _productVersion = new(() => { - get - { - if (productVersion == null) - { - Assembly executingAssembly = Assembly.GetExecutingAssembly(); - var assemblyInformationalVersionAttribute = executingAssembly.GetCustomAttribute(); - if (assemblyInformationalVersionAttribute == null) - { - productVersion = "Unknown"; - } - else - { - productVersion = assemblyInformationalVersionAttribute.InformationalVersion; - } - } + Assembly executingAssembly = Assembly.GetExecutingAssembly(); + var assemblyInformationalVersionAttribute = executingAssembly.GetCustomAttribute(); + return assemblyInformationalVersionAttribute?.InformationalVersion ?? "Unknown"; + }); - return productVersion; - } - } + private static readonly Lazy _platformFamily = new(GetPlatformString); /// - /// Gets a string representing the platform family on which the Selenium assembly is executing. + /// Gets a string representing the informational version of the Selenium product. /// - public static string PlatformFamily => platformFamily ??= GetPlatformString(); + public static string ProductVersion => _productVersion.Value; /// - /// Gets a that contains the resource to use. + /// Gets a string representing the platform family on which the Selenium assembly is executing. /// - /// A file name in the file system containing the resource to use. - /// A string representing the resource name embedded in the - /// executing assembly, if it is not found in the file system. - /// A Stream from which the resource can be read. - /// Thrown if neither the file nor the embedded resource can be found. - /// - /// The GetResourceStream method searches for the specified resource using the following - /// algorithm: - /// - /// - /// In the same directory as the calling assembly. - /// In the full path specified by the argument. - /// Inside the calling assembly as an embedded resource. - /// - /// - /// - public static Stream GetResourceStream(string fileName, string resourceId) - { - Stream? resourceStream; - string resourceFilePath = Path.Combine(FileUtilities.GetCurrentDirectory(), Path.GetFileName(fileName)); - if (File.Exists(resourceFilePath)) - { - resourceStream = new FileStream(resourceFilePath, FileMode.Open, FileAccess.Read); - } - else if (File.Exists(fileName)) - { - resourceStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); - } - else - { - if (string.IsNullOrEmpty(resourceId)) - { - throw new WebDriverException("The file specified does not exist, and you have specified no internal resource ID"); - } - - Assembly executingAssembly = Assembly.GetExecutingAssembly(); - resourceStream = executingAssembly.GetManifestResourceStream(resourceId); - } - - if (resourceStream == null) - { - throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, "Cannot find a file named '{0}' or an embedded resource with the id '{1}'.", resourceFilePath, resourceId)); - } - - return resourceStream; - } + public static string PlatformFamily => _platformFamily.Value; private static string GetPlatformString() {