Added even more helpers
This commit is contained in:
parent
20c8d62a21
commit
843563ae88
@ -70,7 +70,9 @@
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
</ApplicationDefinition>
|
||||
<Compile Include="Util\OsUpgradeDetection.cs" />
|
||||
<Compile Include="Util\OSVersionInfo.cs" />
|
||||
<Compile Include="Util\UEFIHelper.cs" />
|
||||
<Page Include="MainWindow.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
@ -102,7 +104,9 @@
|
||||
<Generator>ResXFileCodeGenerator</Generator>
|
||||
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
||||
</EmbeddedResource>
|
||||
<None Include="app.manifest" />
|
||||
<None Include="app.manifest">
|
||||
<SubType>Designer</SubType>
|
||||
</None>
|
||||
<None Include="packages.config" />
|
||||
<None Include="Properties\Settings.settings">
|
||||
<Generator>SettingsSingleFileGenerator</Generator>
|
||||
|
@ -19,6 +19,8 @@
|
||||
|
||||
<Grid Grid.Row="1">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
@ -39,6 +41,15 @@
|
||||
|
||||
<Label Grid.Row="2" Grid.Column="0">Windows architecture:</Label>
|
||||
<Label Grid.Row="2" Grid.Column="2" Content="{Binding Path=OsArchitecture}"/>
|
||||
|
||||
<Label Grid.Row="3" Grid.Column="0">Current boot mode:</Label>
|
||||
<Label Grid.Row="3" Grid.Column="2" Content="{Binding Path=BootMode}"/>
|
||||
|
||||
<Label Grid.Row="4" Grid.Column="0">Secure Boot state:</Label>
|
||||
<Label Grid.Row="4" Grid.Column="2" Content="{Binding Path=SecureBootEnabled}"/>
|
||||
|
||||
<Label Grid.Row="5" Grid.Column="0">OS upgrade status:</Label>
|
||||
<Label Grid.Row="5" Grid.Column="2" Content="{Binding Path=OsUpgradeStatus}"/>
|
||||
|
||||
</Grid>
|
||||
</Grid>
|
||||
|
@ -14,19 +14,37 @@ namespace DerpingDrivers
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Operating System Architecture display name
|
||||
/// Gets Operating System Architecture display name.
|
||||
/// </summary>
|
||||
public string OsArchitecture =>
|
||||
OsVersionInfo.OsBits == OsVersionInfo.SoftwareArchitecture.Bit64 ? "64-bit" : "32-bit";
|
||||
|
||||
/// <summary>
|
||||
/// Operating System display name
|
||||
/// Gets Operating System display name.
|
||||
/// </summary>
|
||||
public string OsVersionName => $"{OsVersionInfo.Name} {OsVersionInfo.Edition}";
|
||||
|
||||
/// <summary>
|
||||
/// Operating System build number
|
||||
/// Gets Operating System build number.
|
||||
/// </summary>
|
||||
public string OsVersion => OsVersionInfo.VersionString;
|
||||
|
||||
/// <summary>
|
||||
/// Gets Boot mode (Legacy BIOS, UEFI).
|
||||
/// </summary>
|
||||
public string BootMode => UEFIHelper.IsRunningInUEFIMode ? "UEFI" : "Legacy BIOS";
|
||||
|
||||
/// <summary>
|
||||
/// Gets Secure Boot status.
|
||||
/// </summary>
|
||||
public string SecureBootEnabled => UEFIHelper.IsRunningInUEFIMode
|
||||
? (UEFIHelper.IsSecureBootEnabled ? "Enabled" : "Disabled")
|
||||
: "Not available";
|
||||
|
||||
/// <summary>
|
||||
/// Gets OS upgrade status.
|
||||
/// </summary>
|
||||
public string OsUpgradeStatus =>
|
||||
OsUpgradeDetection.IsGrandfathered ? "In-place upgraded" : "Clean installation";
|
||||
}
|
||||
}
|
56
DerpingDrivers/Util/OsUpgradeDetection.cs
Normal file
56
DerpingDrivers/Util/OsUpgradeDetection.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace DerpingDrivers.Util
|
||||
{
|
||||
/// <summary>
|
||||
/// https://superuser.com/a/1184670
|
||||
/// </summary>
|
||||
public class OsUpgradeDetection
|
||||
{
|
||||
public static bool IsGrandfathered
|
||||
{
|
||||
get
|
||||
{
|
||||
using (var setupKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\Setup"))
|
||||
{
|
||||
using (var upgradeKey = setupKey?.OpenSubKey("Upgrade"))
|
||||
{
|
||||
// if this key isn't there, don't look any further
|
||||
if (upgradeKey == null) return false;
|
||||
|
||||
// only look at "Source OS (...)" sub-keys
|
||||
foreach (var sosKeyName in setupKey.GetSubKeyNames().Where(v =>
|
||||
v.StartsWith("Source OS", StringComparison.InvariantCultureIgnoreCase)))
|
||||
{
|
||||
using (var sosKey = setupKey.OpenSubKey(sosKeyName))
|
||||
{
|
||||
var productName = sosKey?.GetValue("ProductName") as string;
|
||||
|
||||
if (string.IsNullOrEmpty(productName))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// TODO: untested but should work
|
||||
if (productName.StartsWith("Windows 7", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO: untested but should work
|
||||
if (productName.StartsWith("Windows 8", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
49
DerpingDrivers/Util/UEFIHelper.cs
Normal file
49
DerpingDrivers/Util/UEFIHelper.cs
Normal file
@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace DerpingDrivers.Util
|
||||
{
|
||||
public class UEFIHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks if Secure Boot is enabled.
|
||||
/// </summary>
|
||||
public static bool IsSecureBootEnabled
|
||||
{
|
||||
get
|
||||
{
|
||||
var val = Convert.ToInt32(Registry.GetValue(
|
||||
@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecureBoot\State",
|
||||
"UEFISecureBootEnabled",
|
||||
0));
|
||||
|
||||
return val > 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the current system is running in UEFI or Legacy BIOS mode.
|
||||
/// </summary>
|
||||
/// <remarks>https://theroadtodelphi.com/2013/02/19/how-distinguish-when-windows-was-installed-in-legacy-bios-or-uefi-mode-using-delphi/</remarks>
|
||||
public static bool IsRunningInUEFIMode
|
||||
{
|
||||
get
|
||||
{
|
||||
GetFirmwareEnvironmentVariable(
|
||||
string.Empty,
|
||||
"{00000000-0000-0000-0000-000000000000}",
|
||||
IntPtr.Zero, 0);
|
||||
|
||||
var error = Marshal.GetLastWin32Error();
|
||||
|
||||
return error != 0x01; // ERROR_INVALID_FUNCTION
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport("kernel32.dll", EntryPoint = "GetFirmwareEnvironmentVariableW", SetLastError = true,
|
||||
CharSet = CharSet.Unicode, ExactSpelling = true)]
|
||||
private static extern uint GetFirmwareEnvironmentVariable(string lpName, string lpGuid, IntPtr pBuffer,
|
||||
uint nSize);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user