📄 registry.cs
字号:
//==========================================================================================
//
// WJ.MPR.WinAPI.Registry
// Copyright (c) 2006, WJ Communications, Inc.
//
// This class is derived from the OpenNETCF.org Registry class.
//
//==========================================================================================
//==========================================================================================
//
// WJ.MPR.WinAPI.Registry
// Copyright (C) 2003, OpenNETCF.org
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the OpenNETCF.org Shared Source License.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the OpenNETCF.org Shared Source License
// for more details.
//
// You should have received a copy of the OpenNETCF.org Shared Source License
// along with this library; if not, email licensing@opennetcf.org to request a copy.
//
// If you wish to contact the OpenNETCF Advisory Board to discuss licensing, please
// email licensing@opennetcf.org.
//
// For general enquiries, email enquiries@opennetcf.org or visit our website at:
// http://www.opennetcf.org
//
//==========================================================================================
using System;
using System.Collections;
using System.Runtime.InteropServices;
namespace WJ.MPR.WinAPI
{
#region Registry
/// <summary>
/// Supplies the base <b>RegistryKeys</b> that access values and subkeys in the registry.
/// </summary>
/// <remarks>This class provides the set of standard root keys found in the registry on
/// machines running Windows CE. The registry is a storage facility for information about
/// applications and default system settings. For example, applications can use the
/// registry for storing information that needs to be preserved once the application is
/// closed, and access that same information when the application is reloaded.
/// For instance, you can store color preferences, screen locations, or the size of the window.
/// The keys exposed by Registry are:
/// <list type="bullet">
/// <item><term>CurrentUser</term>
/// <description>Stores information about user preferences.</description></item>
/// <item><term>LocalMachine</term>
/// <description>Stores configuration information for the local machine.</description></item>
/// <item><term>ClassesRoot</term>
/// <description>Stores information about types (and classes) and their properties.</description></item>
/// <item><term>Users</term>
/// <description>Stores information about the default user configuration.</description></item></list>
/// Once you have identified the root key under which you want to store/retrieve information
/// from the registry, you can use the RegistryKey class to add or remove subkeys, and manipulate
/// the values for a given key.</remarks>
public sealed class Registry
{
/// <summary>
/// Contains the configuration data for the local machine. This field reads the Windows registry base key HKEY_LOCAL_MACHINE.
/// </summary>
public static readonly RegistryKey LocalMachine = new RegistryKey((uint)RootKeys.LocalMachine, "\\HKEY_LOCAL_MACHINE", true, true);
/// <summary>
/// Contains information about the current user preferences. This field reads the Windows registry base key HKEY_CURRENT_USER.
/// </summary>
public static readonly RegistryKey CurrentUser = new RegistryKey((uint)RootKeys.CurrentUser, "\\HKEY_CURRENT_USER", true, true);
/// <summary>
/// Defines the types (or classes) of documents and the properties associated with those types. This field reads the Windows registry base key HKEY_CLASSES_ROOT.
/// </summary>
public static readonly RegistryKey ClassesRoot = new RegistryKey((uint)RootKeys.ClassesRoot, "\\HKEY_CLASSES_ROOT", true, true);
/// <summary>
/// Contains information about the default user configuration. This field reads the Windows registry base key HKEY_USERS.
/// </summary>
public static readonly RegistryKey Users = new RegistryKey((uint)RootKeys.Users, "\\HKEY_USERS", true, true);
}
//hKeys for the root keys
internal enum RootKeys : uint
{
ClassesRoot = 0x80000000,
CurrentUser = 0x80000001,
LocalMachine = 0x80000002,
Users = 0x80000003
}
#endregion
#region Registry Key
/// <summary>
/// Represents a key level node in the Windows registry. This class is a registry encapsulation.
/// </summary>
public sealed class RegistryKey : MarshalByRefObject, IDisposable
{
//hkey - handle to a registry key
private uint m_handle;
//full name of key
private string m_name;
//was key opened as writable
private bool m_writable;
//is root key
private bool m_isroot;
//error code when all items have been enumerated
private const int ERROR_NO_MORE_ITEMS = 259;
#region Constructor
internal RegistryKey(uint handle, string name, bool writable, bool isroot)
{
m_handle = handle;
m_name = name;
m_writable = writable;
m_isroot = isroot;
}
#endregion
#region Name
/// <summary>
/// Retrieves the name of the key.
/// </summary>
public string Name
{
get
{
return m_name;
}
}
#endregion
#region To String
/// <summary>
/// Retrieves a string representation of this key.
/// </summary>
/// <returns>A string representing the key. If the specified key is invalid (cannot be found) then a null value is returned.</returns>
/// <exception cref="System.ObjectDisposedException"> The RegistryKey being manipulated is closed (closed keys cannot be accessed).</exception>
public override string ToString()
{
if(CheckHKey())
{
return m_name + " [0x" + m_handle.ToString("X") + "]";
}
else
{
throw new ObjectDisposedException("The RegistryKey being manipulated is closed (closed keys cannot be accessed).");
}
}
#endregion
#region Flush
/// <summary>
/// Writes all the attributes of the specified open registry key into the registry.
/// </summary>
/// <remarks>The Flush method may also write out parts of or all of the other keys.
/// Calling this function excessively can have a negative effect on an application抯 performance.</remarks>
public void Flush()
{
//flush changes to memory
int result = RegFlushKey(m_handle);
}
#endregion
#region Close
/// <summary>
/// Closes the key and flushes it to storage if the contents have been modified.
/// </summary>
/// <remarks>Calling this method on system keys will have no effect, since system keys should never be closed.
/// This method does nothing if you call it on an instance of <b>RegistryKey</b> that is already closed.</remarks>
public void Close()
{
if(m_isroot)
{
//we do not close root keys - because they can not be reopened
//close will fail silently - no exception is raised
}
else
{
//ignore if already closed
if(CheckHKey())
{
//close the key
int result = RegCloseKey(m_handle);
if(result==0)
{
//set handle to invalid value
m_handle = 0;
}
else
{
//error occured
throw new ExternalException("Error closing RegistryKey");
}
}
}
}
#endregion
#region Create SubKey
/// <summary>
/// Creates a new subkey or opens an existing subkey.
/// The string subKey is not case-sensitive.
/// </summary>
/// <param name="subkey">Name or path of subkey to create or open.</param>
/// <returns>Returns the subkey, or null if the operation failed.</returns>
/// <exception cref="System.ArgumentNullException">The specified subkey is null.</exception>
/// <exception cref="System.ArgumentException">The length of the specified subkey is longer than the maximum length allowed (255 characters).</exception>
/// <exception cref="System.ObjectDisposedException">The RegistryKey on which this method is being invoked is closed (closed keys cannot be accessed).</exception>
public RegistryKey CreateSubKey(string subkey)
{
//check handle is valid
if(CheckHKey())
{
//check subkey is not null
if(subkey!=null)
{
//check subkey length
if(subkey.Length < 256)
{
//handle to new registry key
uint newhandle = 0;
//key disposition - did this create a new key or open an existing key
uint kdisp = 0;
//create new key
int result = RegCreateKeyEx(m_handle, subkey, 0, null, 0, 0, IntPtr.Zero, ref newhandle, ref kdisp);
if(result==0)
{
//success return the new key
return new RegistryKey(newhandle, m_name + "\\" + subkey, m_writable, false);
}
else
{
throw new ExternalException("An error occured creating the registry key.");
}
}
else
{
//name is more than 255 chars
throw new ArgumentException("The length of the specified subkey is longer than the maximum length allowed (255 characters).");
}
}
else
{
throw new ArgumentNullException("The specified subkey is null.");
}
}
else
{
//registry key is closed
throw new ObjectDisposedException("The RegistryKey on which this method is being invoked is closed (closed keys cannot be accessed).");
}
}
#endregion
#region Open SubKey
/// <summary>
/// Retrieves a subkey as read-only.
/// </summary>
/// <param name="name">Name or path of subkey to open.</param>
/// <returns>The subkey requested, or null if the operation failed.</returns>
public RegistryKey OpenSubKey(string name)
{
return OpenSubKey(name, false);
}
/// <summary>
/// Retrieves a specified subkey.
/// </summary>
/// <param name="name">Name or path of subkey to open.</param>
/// <param name="writable">Set to true if you need write access to the key.</param>
/// <returns>The subkey requested, or null if the operation failed.</returns>
/// <exception cref="System.ArgumentNullException">name is null.</exception>
/// <exception cref="System.ArgumentException">The length of the specified subkey is longer than the maximum length allowed (255 characters).</exception>
/// <exception cref="System.ObjectDisposedException">The RegistryKey being manipulated is closed (closed keys cannot be accessed).</exception>
public RegistryKey OpenSubKey(string name, bool writable)
{
//check handle is valid
if(CheckHKey())
{
//check name is not null
if(name!=null)
{
//check length
if(name.Length < 256)
{
//handle to receive new key
uint newhandle = 0;
int result = RegOpenKeyEx(m_handle, name, 0, 0, ref newhandle);
if(result==0)
{
return new RegistryKey(newhandle, m_name + "\\" + name, writable, false);
}
else
{
//desktop model returns null of key not found
return null;
//throw new ExternalException("An error occured retrieving the registry key");
}
}
else
{
throw new ArgumentException("The length of the specified subkey is longer than the maximum length allowed (255 characters).");
}
}
else
{
throw new ArgumentNullException("name is null.");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -