I wanted to export registry in C# .Net project. First of all I noticed that the Microsoft.Win32.Registry doesnt' provide this function.
I've searched a web and found VB.NET implementation at http://www.controlled-insanity.com/Forum/viewtopic.php?p=58715&sid=6cb99f5471bc3e916896443cd0f9f26e.
Unfortunately the code calls a few functions that were not included on the page and I wrote ( or copied from others web sites) them to make the code compilable.
Because my particular project is in C# and I didn't want to have additional DLL, I used .Net Reflector to convert VB class to C#.
A few disasssembled functions in C# were not compilable and looked very ugly.
I've noticed that vb On Error Resume Next causes .Net Reflector to generate not compilable C# code.
So I commented out On Error Resume Next , which is a good idea anyway, recompile VB code and disaccemble it as C# code, fixed some VB/C# discrepencies and make the C# class working.
At the end I understood that to export the registry the class I just need to Process.Start regedit.exe with corresponding parameters, so I didn't need most of the code.
However I will post the whole class here. Note that I tested only ExportKey method of the class.
//'http://www.controlled-insanity.com/Forum/viewtopic.php?p=58715&sid=6cb99f5471bc3e916896443cd0f9f26e
namespace ModRegistry
{
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
// [StandardModule]
public sealed class RegistryHelper
{
// 'Just a wee bit of API to fill the gaps not covered by .NET's registry access
// 'Used to determine a value's type and read/write strings of the type REG_EXPAND_SZ
[DllImport("advapi32.dll", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
private static extern int RegCloseKey(int hKey);
[DllImport("advapi32.dll", EntryPoint="RegCreateKeyA", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
private static extern int RegCreateKey(int hKey, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpSubKey, ref int phkResult);
[DllImport("advapi32.dll", EntryPoint="RegOpenKeyA", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
private static extern int RegOpenKey(int hKey, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpSubKey, ref int phkResult);
[DllImport("advapi32.dll", EntryPoint="RegQueryValueExA", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
private static extern int RegQueryValueEx(int hKey, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpValueName, int lpReserved, ref int lpType, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpData, ref int lpcbData);
[DllImport("advapi32.dll", EntryPoint="RegSetValueExA", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
private static extern int RegSetValueEx(int hKey, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpValueName, int Reserved, int dwType, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpData, int cbData);
// Methods
public static bool CreateKey(RegistryKey hKey, string strPath, string NewKeyName)
{
bool flag1=false;
RegistryKey key1 = hKey.OpenSubKey(strPath, true);
if (key1 != null)
{
key1.CreateSubKey(NewKeyName);
key1.Close();
flag1 = true;
}
return flag1;
}
public static bool DeleteKey(RegistryKey hKey, string strPath)
{
bool flag1=false;
int num1=0;
int num2=0;
try
{
ProjectData.ClearProjectError();
num2 = 1;
hKey.DeleteSubKeyTree(strPath);
flag1 = true;
}
catch (Exception obj1) //when (?)
{
Exception exception2 = (Exception) obj1;
ProjectData.SetProjectError(exception2);
Exception exception1 = exception2;
if (num1 == 0)
{
num1 = -1;
switch (num2)
{
case 1:
{
goto Label_0058;
}
}
throw;
}
}
Label_0058:
if (num1 != 0)
{
ProjectData.ClearProjectError();
}
return flag1;
}
public static bool DeleteValue(RegistryKey hKey, string strPath, string strValue)
{
bool flag1=false;
RegistryKey key1 = hKey.OpenSubKey(strPath, true);
if (key1 != null)
{
key1.DeleteValue(strValue);
key1.Close();
flag1 = true;
}
return flag1;
}
public static void ExportKey(RegistryKey rKey, string sfile)
{
RegistryHelper.ExportKey(rKey.Name, sfile);
}
public static void ExportKey(string sRegKeyPath, string sfile)
{
string text1 = "\"" + sRegKeyPath + "\"";
RegistryHelper.FileAppend(sfile, "");
RegistryHelper.ShellFile("regedit.exe", "/E " + RegistryHelper.GetDosPath(sfile) + " " + text1, ProcessWindowStyle.Normal);
}
public static void ExportKey(RegistryKey hKey, string strPath, string sfile)
{
string text1="";
if (hKey == Registry.ClassesRoot)
{
text1 = @"HKEY_CLASSES_ROOT\";
}
else if (hKey == Registry.CurrentUser)
{
text1 = @"HKEY_CURRENT_USER\";
}
else if (hKey == Registry.LocalMachine)
{
text1 = @"HKEY_LOCAL_MACHINE\";
}
else if (hKey == Registry.Users)
{
text1 = @"HKEY_USERS\";
}
else if (hKey == Registry.CurrentConfig)
{
text1 = @"HKEY_CURRENT_CONFIG\";
}
else if (hKey == Registry.DynData)
{
text1 = @"HKEY_DYN_DATA\";
}
else if (hKey == Registry.PerformanceData)
{
text1 = @"HKEY_PERFORMANCE_DATA\";
}
RegistryHelper.ExportKey(text1, sfile);
// ' FileAppend(sfile, "")
// 'mPath = Microsoft.VisualBasic.ControlChars.Quote & mPath & strPath & Microsoft.VisualBasic.ControlChars.Quote
// 'ShellFile("regedit.exe", "/E " & GetDosPath(sfile) & " " & mPath, 0)
}
public static string[] GetAllKeys(RegistryKey hKey, string strPath)
{
string[] textArray1=null;
RegistryKey key1 = hKey.OpenSubKey(strPath, true);
if (key1 != null)
{
textArray1 = key1.GetSubKeyNames();
key1.Close();
}
return textArray1;
}
public static bool GetAllValues(RegistryKey hKey, string strPath, ref string[] mNames)
{
bool flag1=false;
RegistryKey key1 = hKey.OpenSubKey(strPath, true);
if (key1 != null)
{
mNames = key1.GetValueNames();
flag1 = true;
key1.Close();
}
return flag1;
}
public static string GetDosPath(string path)
{
return RegistryHelper.GetShortFileName(path);
}
public static void GetRootKeyAndPath(string FullPath, ref RegistryKey Rootkey, ref string strPath)
{
if ((FullPath.Length != 0) && (FullPath.IndexOf(@"\") != -1))
{
string[] textArray1 = Strings.Split(FullPath, @"\", -1, CompareMethod.Binary);
string text1 = RegistryHelper.AddBackslash(FullPath, @"\");
// 'tmp(0) is "My Computer"
// 'tmp(1) is the "Root" key - eg. "HKEY_CLASSES_ROOT"
strPath = text1.Replace(textArray1[0] + @"\" + textArray1[1] + @"\", "");
// 'strPath is the path of the subkey
string text2 = textArray1[1];//Determine the "Root" key
if (StringType.StrCmp(text2, "HKEY_CLASSES_ROOT", false) == 0)
{
Rootkey = Registry.ClassesRoot;
}
else if (StringType.StrCmp(text2, "HKEY_CURRENT_USER", false) == 0)
{
Rootkey = Registry.CurrentUser;
}
else if (StringType.StrCmp(text2, "HKEY_LOCAL_MACHINE", false) == 0)
{
Rootkey = Registry.LocalMachine;
}
else if (StringType.StrCmp(text2, "HKEY_USERS", false) == 0)
{
Rootkey = Registry.Users;
}
else if (StringType.StrCmp(text2, "HKEY_CURRENT_CONFIG", false) == 0)
{
Rootkey = Registry.CurrentConfig;
}
else if (StringType.StrCmp(text2, "HKEY_DYNDATA", false) == 0)
{
Rootkey = Registry.DynData;
}
else if (StringType.StrCmp(text2, "HKEY_PERFORMANCE_DATA", false) == 0)
{
Rootkey = Registry.PerformanceData;
}
strPath = RegistryHelper.RemoveBackslash(strPath);
}
}
public static string GetRootKeyString(string FullPath)
{
string[] textArray1 = Strings.Split(FullPath, @"\", -1, CompareMethod.Binary);
return textArray1[1];
}
public static string GetSafeKeyName(RegistryKey hKey, string strPath, string NewName, [Optional] bool AsNew /* = false */)
{
string text1="";
int num1 = 1;
string text2 = NewName;
if (AsNew)
{
text2 = "New Key";
NewName = "New Key #1";
}
RegistryKey key1 = hKey.OpenSubKey(strPath, true);
if (key1 == null)
{
return text1;
}
if (key1.SubKeyCount > 0)
{
string[] textArray1 = key1.GetSubKeyNames();
while (true)
{
bool flag1=false;
int num3 = key1.SubKeyCount - 1;
for (int num2 = 0; num2 <= num3; num2++)
{
if (StringType.StrCmp(textArray1[num2], NewName, false) == 0)
{
flag1 = true;
break;
}
}
if (!flag1)
{
text1 = NewName;
goto Label_0096;
}
num1++;
flag1 = false;
NewName = text2 + " #" + StringType.FromInteger(num1);
}
}
text1 = NewName;
Label_0096:
key1.Close();
return text1;
}
public static string GetSafeValueName(RegistryKey hKey, string strPath, string NewName, [Optional] bool AsNew /* = false */)
{
string text1="";
int num1 = 1;
string text2 = NewName;
if (AsNew)
{
text2 = "New Value";
NewName = "New Value #1";
}
RegistryKey key1 = hKey.OpenSubKey(strPath, true);
if (key1 == null)
{
return text1;
}
if (key1.ValueCount > 0)
{
string[] textArray1 = key1.GetValueNames();
while (true)
{
bool flag1=false;
int num3 = key1.ValueCount - 1;
for (int num2 = 0; num2 <= num3; num2++)
{
if (StringType.StrCmp(textArray1[num2], NewName, false) == 0)
{
flag1 = true;
break;
}
}
if (!flag1)
{
text1 = NewName;
goto Label_0096;
}
num1++;
flag1 = false;
NewName = text2 + " #" + StringType.FromInteger(num1);
}
}
text1 = NewName;
Label_0096:
key1.Close();
return text1;
}
public static byte[] GetSettingByte(ref RegistryKey hKey, ref string strPath, ref string strValue)
{
byte[] buffer1=null;
RegistryKey key1 = hKey.OpenSubKey(strPath, true);
if (key1 != null)
{
byte[] buffer2 = (byte[]) key1.GetValue(strValue, "");
key1.Close();
return buffer2;
}
return buffer1;
}
public static int GetSettingInteger(ref RegistryKey hKey, ref string strPath, ref string strValue)
{
RegistryKey key1 = hKey.OpenSubKey(strPath, true);
if (key1 != null)
{
int num1 = IntegerType.FromObject(key1.GetValue(strValue, 0));
key1.Close();
return num1;
}
return -1;
}
public static string GetSettingString(RegistryKey hKey, string strPath, string strValue, [Optional] bool DontExpand /* = false */)
{
string text1="";
int num6 = RegistryHelper.GetValueType(hKey, strPath, strValue);
if (DontExpand | (num6 > 2))
{
// '.NET will automatically expand string values of the type REG_EXPAND_SZ
// 'It always likes to do the work for you ! Trouble is we want to see
// 'the data in it's raw form !!!
int num1=0;
int num2=0;
int num3=0;
int num5=0;
string text2="";
if (hKey == Registry.ClassesRoot)
{