Code C# – Thao tác với Registry của windows

Lớp myRegistry bên dưới cung cấp cho ta các hàm thao tác với Registry của windows như:

  1. Đọc giá trị của 1 key.
  2. Đăng ký key và gi giá trị của key vào registry.
  3. Xóa key đã đăng ký.
public sealed class myRegistry
    {
        /// <summary>
        /// Method is used to read value from Registry.
        /// </summary>
        /// <param name="SubKey">Subkey which contains key to be select.</param>
        /// <param name="KeyName">Key which value will be get.</param>
        /// <returns>Value of string</returns>
        public static string Read(string SubKey, string KeyName)
        {
            // Base registry key.
            RegistryKey baseRegistryKey = Microsoft.Win32.Registry.LocalMachine;
            // Opening the registry key
            RegistryKey rk = baseRegistryKey;
            // Open a subKey as read-only
            RegistryKey sk1 = rk.CreateSubKey(SubKey);
            try
            {
                // If the RegistryKey exists I get its value or null is returned.
                return (string)sk1.GetValue(KeyName.ToUpper());
            }
            catch { return null; }
            finally
            {
                if (sk1 != null)
                {
                    sk1.Close();
                }
            }
        }
        /// <summary>
        /// Method is used to write value to Registry.
        /// </summary>
        /// <param name="SubKey">Subkey which contains key to be select.</param>
        /// <param name="KeyName">Key which value will be inserted.</param>
        /// <param name="Value">Value to be write.</param>
        /// <returns></returns>
        public static bool Write(string SubKey, string KeyName, object Value)
        {
            RegistryKey baseRegistryKey = Microsoft.Win32.Registry.LocalMachine;
            // Setting
            RegistryKey rk = baseRegistryKey;
            // Check if there is no SubKey, create sub key.
            RegistryKey sk1 = null;
            try
            {
                sk1 = rk.CreateSubKey(SubKey);
                // Save the value
                sk1.SetValue(KeyName.ToUpper(), Value);
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                if (sk1 != null)
                {
                    sk1.Close();
                }
                rk.Close();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="SubKey"></param>
        /// <param name="KeyName"></param>
        /// <returns></returns>
        public static bool DeleteKey(string SubKey, string KeyName)
        {
            try
            {
                RegistryKey baseRegistryKey = Microsoft.Win32.Registry.LocalMachine;
                // Setting
                RegistryKey rk = baseRegistryKey;
                // Check if there is no SubKey, create sub key.
                RegistryKey sk1 = null;
                sk1 = rk.OpenSubKey(SubKey);
                sk1 = rk.CreateSubKey(SubKey);
                // Delete key.
                sk1.DeleteValue(KeyName);
                return true;
            }
            catch 
            {
                return false;
            }
        }
    }

nosomovo

Chia sẻ lên
WEB5k - Thiết kế website giá rẻ chuẩn SEO