|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Reflection; |
| 4 | + |
| 5 | +namespace EasyPost |
| 6 | +{ |
| 7 | + public static class RuntimeInfo |
| 8 | + { |
| 9 | + internal struct ApplicationInfo |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// Get the version of the application as a string. |
| 13 | + /// </summary> |
| 14 | + /// <returns>The version of the application as a string.</returns> |
| 15 | + internal static string ApplicationVersion |
| 16 | + { |
| 17 | + get |
| 18 | + { |
| 19 | + try |
| 20 | + { |
| 21 | + Assembly assembly = typeof(ApplicationInfo).Assembly; |
| 22 | + FileVersionInfo info = FileVersionInfo.GetVersionInfo(assembly.Location); |
| 23 | + return info.FileVersion ?? "Unknown"; |
| 24 | + } |
| 25 | + catch (Exception) |
| 26 | + { |
| 27 | + return "Unknown"; |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Get the .NET framework version as a string. |
| 34 | + /// </summary> |
| 35 | + /// <returns>The .NET framework version as a string.</returns> |
| 36 | + internal static string DotNetVersion |
| 37 | + { |
| 38 | + get |
| 39 | + { |
| 40 | + Version dotNetVersion = Environment.Version; |
| 41 | + if (dotNetVersion == null) |
| 42 | + { |
| 43 | + /* |
| 44 | + * We're on a pre-4.0 .NET Framework version, where Environment.Version is null. |
| 45 | + */ |
| 46 | + return "3.5-"; |
| 47 | + } |
| 48 | + |
| 49 | + string versionString = dotNetVersion.ToString(); |
| 50 | + if (versionString == "4.0.30319.42000") |
| 51 | + { |
| 52 | + /* |
| 53 | + * We're on a v4.6+ version (or pre-.NET Core 3.0, which we don't support), |
| 54 | + * but we can't get the exact version. |
| 55 | + * See: https://docs.microsoft.com/en-us/dotnet/api/system.environment.version?view=net-6.0#remarks |
| 56 | + */ |
| 57 | + versionString = "4.6+"; |
| 58 | + } |
| 59 | + |
| 60 | + return versionString; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + internal struct OperationSystemInfo |
| 66 | + { |
| 67 | + /// <summary> |
| 68 | + /// Get details about the operating system. |
| 69 | + /// </summary> |
| 70 | + /// <returns>Details about the operating system.</returns> |
| 71 | + private static OperatingSystem OperatingSystem |
| 72 | + { |
| 73 | + get { return Environment.OSVersion; } |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Get the name of the operating system. |
| 78 | + /// </summary> |
| 79 | + /// <returns>Name of the operating system.</returns> |
| 80 | + internal static string Name |
| 81 | + { |
| 82 | + get |
| 83 | + { |
| 84 | + switch (OperatingSystem.Platform) |
| 85 | + { |
| 86 | + case PlatformID.Win32S: |
| 87 | + case PlatformID.Win32Windows: |
| 88 | + case PlatformID.Win32NT: |
| 89 | + case PlatformID.WinCE: |
| 90 | + return "Windows"; |
| 91 | + case PlatformID.Unix: |
| 92 | + return "Linux"; |
| 93 | + case PlatformID.MacOSX: // in newer versions, Mac OS X is PlatformID.Unix unfortunately |
| 94 | + return "Darwin"; |
| 95 | + default: |
| 96 | + return "Unknown"; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Get the version of the operating system. |
| 103 | + /// </summary> |
| 104 | + /// <returns>Version of the operating system.</returns> |
| 105 | + internal static string Version |
| 106 | + { |
| 107 | + get { return OperatingSystem.Version.ToString(); } |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Get the architecture of the operating system. |
| 112 | + /// </summary> |
| 113 | + /// <returns>Architecture of the operating system.</returns> |
| 114 | + internal static string Architecture |
| 115 | + { |
| 116 | + get |
| 117 | + { |
| 118 | + // Because of how this library uses parent files, we can't easily target different frameworks and use different functions to get the architecture. |
| 119 | + return "Unknown"; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments