⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 registry.cs

📁 功能:基于windows mobile 的地图查看器。使用vs2005开发
💻 CS
字号:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;

namespace PInvokeLibrary
{
	/// <summary>
	/// Provides access to the device's registry.
	/// </summary>
	public class Registry
	{
		/// <summary>
		/// Creates a registry key.
		/// </summary>
		/// <param name="keyName">Name of the key to be created.</param>
		/// <returns>ERROR_SUCCESS if successful</returns>
		public static int CreateKey(string keyName)
		{
			UIntPtr hkey = UIntPtr.Zero;
			uint disposition = 0;

			try
			{
				return RegCreateKeyEx(HKCU, keyName, 0, null, 0,
					KeyAccess.None, IntPtr.Zero, ref hkey, ref disposition);
			}
			finally
			{
				if (UIntPtr.Zero != hkey)
				{
					RegCloseKey(hkey);
				}
			}
		}

		/// <summary>
		/// Deletes a registry key.
		/// </summary>
		/// <param name="keyName">Name of key</param>
		/// <returns>ERROR_SUCCESS if successful</returns>
		public static int DeleteKey(string keyName)
		{
			return RegDeleteKey(HKCU, keyName);
		}

		/// <summary>
		/// Create a string value in the specified registry key
		/// </summary>
		/// <param name="keyName">Name of key</param>
		/// <param name="valueName">Name of value</param>
		/// <param name="stringData">Value data</param>
		/// <returns>ERROR_SUCCESS if successful</returns>
		public static int CreateValueString(string keyName, string valueName, string stringData)
		{
			UIntPtr hkey = UIntPtr.Zero;

			try
			{
				int result = RegOpenKeyEx(HKCU, keyName, 0, KeyAccess.None, ref hkey);
				if (ERROR_SUCCESS != result)
					return result;
                    
				byte[] bytes = Encoding.Unicode.GetBytes(stringData);
                    
				return RegSetValueEx(hkey, valueName, 0, KeyType.String,
					bytes, (uint)bytes.Length);
			}
			finally
			{
				if (UIntPtr.Zero != hkey)
				{
					RegCloseKey(hkey);
				}
			}
		}

		/// <summary>
		/// Create a DWORD value in the specified registry key
		/// </summary>
		/// <param name="keyName">Name of key</param>
		/// <param name="valueName">Name of value</param>
		/// <param name="dwordData">Value data</param>
		/// <returns>ERROR_SUCCESS if successful</returns>
		public static int CreateValueDWORD(string keyName, string valueName, uint dwordData)
		{
			UIntPtr hkey = UIntPtr.Zero;

			try
			{
				int result = RegOpenKeyEx(HKCU, keyName, 0, KeyAccess.None, ref hkey);
				if (ERROR_SUCCESS != result)
					return result;

				byte[] bytes = BitConverter.GetBytes(dwordData);
				return RegSetValueEx(hkey, valueName, 0, KeyType.Dword,
					bytes, (uint)bytes.Length);
			}
			finally
			{
				if (UIntPtr.Zero != hkey)
				{
					RegCloseKey(hkey);
				}
			}
		}

		/// <summary>
		/// Get the specified string value registry entry
		/// </summary>
		/// <param name="keyName">Key name</param>
		/// <param name="valueName">Value name</param>
		/// <param name="stringResult">string data</param>
		/// <returns>ERROR_SUCCESS if successful</returns>
		public static int GetStringValue(string keyName, string valueName, ref string stringResult)
		{
			UIntPtr hkey = UIntPtr.Zero;

			try
			{
				int result = RegOpenKeyEx(HKCU, keyName, 0, KeyAccess.None, ref hkey);
				if (ERROR_SUCCESS != result)
					return result;

				byte[] bytes = null;
				uint length = 0;
				KeyType keyType = KeyType.None;
                    
				result = RegQueryValueEx(hkey, valueName, IntPtr.Zero, ref keyType,
					null, ref length);

				if (ERROR_SUCCESS != result)
					return result;

				keyType = KeyType.None;
				bytes = new byte[length];

				result = RegQueryValueEx(hkey, valueName, IntPtr.Zero, ref keyType,
					bytes, ref length);

				if (ERROR_SUCCESS != result)
					return result;

				stringResult = Encoding.Unicode.GetString(bytes, 0, bytes.Length);

				return ERROR_SUCCESS;
			}
			finally
			{
				if (UIntPtr.Zero != hkey)
				{
					RegCloseKey(hkey);
				}
			}
		}

