Introduction
This article describes the basic concepts of registry development under. NET Compact Framework, introduces the tools for operating the registry under Windows Mobile and Wince, and implements a registry export tool using C.
Background
Like Windows desktop systems, Wince stores system information, driver information, and application information in the registry. Recently, you need to enable automatic registration of 3G devices and automatic creation of 3G link items. In the process of implementing this function, it is found that both the device registration information and the link item information are saved in the registry. Therefore, you need to export the registry function and compare the registry information before and after registration.
What is registry?
Registry is a hierarchical file database used to store operating system information, driver information, application information, user information, and so on. There are two Key items in the Registry: key and Value. Here I keep them in English, because translating them into Chinese is a Key and Value, which is easy to confuse. The so-called Key can containSubKeyAndValue. It can be understood as a container node. the SubKey and Key are essentially the same. The SubKey can continue to include SubKey and Value. Value is includeData Type,Data ValueCan also be understood as a leaf node, and Value cannot contain otherSubKeyAndValue.
Solution
To view and modify the registry, we recommend that you use the following two tools:
Windows CE Remote Registry Editor
Windows CE Remote Registry Editor can be found in the VS 2005 and VS 2008 menus. You can remotely view and modify the registry, which is very convenient. However, this tool does not support the import/export function.
PHM Registry Editor
This tool can be installed on a device, and you can directly view and modify the Registry on the device. PHM Registry Editor only supports the import function, does not support the export function, and exports are in private format, and is not compatible with MS.
Reality
Due to the limitations of the above two tools, I have implemented a Registry export function, which is compatible with the file format of the Registry Editor export function on the desktop.
The operation registry class is introduced in CF.net 2.0, which is not supported by CF.net 1.0. To use the registry function, you must reference Microsoft. Win32 namespace.
using Microsoft.Win32;
The following is the export interface, which exports all the keys under the root to StringBuilder.
public StringBuilder Export()
{
StringBuilder sb = new StringBuilder();
Export(sb, Registry.ClassesRoot);
Export(sb, Registry.CurrentUser);
Export(sb, Registry.LocalMachine);
Export(sb, Registry.Users);
return sb;
}
In Windows Mobile and Windows Wince systems, the Registry has fewer items than the desktop system, such as visibility.
Code for exporting a specific key
public void Export(StringBuilder sb, RegistryKey key)
{
//log down itself.
sb.AppendFormat(CultureInfo.CurrentCulture, "\r\n[{0}]\r\n", key.Name);
//log down values
string[] s = key.GetValueNames();
Array.Sort(s);
//log down "Default" first
try
{
key.GetValue(Default);
ExportValue(sb, key, Default);
}
catch
{
}
foreach(string name in s)
{
if (!name.Equals(Default))
{
ExportValue(sb, key, name);
}
}
//log down subkeys
s = key.GetSubKeyNames();
Array.Sort(s);
foreach(string subKeyName in s)
{
Export(sb, key.OpenSubKey(subKeyName));
}
}
When exporting a Key, first export itself, and then export all Values under the Key. When exporting a Value, if there is a Default Value, export the Default Value first, and then export other Values. Export the Value and then recursively export all SubKeys.
Code for exporting Value
private void ExportValue(StringBuilder sb, RegistryKey key, string name)
{
switch (key.GetValueKind(name))
{
case RegistryValueKind.DWord:
int dword = (int)key.GetValue(name);
if (name.Equals(Default))
{
sb.AppendFormat(CultureInfo.CurrentCulture, "@=dword:{0:X8}\r\n", dword);
}
else
{
sb.AppendFormat(CultureInfo.CurrentCulture, "\"{0}\"=dword:{1:X8}\r\n", name, dword);
}
break;
case RegistryValueKind.String:
if (name.Equals(Default))
{
sb.AppendFormat(CultureInfo.CurrentCulture, "@=\"{0}\"\r\n", key.GetValue(name));
}
else
{
sb.AppendFormat(CultureInfo.CurrentCulture, "\"{0}\"=\"{1}\"\r\n", name, key.GetValue(name));
}
break;
case RegistryValueKind.MultiString:
string[] values = (string[])key.GetValue(name);
if (name.Equals(Default))
{
sb.Append("@=multi_sz:");
}
else
{
sb.AppendFormat(CultureInfo.CurrentCulture, "\"{0}\"=multi_sz:", name);
}
for (int i = 0; i < values.Length; i++)
{
if (i != 0)
{
sb.Append(",");
}
sb.AppendFormat(CultureInfo.CurrentCulture, "\"{0}\"", values[i]);
}
sb.Append("\r\n");
break;
case RegistryValueKind.Binary:
byte[] bytes = (byte[])key.GetValue(name);
if (name.Equals(Default))
{
sb.Append("@=hex:");
}
else
{
sb.AppendFormat(CultureInfo.CurrentCulture, "\"{0}\"=hex:", name);
}
int j = 0;
for (int i = 0; i < bytes.Length; i++)
{
// Display each byte as two hexadecimal digits.
if (i == (bytes.Length - 1))
{
sb.AppendFormat(CultureInfo.CurrentCulture, "{0:X2}", bytes[i]);
}
else
{
sb.AppendFormat(CultureInfo.CurrentCulture, "{0:X2},", bytes[i]);
}
++j;
if (j == 25)
{
j = 0;
sb.Append("\\\r\n");
}
}
sb.Append("\r\n");
break;
}
}
Because Value has different data types, different formats are exported based on the data type. The key. GetValueKind () function can retrieve the Key data type RegistryValueKind.
Program Running Effect
Source code: RegistryExporter.rar
Environment: Visual Studio 2008 + Windows Mobile 6 professional SDK +. NET Compact Framework 2.0