Last active
November 7, 2023 14:39
-
-
Save floko84/a1efd40c73367e40087d5e4acd968ab3 to your computer and use it in GitHub Desktop.
Parse Microsoft Office DisabledItems from Registry $"HKCU\Software\Microsoft\Office\{version}\{appName}\Resiliency\DisabledItems" in C#.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
/// <summary> | |
/// Structure of an Microsoft Office disabled item entry. | |
/// </summary> | |
public readonly struct DisabledItem | |
{ | |
public DisabledItem(int type, string path, string name) | |
{ | |
this.Type = type; | |
this.Path = path; | |
this.Name = name; | |
} | |
public int Type { get; } | |
public string Path { get; } | |
public string Name { get; } | |
public static DisabledItem Parse(byte[] value) | |
{ | |
if (value == null) | |
{ | |
throw new ArgumentNullException(nameof(value)); | |
} | |
using (var stream = new MemoryStream(value, false)) | |
using (var reader = new BinaryReader(stream, Encoding.Unicode)) | |
{ | |
var type = reader.ReadInt32(); | |
var pathLength = reader.ReadInt32(); | |
var nameLength = reader.ReadInt32(); | |
var pathBytes = reader.ReadBytes(pathLength); | |
var path = string.Concat(Encoding.Unicode.GetString(pathBytes).TakeWhile(x => x != 0)); | |
var nameBytes = reader.ReadBytes(nameLength); | |
var name = string.Concat(Encoding.Unicode.GetString(nameBytes).TakeWhile(x => x != 0)); | |
return new DisabledItem(type, path, name); | |
} | |
} | |
public static bool TryParse(byte[] value, out DisabledItem result) | |
{ | |
try | |
{ | |
result = Parse(value); | |
return true; | |
} | |
catch | |
{ | |
result = default(DisabledItem); | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment