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

📄 registry.cs

📁 蓝牙通讯
💻 CS
📖 第 1 页 / 共 3 页
字号:
		{
			DeleteValue(name, true);
		}
		/// <summary>
		/// Deletes the specified value from this key.
		/// </summary>
		/// <param name="name">Name of the value to delete.</param>
		/// <param name="throwOnMissingValue">Indicates whether an exception should be raised if the specified value cannot be found.
		/// If this argument is true and the specified value does not exist then an exception is raised.
		/// If this argument is false and the specified value does not exist, then no action is taken</param>
		/// <exception cref="System.ArgumentException">name is not a valid reference to a value (and throwOnMissingValue is true) or name is null</exception>
		/// <exception cref="System.ObjectDisposedException">The RegistryKey being manipulated is closed (closed keys cannot be accessed).</exception>
		/// <exception cref="System.UnauthorizedAccessException">The RegistryKey being manipulated is readonly.</exception>
		public void DeleteValue(string name, bool throwOnMissingValue)
		{
			if(m_writable)
			{
				if(CheckHKey())
				{
					if(name!=null)
					{
						//call api function to delete value
						int result = RegDeleteValue(m_handle, name);

						//check for error in supplied name
						if(result==87)
						{
							//only throw exception if flag is set
							if(throwOnMissingValue)
							{
								//name doesnt exist
								throw new ArgumentException("name is not a valid reference to a value (and throwOnMissingValue is true) or name is null");
							}
						}
					}
					else
					{
						//name is null
						throw new ArgumentException("name is null");
					}
				}
				else
				{
					//handle is closed throw exception
					throw new ObjectDisposedException("The RegistryKey being manipulated is closed (closed keys cannot be accessed).");
				}
			}
			else
			{
				//key is readonly throw exception
				throw new UnauthorizedAccessException("Cannot delete a value from a RegistryKey opened as ReadOnly.");
			}

		}
		#endregion

		#region Get Value Names
		/// <summary>
		/// Retrieves an array of strings that contains all the value names associated with this key.
		/// </summary>
		/// <returns>An array of strings that contains the value names for the current key.</returns>
		/// <remarks>If no value names for the key are found, an empty array is returned.
		/// <para>All RegistryKeys are assigned a default value.
		/// This is not counted as a value name, and is not returned as part of the result set.</para></remarks>
		/// <exception cref="System.ObjectDisposedException">The RegistryKey being manipulated is closed (closed keys cannot be accessed).</exception>
		public string[] GetValueNames()
		{
			if(CheckHKey())
			{
				int result = 0;
				//store the names
				ArrayList valuenames = new ArrayList();
				int index = 0;
				//buffer to store the name
				char[] buffer = new char[256];
				int valuenamelen = buffer.Length;

				//get first value name
				result = RegEnumValue(m_handle, index, buffer, ref valuenamelen, 0, 0, null, 0);
				
				//enumerate sub keys
				while(result != ERROR_NO_MORE_ITEMS)
				{
					//add the name to the arraylist
					valuenames.Add(new string(buffer, 0, valuenamelen));
					//increment index
					index++;
					//reset length available to max
					valuenamelen = buffer.Length;

					//get next value name
					result = RegEnumValue(m_handle, index, buffer, ref valuenamelen, 0, 0, null, 0);
				}

				//sort the results
				valuenames.Sort();
				
				//return a fixed size string array
				return (string[])valuenames.ToArray(typeof(string));
			}
			else
			{
				//key is closed
				throw new ObjectDisposedException("The RegistryKey being manipulated is closed (closed keys cannot be accessed).");
			}
		}
		#endregion

		#region Value Count
		/// <summary>
		/// Retrieves the count of values in the key.
		/// </summary>
		/// <exception cref="System.ObjectDisposedException"> The RegistryKey being manipulated is closed (closed keys cannot be accessed).</exception>
		public int ValueCount
		{
			get
			{
				//check handle
				if(CheckHKey())
				{
					int subkeycount;
					int valuescount;
					int maxsubkeylen;
					int maxsubkeyclasslen;
					int maxvalnamelen;
					int maxvallen;
					char[] name = new char[256];
					int namelen = name.Length;

					if(RegQueryInfoKey(m_handle, name, ref namelen, 0, out subkeycount, out maxsubkeylen, out maxsubkeyclasslen, out valuescount, out maxvalnamelen, out maxvallen, 0, 0)==0)
					{
						return valuescount;
					}
					else
					{
						throw new ExternalException("Error retrieving registry properties");
					}
				}
				else
				{
					throw new ObjectDisposedException("The RegistryKey being manipulated is closed (closed keys cannot be accessed).");
				}
			}
		}
		#endregion


		#region Check HKey
		//used to check that the handle is a valid open hkey
		private bool CheckHKey()
		{
			if(m_handle==0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		#endregion

		#region IDisposable Members
		/// <summary>
		/// Free up resources used by the RegistryKey
		/// </summary>
		public void Dispose()
		{
			//close and save out data
			this.Close();
		}
		#endregion

		#region KeyType Enum
		// <summary>
		// Key disposition for RegCreateKey(Ex)
		// </summary>
		/*private enum KeyDisposition : int 
		{
			REG_CREATED_NEW_KEY = 1, 
			REG_OPENED_EXISTING_KEY = 2 
		} */

		/// <summary>
		/// Key type for RegCreateKey(Ex)
		/// </summary>
		private enum KeyType : int
		{
			//String
			String = 1,
			ExpandString = 2,
			//binary data (byte[])
			Binary = 3,
			//dword (UInt32)
			DWord = 4,
			//Multi String
			MultiString = 7,
		}
		#endregion

		#region Registry P/Invokes

		//open key
		[DllImport("coredll.dll", EntryPoint="RegOpenKeyEx", SetLastError=true)] 
		private static extern int RegOpenKeyEx(
			uint hKey,
			string lpSubKey,
			int ulOptions,
			int samDesired,
			ref uint phkResult); 

		//create key
		[DllImport("coredll.dll", EntryPoint="RegCreateKeyEx", SetLastError=true)] 
		private static extern int RegCreateKeyEx(
			uint hKey,
			string lpSubKey,
			int lpReserved,
			string lpClass,
			int dwOptions,
			int samDesired,
			IntPtr lpSecurityAttributes,
			ref uint phkResult, 
			ref uint lpdwDisposition); 

		//enumerate keys
		[DllImport("coredll.dll", EntryPoint="RegEnumKeyEx", SetLastError=true)]
		private static extern int RegEnumKeyEx(
			uint hKey,
			int iIndex, 
			char[] sKeyName,
			ref int iKeyNameLen, 
			int iReservedZero,
			byte[] sClassName,
			int iClassNameLenZero, 
			int iFiletimeZero);

		//enumerate values
		[DllImport("coredll.dll", EntryPoint="RegEnumValue", SetLastError=true)]
		private static extern int RegEnumValue(
			uint hKey,
			int iIndex,
			char[] sValueName, 
			ref int iValueNameLen,
			int iReservedZero,
			int iTypeZero, /*should take ref KeyType but we never want to restrict type when enumerating values*/
			byte[] byData,
			int iDataLenZero /*takes ref int but we dont need the value when enumerating the names*/);

		//query key info
		[DllImport("coredll.dll", EntryPoint="RegQueryInfoKey", SetLastError=true)]
		private static extern int RegQueryInfoKey(
			uint hKey,
			char[] lpClass,
			ref int lpcbClass, 
			int reservedZero,
			out int cSubkey, 
			out int iMaxSubkeyLen,
			out int lpcbMaxSubkeyClassLen,
			out int cValueNames,
			out int iMaxValueNameLen, 
			out int iMaxValueLen,
			int securityDescriptorZero,
			int lastWriteTimeZero);

		//get value
		[DllImport("coredll.dll", EntryPoint="RegQueryValueEx", SetLastError=true)] 
		private static extern int RegQueryValueEx(
			uint hKey,
			string lpValueName,
			int lpReserved, 
			ref KeyType lpType,
			byte[] lpData,
			ref int lpcbData); 

		//set value
		[DllImport("coredll.dll", EntryPoint="RegSetValueExW", SetLastError=true)] 
		private static extern int RegSetValueEx(
			uint hKey,
			string lpValueName,
			int lpReserved, 
			KeyType lpType,
			byte[] lpData,
			int lpcbData); 

		//close key
		[DllImport("coredll.dll", EntryPoint="RegCloseKey", SetLastError=true)] 
		private static extern int RegCloseKey(
			uint hKey);

		//delete key
		[DllImport("coredll.dll", EntryPoint="RegDeleteKey", SetLastError=true)]
		private static extern int RegDeleteKey(
			uint hKey,
			string keyName);

		//delete value
		[DllImport("coredll.dll", EntryPoint="RegDeleteValue", SetLastError=true)]
		private static extern int RegDeleteValue(
			uint hKey,
			string valueName);

		//flush key
		[DllImport("coredll.dll", EntryPoint="RegFlushKey", SetLastError=true)]
		private static extern int RegFlushKey(
			uint hKey );


		#endregion
	}
	#endregion
}

⌨️ 快捷键说明

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