diff --git a/IPBanCore/Core/Utility/OSUtility.cs b/IPBanCore/Core/Utility/OSUtility.cs index 08850831..d7d1679c 100644 --- a/IPBanCore/Core/Utility/OSUtility.cs +++ b/IPBanCore/Core/Utility/OSUtility.cs @@ -157,30 +157,44 @@ private static void LoadVersionFromWmic() // attempt to run wmic to generate the info we want StartProcessAndWait(5000, "cmd", "/C wmic path Win32_OperatingSystem get Caption,Version /format:table > \"" + tempFile + "\""); - // try up to 10 times to read the file - for (int i = 0; i < 10; i++) + // try up to 10 times to read the file in case file is still in use + try { - try + for (int i = 0; i < 10; i++) { - string[] lines = File.ReadAllLines(tempFile); - ExtensionMethods.FileDeleteWithRetry(tempFile); - if (lines.Length > 1) + try { - int versionIndex = lines[0].IndexOf("Version"); - if (versionIndex >= 0) + // if this throws, we will try again + string[] lines = File.ReadAllLines(tempFile); + + // if we have enough lines, try to parse them out + if (lines.Length > 1) { - FriendlyName = lines[1].Substring(0, versionIndex - 1).Trim(); - Version = lines[1].Substring(versionIndex).Trim(); - return; + int versionIndex = lines[0].IndexOf("Version"); + if (versionIndex >= 0) + { + FriendlyName = lines[1].Substring(0, versionIndex - 1).Trim(); + Version = lines[1].Substring(versionIndex).Trim(); + return; + } } + + throw new InvalidDataException("Invalid file generated by wmic"); } - throw new IOException("Invalid file generated by wmic"); - } - catch (Exception ex) - { - lastError = ex; + catch (Exception ex) + { + lastError = ex; + if (ex is InvalidDataException) + { + break; + } + } + Thread.Sleep(200); } - Thread.Sleep(200); + } + finally + { + ExtensionMethods.FileDeleteWithRetry(tempFile); } throw new ApplicationException("Unable to load os version using wmic", lastError); }