		/// <summary>
		/// Get the specified DWORD value registry entry.
		/// </summary>
		/// <param name="keyName">Key name</param>
		/// <param name="valueName">Value name</param>
		/// <param name="dwordResult">Value data</param>
		/// <returns>ERROR_SUCCESS if successful</returns>
		public static int GetDWORDValue(string keyName, string valueName, ref uint dwordResult)
		{
			UIntPtr hkey = UIntPtr.Zero;

			try
			{
				int result = RegOpenKeyEx(HKCU, keyName, 0, KeyAccess.None, ref hkey);
				if (ERROR_SUCCESS != result)
					return result;

				byte[] bytes = null;
				uint length = 0;
				KeyType keyType = KeyType.None;
                    
				result = RegQueryValueEx(hkey, valueName, IntPtr.Zero, ref keyType,
					null, ref length);

				bytes = new byte[Marshal.SizeOf(typeof(uint))];
				length = (uint)bytes.Length;
				keyType = KeyType.None;
                    
				result = RegQueryValueEx(hkey, valueName, IntPtr.Zero, ref keyType,
					bytes, ref length);

				if (ERROR_SUCCESS != result)
					return result;

				dwordResult = BitConverter.ToUInt32(bytes, 0);

				return ERROR_SUCCESS;
			}
			finally
			{
				if (UIntPtr.Zero != hkey)
				{
					RegCloseKey(hkey);
				}
			}
		}
  
		/// <summary>
		/// Delete the specified value form the registry key.
		/// </summary>
		/// <param name="keyName">Key name</param>
		/// <param name="valueName">Value name</param>
		/// <returns>ERROR_SUCCESS if successful</returns>
		public static int DeleteValue(string keyName, string valueName)
		{
			UIntPtr hkey = UIntPtr.Zero;

			try
			{
				int result = RegOpenKeyEx(HKCU, keyName, 0, KeyAccess.None, ref hkey);
				if (ERROR_SUCCESS != result)
					return result;

				return RegDeleteValue(hkey, valueName);
			}
			finally
			{
				if (UIntPtr.Zero != hkey)
				{
					RegCloseKey(hkey);
				}
			}
		}

		/// <summary>
		/// Key value types
		/// </summary>
		public enum KeyType : uint
		{
			None = 0,
			String = 1,
			Dword = 4,
		}

		/// <summary>
		/// Key access types
		/// </summary>
		public enum KeyAccess : uint
		{
			None = 0x0000,
			QueryValue = 0x0001,
			SetValue   = 0x0002,
			CreateSubKey = 0x0004,
			EnumerateSubKeys = 0x0008,
			Notify = 0x0010,
			CreateLink = 0x0020
		}

		/// <summary>
		/// HKEY_CLASSES_ROOT
		/// </summary>
		public static UIntPtr HKCR = new UIntPtr(0x80000000);
		/// <summary>
		/// HKEY_CURRENT_USER
		/// </summary>
		public static UIntPtr HKCU = new UIntPtr(0x80000001);
		/// <summary>
		/// HKEY_LOCAL_MACHINE
		/// </summary>
		public static UIntPtr HKLM = new UIntPtr(0x80000002);

		/// <summary>
		/// HKEY_USERS
		/// </summary>
		public static UIntPtr HKU = new UIntPtr(0x80000003);

		/// <summary>
		/// Successful return value from Registry API
		/// </summary>
		public const int ERROR_SUCCESS = 0;

		/// <summary>
		/// This function creates the specified key. If the key already exists in
		/// the registry, the function opens it. A remote application interface
		/// (RAPI) version of this function exists, and it is called
		/// CeRegCreateKeyEx.
		/// </summary>
		/// <param name="hkey">[in] Handle to a currently open key or one of:
		/// HKCR, HKCU, HKLM.</param>
		/// <param name="lpSubKey">[in] Pointer to a null-terminated string specifying
		/// the name of a subkey that this function opens or creates. The subkey
		/// specified must be a subkey of the key identified by the hKey parameter.
		/// This subkey must not begin with the backslash character (慭

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -