nx-installation_projects_vi.../NX-Installation/CreateReadWriteDeleteRegistry.cs

70 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
namespace NX_Installation
{
class CreateReadWriteDeleteRegistry
{
// Create a Subkey
public string MainKey { get; set; }
public string SubKey { get; set; }
public string Key { get; set; }
public string Value { get; set; }
public bool Error { get; set; }
public void WriteSubkey()
{
// Write Values to the Subkey
RegistryKey newKey = Registry.CurrentUser.CreateSubKey(SubKey);
newKey.SetValue(Key, Value);
}
public string ReadSubkey()
{
// Read Values from the Subkey
if (SubKeyExist(SubKey))
{
RegistryKey myKey = Registry.CurrentUser.OpenSubKey(SubKey);
return (string)myKey.GetValue(Key);
}
else
{
throw new Exception("Subkey does not exist!");
}
}
public void DeleteSubkey()
{
// Delete the Subkey
MainKey = SubKey.Remove(SubKey.LastIndexOf('\\'));
if (SubKeyExist(MainKey))
{
Registry.CurrentUser.DeleteSubKeyTree(MainKey);
}
else
{
throw new Exception("Subkey does not exist!");
}
}
public bool SubKeyExist(string Subkey)
{
// Check if a Subkey exist
RegistryKey myKey = Registry.CurrentUser.OpenSubKey(Subkey);
if (myKey == null)
{
return false;
}
else
{
return true;
}
}
}
}