mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-09 12:46:47 +02:00
[Registry Preview] Better preview in data grid like in Regedit (#28488)
* Fix typo * Better data grid preview like regedit * Fix sorting of resource strings * Add error icons back in * Remove comments then trim whitespace * Better string detection
This commit is contained in:
@@ -8,7 +8,9 @@ using System.Collections.Generic;
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.UI.Input;
|
using Microsoft.UI.Input;
|
||||||
using Microsoft.UI.Xaml;
|
using Microsoft.UI.Xaml;
|
||||||
@@ -413,21 +415,75 @@ namespace RegistryPreview
|
|||||||
value += registryLine;
|
value += registryLine;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean out any escaped characters in the value, only for the preview
|
|
||||||
value = StripEscapedCharacters(value);
|
|
||||||
|
|
||||||
// update the ListViewItem with the loaded value, based off REG value type
|
// update the ListViewItem with the loaded value, based off REG value type
|
||||||
switch (registryValue.Type)
|
switch (registryValue.Type)
|
||||||
{
|
{
|
||||||
case "ERROR":
|
case "ERROR":
|
||||||
// do nothing
|
// do nothing
|
||||||
break;
|
break;
|
||||||
|
case "REG_SZ":
|
||||||
|
if (value == "\"")
|
||||||
|
{
|
||||||
|
// Value is most likely missing an end quote
|
||||||
|
registryValue.Type = "ERROR";
|
||||||
|
value = resourceLoader.GetString("InvalidString");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (int i = 1; i < value.Length; i++)
|
||||||
|
{
|
||||||
|
if (value[i - 1] == '\\')
|
||||||
|
{
|
||||||
|
// Only allow these escape characters
|
||||||
|
if (value[i] != '"' && value[i] != '\\')
|
||||||
|
{
|
||||||
|
registryValue.Type = "ERROR";
|
||||||
|
value = resourceLoader.GetString("InvalidString");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value[i - 1] != '\\' && value[i] == '"')
|
||||||
|
{
|
||||||
|
// Don't allow non-escaped quotes
|
||||||
|
registryValue.Type = "ERROR";
|
||||||
|
value = resourceLoader.GetString("InvalidString");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (registryValue.Type != "ERROR")
|
||||||
|
{
|
||||||
|
// Clean out any escaped characters in the value, only for the preview
|
||||||
|
value = StripEscapedCharacters(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
registryValue.Value = value;
|
||||||
|
break;
|
||||||
case "REG_BINARY":
|
case "REG_BINARY":
|
||||||
case "REG_NONE":
|
case "REG_NONE":
|
||||||
if (value.Length <= 0)
|
if (value.Length <= 0)
|
||||||
{
|
{
|
||||||
value = resourceLoader.GetString("ZeroLength");
|
value = resourceLoader.GetString("ZeroLength");
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Hexes are usually two characters (00), it's invalid if less or more than 2
|
||||||
|
var bytes = value.Split(',').Select(
|
||||||
|
c => c.Length == 2 ? byte.Parse(c, NumberStyles.HexNumber, CultureInfo.InvariantCulture) : throw null);
|
||||||
|
value = string.Join(' ', bytes.Select(b => b.ToString("x2", CultureInfo.CurrentCulture)));
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
registryValue.Type = "ERROR";
|
||||||
|
value = resourceLoader.GetString("InvalidBinary");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
registryValue.Value = value;
|
registryValue.Value = value;
|
||||||
|
|
||||||
@@ -436,6 +492,19 @@ namespace RegistryPreview
|
|||||||
if (value.Length <= 0)
|
if (value.Length <= 0)
|
||||||
{
|
{
|
||||||
registryValue.Type = "ERROR";
|
registryValue.Type = "ERROR";
|
||||||
|
value = resourceLoader.GetString("InvalidDword");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (uint.TryParse(value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out uint dword))
|
||||||
|
{
|
||||||
|
value = $"0x{dword:x8} ({dword})";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
registryValue.Type = "ERROR";
|
||||||
|
value = resourceLoader.GetString("InvalidDword");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
registryValue.Value = value;
|
registryValue.Value = value;
|
||||||
@@ -444,8 +513,55 @@ namespace RegistryPreview
|
|||||||
case "REG_QWORD":
|
case "REG_QWORD":
|
||||||
if (value.Length <= 0)
|
if (value.Length <= 0)
|
||||||
{
|
{
|
||||||
|
registryValue.Type = "ERROR";
|
||||||
value = resourceLoader.GetString("InvalidQword");
|
value = resourceLoader.GetString("InvalidQword");
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Hexes are usually two characters (00), it's invalid if less or more than 2
|
||||||
|
var bytes = value.Split(',').Select(
|
||||||
|
c => c.Length == 2 ? byte.Parse(c, NumberStyles.HexNumber, CultureInfo.InvariantCulture) : throw null).ToArray();
|
||||||
|
ulong qword = BitConverter.ToUInt64(bytes);
|
||||||
|
value = $"0x{qword:x8} ({qword})";
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
registryValue.Type = "ERROR";
|
||||||
|
value = resourceLoader.GetString("InvalidQword");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
registryValue.Value = value;
|
||||||
|
break;
|
||||||
|
case "REG_EXPAND_SZ":
|
||||||
|
case "REG_MULTI_SZ":
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Hexes are usually two characters (00), it's invalid if less or more than 2
|
||||||
|
var bytes = value.Split(',').Select(
|
||||||
|
c => c.Length == 2 ? byte.Parse(c, NumberStyles.HexNumber, CultureInfo.InvariantCulture) : throw null).ToArray();
|
||||||
|
|
||||||
|
if (registryValue.Type == "REG_MULTI_SZ")
|
||||||
|
{
|
||||||
|
// Replace zeros (00,00) with spaces
|
||||||
|
for (int i = 0; i < bytes.Length; i += 2)
|
||||||
|
{
|
||||||
|
if (bytes[i] == 0 && bytes[i + 1] == 0)
|
||||||
|
{
|
||||||
|
bytes[i] = 0x20;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
value = Encoding.Unicode.GetString(bytes);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
registryValue.Type = "ERROR";
|
||||||
|
value = resourceLoader.GetString("InvalidString");
|
||||||
|
}
|
||||||
|
|
||||||
registryValue.Value = value;
|
registryValue.Value = value;
|
||||||
break;
|
break;
|
||||||
@@ -1036,7 +1152,7 @@ namespace RegistryPreview
|
|||||||
value = value.Remove(indexOf, value.Length - indexOf);
|
value = value.Remove(indexOf, value.Length - indexOf);
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
return value.TrimEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ namespace RegistryPreview
|
|||||||
switch (Type)
|
switch (Type)
|
||||||
{
|
{
|
||||||
case "REG_SZ":
|
case "REG_SZ":
|
||||||
case "REG_EXAND_SZ":
|
case "REG_EXPAND_SZ":
|
||||||
case "REG_MULTI_SZ":
|
case "REG_MULTI_SZ":
|
||||||
return uriStringValue;
|
return uriStringValue;
|
||||||
case "ERROR":
|
case "ERROR":
|
||||||
|
|||||||
@@ -138,6 +138,12 @@
|
|||||||
<data name="FilterRegistryName" xml:space="preserve">
|
<data name="FilterRegistryName" xml:space="preserve">
|
||||||
<value>Registry files (*.reg)</value>
|
<value>Registry files (*.reg)</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="InvalidBinary" xml:space="preserve">
|
||||||
|
<value>(Invalid binary value)</value>
|
||||||
|
</data>
|
||||||
|
<data name="InvalidDword" xml:space="preserve">
|
||||||
|
<value>(Invalid DWORD (32-bit) value)</value>
|
||||||
|
</data>
|
||||||
<data name="InvalidQword" xml:space="preserve">
|
<data name="InvalidQword" xml:space="preserve">
|
||||||
<value>(Invalid QWORD (64-bit) value)</value>
|
<value>(Invalid QWORD (64-bit) value)</value>
|
||||||
</data>
|
</data>
|
||||||
@@ -147,6 +153,9 @@
|
|||||||
<data name="InvalidRegistryFileTitle" xml:space="preserve">
|
<data name="InvalidRegistryFileTitle" xml:space="preserve">
|
||||||
<value>File was not a Registry file</value>
|
<value>File was not a Registry file</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="InvalidString" xml:space="preserve">
|
||||||
|
<value>(Invalid string value)</value>
|
||||||
|
</data>
|
||||||
<data name="LargeRegistryFile" xml:space="preserve">
|
<data name="LargeRegistryFile" xml:space="preserve">
|
||||||
<value> is larger than 10MB which is too large for this application.</value>
|
<value> is larger than 10MB which is too large for this application.</value>
|
||||||
</data>
|
</data>
|
||||||
@@ -228,7 +237,7 @@
|
|||||||
<value>User Account Control</value>
|
<value>User Account Control</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="ValueColumn.Header" xml:space="preserve">
|
<data name="ValueColumn.Header" xml:space="preserve">
|
||||||
<value>Value</value>
|
<value>Data</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="WriteButton.Label" xml:space="preserve">
|
<data name="WriteButton.Label" xml:space="preserve">
|
||||||
<value>Write to Registry</value>
|
<value>Write to Registry</value>
|
||||||
|
|||||||
Reference in New Issue
Block a user