Home
Manage Your Code
Snippet: Class for write and read Strings to Registry (C#)
Title: Class for write and read Strings to Registry Language: C#
Description: This is a class for Registryaccess, so far only string values are supported. Views: 284
Author: Frank Blau Date Added: 7/31/2008
Copy Code  
1  /// <summary>
2  /// Class for Registry String Access.
3  /// </summary>
4  class CREGISTRYACCESS
5  {
6    // Konstanten
7    private const string SOFTWARE_KEY = "Software";
8    private const string COMPANY_NAME = "Florianus";
9    private const string APPLICATION_NAME = "Protec";
10    /// <summary>
11    /// Method to read string value.
12    /// </summary>
13    /// <param name="sKey">The Key value.</param>
14    /// <param name="sDefaultValue">Standard value.</param>
15    /// <returns>the value</returns>
16    static public string GetStringRegistryValue(string sKey, string sDefaultValue)
17    {
18      RegistryKey rkCompany;
19      RegistryKey rkApplication;
20      rkCompany = Registry.CurrentUser.OpenSubKey(SOFTWARE_KEY, false).OpenSubKey(COMPANY_NAME, false);
21      if (rkCompany != null)
22      {
23        rkApplication = rkCompany.OpenSubKey(APPLICATION_NAME, true);
24        if (rkApplication != null)
25        {
26          foreach (string sRKey in rkApplication.GetValueNames())
27          {
28            if (sRKey == sKey)
29            {
30              return (string)rkApplication.GetValue(sRKey);
31            }
32          }
33        }
34      }
35      return sDefaultValue;
36    }
37    /// <summary>
38    /// Method to write string value.
39    /// </summary>
40    /// <param name="sKey">The Key.</param>
41    /// <param name="sStringValue">The value.</param>
42    static public void SetStringRegistryValue(string sKey, string sStringValue)
43    {
44      RegistryKey rkSoftware;
45      RegistryKey rkCompany;
46      RegistryKey rkApplication;
47
48      rkSoftware = Registry.CurrentUser.OpenSubKey(SOFTWARE_KEY, true);
49      rkCompany = rkSoftware.CreateSubKey(COMPANY_NAME);
50
51      if (rkCompany != null)
52      {
53        rkApplication = rkCompany.CreateSubKey(APPLICATION_NAME);
54        if (rkApplication != null)
55        {
56          rkApplication.SetValue(sKey, sStringValue);
57        }
58      }
59    }
60  }
Notes
Namespace in use: using Microsoft.Win32; using System; using System.Collections.Generic; using System.Text;