Skip to content

Commit

Permalink
More comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jjxtra committed Apr 16, 2020
1 parent a7a085a commit 9a82e91
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions IPBanCore/Core/Utility/OSUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 9a82e91

Please sign in to comment.