動作確認環境:C# 8(.NET Core 3.1)、9(.NET 5.0)
- レジストリから
HKEY_CLASSES_ROOT\AppID
キーの情報を取得する。
C#でWindows 10のレジストリに登録されたAppID情報を取得するクラスです。レジストリから値の取得は実装していますが、取得した値の整形は実装していません。
using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using Microsoft.Win32; var infos = RegistryAppIDKeyInfo.GetInfos(); var infosHasAccessPermission = infos.Where(info => info.AccessPermission != null).ToArray(); var infosHasActivateAtStorage = infos.Where(info => info.ActivateAtStorage != null).ToArray(); var infosHasAppIDFlags = infos.Where(info => info.AppIDFlags != null).ToArray(); var infosHasAuthenticationLevel = infos.Where(info => info.AuthenticationLevel != null).ToArray(); var infosHasDLLSurrogate = infos.Where(info => info.DLLSurrogate != null).ToArray(); var infosHasDLLSurrogateExecutable = infos.Where(info => info.DLLSurrogateExecutable != null).ToArray(); var infosHasEndpoints = infos.Where(info => info.EndPoints != null).ToArray(); var infosHasLaunchPermission = infos.Where(info => info.LaunchPermission != null).ToArray(); var infosHasLoadUserSettings = infos.Where(info => info.LoadUserSettings != null).ToArray(); var infosHasLocalService = infos.Where(info => info.LocalService != null).ToArray(); var infosHasPreferredServerBitness = infos.Where(info => info.PreferredServerBitness != null).ToArray(); var infosHasRemoteServerName = infos.Where(info => info.RemoteServerName != null).ToArray(); var infosHasROTFlags = infos.Where(info => info.ROTFlags != null).ToArray(); var infosHasRunAs = infos.Where(info => info.RunAs != null).ToArray(); var infosHasServiceParameters = infos.Where(info => info.ServiceParameters != null).ToArray(); var infosHasSRPTrustLevel = infos.Where(info => info.SRPTrustLevel != null).ToArray(); sealed record RegistryAppIDKeyInfo { public readonly Guid AppID; public readonly ImmutableArray<byte> AccessPermission; public readonly string ActivateAtStorage; public readonly uint? AppIDFlags; public readonly uint? AuthenticationLevel; public readonly string DLLSurrogate; public readonly string DLLSurrogateExecutable; public readonly ImmutableArray<string> EndPoints; public readonly ImmutableArray<byte> LaunchPermission; public readonly uint? LoadUserSettings; public readonly string LocalService; public readonly uint? PreferredServerBitness; public readonly string RemoteServerName; public readonly uint? ROTFlags; public readonly string RunAs; public readonly string ServiceParameters; public readonly uint? SRPTrustLevel; public static RegistryAppIDKeyInfo[] GetInfos( RegistryView view = RegistryView.Registry64) { using var hkcr = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, view); using var appIdsKey = hkcr.OpenSubKey("AppID"); var infos = new List<RegistryAppIDKeyInfo>(); foreach (var entryName in appIdsKey.GetSubKeyNames()) { if (!Guid.TryParseExact(entryName, "B", out var appId)) continue; using var appIdKey = appIdsKey.OpenSubKey(entryName); infos.Add(new RegistryAppIDKeyInfo(appId, appIdKey)); } return infos.ToArray(); } RegistryAppIDKeyInfo() => throw new InvalidOperationException(); RegistryAppIDKeyInfo(in Guid guid, RegistryKey key) { if (key == null) throw new ArgumentNullException(nameof(key)); AppID = guid; if (key.GetValue("AccessPermission", null) is byte[] accessPermission) AccessPermission = accessPermission.ToImmutableArray(); ActivateAtStorage = (string)key.GetValue("ActivateAtStorage", null); AppIDFlags = (uint?)(int?)key.GetValue("AppIdFlags", null); AuthenticationLevel = (uint?)(int?)key.GetValue("AuthenticationLevel", null); DLLSurrogate = (string)key.GetValue("DLLSurrogate", null); DLLSurrogateExecutable = (string)key.GetValue("DllSurrogateExecutable", null); if (key.GetValue("Endpoints", null) is string[] endPoints) EndPoints = endPoints.ToImmutableArray(); if (key.GetValue("LaunchPermission", null) is byte[] launchPermission) LaunchPermission = launchPermission.ToImmutableArray(); LoadUserSettings = (uint?)(int?)key.GetValue("LoadUserSettings", null); LocalService = (string)key.GetValue("LocalService", null); PreferredServerBitness = (uint?)(int?)key.GetValue("PreferredServerBitness", null); RemoteServerName = (string)key.GetValue("RemoteServerName", null); ROTFlags = (uint?)(int?)key.GetValue("ROTFlags", null); RunAs = (string)key.GetValue("RunAs", null); ServiceParameters = (string)key.GetValue("ServiceParameters", null); SRPTrustLevel = (uint?)(int?)key.GetValue("SRPTrustLevel", null); } }
.csproj
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net5.0-windows</TargetFramework> <UseWindowsForms>true</UseWindowsForms> </PropertyGroup> </Project>
Main関数(C# 8)
using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using Microsoft.Win32; class Program { static void Main() { var infos = RegistryAppIDKeyInfo.GetInfos(); var infosHasAccessPermission = infos.Where(info => info.AccessPermission != null).ToArray(); var infosHasActivateAtStorage = infos.Where(info => info.ActivateAtStorage != null).ToArray(); var infosHasAppIDFlags = infos.Where(info => info.AppIDFlags != null).ToArray(); var infosHasAuthenticationLevel = infos.Where(info => info.AuthenticationLevel != null).ToArray(); var infosHasDLLSurrogate = infos.Where(info => info.DLLSurrogate != null).ToArray(); var infosHasDLLSurrogateExecutable = infos.Where(info => info.DLLSurrogateExecutable != null).ToArray(); var infosHasEndpoints = infos.Where(info => info.EndPoints != null).ToArray(); var infosHasLaunchPermission = infos.Where(info => info.LaunchPermission != null).ToArray(); var infosHasLoadUserSettings = infos.Where(info => info.LoadUserSettings != null).ToArray(); var infosHasLocalService = infos.Where(info => info.LocalService != null).ToArray(); var infosHasPreferredServerBitness = infos.Where(info => info.PreferredServerBitness != null).ToArray(); var infosHasRemoteServerName = infos.Where(info => info.RemoteServerName != null).ToArray(); var infosHasROTFlags = infos.Where(info => info.ROTFlags != null).ToArray(); var infosHasRunAs = infos.Where(info => info.RunAs != null).ToArray(); var infosHasServiceParameters = infos.Where(info => info.ServiceParameters != null).ToArray(); var infosHasSRPTrustLevel = infos.Where(info => info.SRPTrustLevel != null).ToArray(); } internal sealed class RegistryAppIDKeyInfo { public readonly Guid AppID; public readonly ImmutableArray<byte> AccessPermission; public readonly string ActivateAtStorage; public readonly uint? AppIDFlags; public readonly uint? AuthenticationLevel; public readonly string DLLSurrogate; public readonly string DLLSurrogateExecutable; public readonly ImmutableArray<string> EndPoints; public readonly ImmutableArray<byte> LaunchPermission; public readonly uint? LoadUserSettings; public readonly string LocalService; public readonly uint? PreferredServerBitness; public readonly string RemoteServerName; public readonly uint? ROTFlags; public readonly string RunAs; public readonly string ServiceParameters; public readonly uint? SRPTrustLevel; public static RegistryAppIDKeyInfo[] GetInfos( RegistryView view = RegistryView.Registry64) { using var hkcr = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, view); using var appIdsKey = hkcr.OpenSubKey("AppID"); var infos = new List<RegistryAppIDKeyInfo>(); foreach (var entryName in appIdsKey.GetSubKeyNames()) { if (!Guid.TryParseExact(entryName, "B", out var appId)) continue; using var appIdKey = appIdsKey.OpenSubKey(entryName); infos.Add(new RegistryAppIDKeyInfo(appId, appIdKey)); } return infos.ToArray(); } RegistryAppIDKeyInfo() => throw new InvalidOperationException(); RegistryAppIDKeyInfo(in Guid guid, RegistryKey key) { if (key == null) throw new ArgumentNullException(nameof(key)); AppID = guid; var accessPermission = (byte[])key.GetValue("AccessPermission", null); if (accessPermission != null) AccessPermission = ImmutableArray.CreateRange(accessPermission); ActivateAtStorage = (string)key.GetValue("ActivateAtStorage", null); AppIDFlags = (uint?)(int?)key.GetValue("AppIdFlags", null); AuthenticationLevel = (uint?)(int?)key.GetValue("AuthenticationLevel", null); DLLSurrogate = (string)key.GetValue("DLLSurrogate", null); DLLSurrogateExecutable = (string)key.GetValue("DllSurrogateExecutable", null); var endPoints = (string[])key.GetValue("Endpoints", null); if (endPoints != null) EndPoints = ImmutableArray.CreateRange(endPoints); var launchPermission = (byte[])key.GetValue("LaunchPermission", null); if (launchPermission != null) LaunchPermission = ImmutableArray.CreateRange(launchPermission); LoadUserSettings = (uint?)(int?)key.GetValue("LoadUserSettings", null); LocalService = (string)key.GetValue("LocalService", null); PreferredServerBitness = (uint?)(int?)key.GetValue("PreferredServerBitness", null); RemoteServerName = (string)key.GetValue("RemoteServerName", null); ROTFlags = (uint?)(int?)key.GetValue("ROTFlags", null); RunAs = (string)key.GetValue("RunAs", null); ServiceParameters = (string)key.GetValue("ServiceParameters", null); SRPTrustLevel = (uint?)(int?)key.GetValue("SRPTrustLevel", null); } } }
.csproj
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp3.1</TargetFramework> <UseWindowsForms>true</UseWindowsForms> </PropertyGroup> </Project>