Csharp/C Sharp by API/Microsoft.Win32/RegistryKey

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

RegistryKey.Close()

  
using System;
using Microsoft.Win32;
class MainClass
{
   public static void Main()
   {
      RegistryKey start = Registry.LocalMachine;
      string DNSservers = @"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters";
      RegistryKey DNSserverKey = start.OpenSubKey(DNSservers);
      if (DNSserverKey == null)
      {
         Console.WriteLine("Unable to open DNS servers key");
         return;
      }
  
      string serverlist = (string)DNSserverKey.GetValue("NameServer");
      Console.WriteLine("DNS Servers: {0}", serverlist);
      DNSserverKey.Close();
      start.Close();
   }
}


RegistryKey.CreateSubKey(String value)

 
  using System;
  using System.Resources;
  using Microsoft.Win32;
  using System.Diagnostics;
  class Test
  {
    static void Main(string[] args)
    {
      // Save user prefs to reg.
      RegistryKey regKey = Registry.CurrentUser;
      regKey = regKey.CreateSubKey("Software\\Intertech\\YourKeyRes");
      regKey.SetValue("CurrSize", "29");
      regKey.SetValue("CurrColor", "Red");
      Console.WriteLine("Settings saved in registry");
    }
  }


RegistryKey.GetSubKeyNames()

  
using System;
using Microsoft.Win32;
class MainClass
{
    public static void SearchSubKeys(RegistryKey root, String searchKey)
    {
        
        foreach (string keyname in root.GetSubKeyNames())
        {
            try
            {
                using (RegistryKey key = root.OpenSubKey(keyname))
                {
                    if (keyname == searchKey) 
                        Console.WriteLine("Registry key found : {0} contains {1} values",
                            key.Name, key.ValueCount);
                       
                    SearchSubKeys(key, searchKey);
                }
            }
            catch (System.Security.SecurityException)
            {
            }
        }
    }
    public static void Main(String[] args)
    {
        using (RegistryKey root = Registry.CurrentUser)
        {
            string myKey="Java";
            SearchSubKeys(root, myKey);
        }
    }
}


RegistryKey.GetValue

  

using System;
using Microsoft.Win32;
class MainClass
{
   public static void Main()
   {
      RegistryKey start = Registry.LocalMachine;
      string DNSservers = @"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters";
      RegistryKey DNSserverKey = start.OpenSubKey(DNSservers);
      if (DNSserverKey == null)
      {
         Console.WriteLine("Unable to open DNS servers key");
         return;
      }
  
      string serverlist = (string)DNSserverKey.GetValue("NameServer");
      Console.WriteLine("DNS Servers: {0}", serverlist);
      DNSserverKey.Close();
      start.Close();
   }
}


RegistryKey.OpenSubKey

  
using System;
using Microsoft.Win32;
class MainClass
{
    public static void SearchSubKeys(RegistryKey root, String searchKey)
    {
        foreach (string keyname in root.GetSubKeyNames())
        {
            try
            {
                using (RegistryKey key = root.OpenSubKey(keyname))
                {
                    if (keyname == searchKey) {
                        foreach (string valuename in key.GetValueNames())
                        {
                            if (key.GetValue(valuename) is String)
                            {
                                Console.WriteLine("  Value : {0} = {1}",
                                    valuename, key.GetValue(valuename));
                            }
                        }
                    }
                    SearchSubKeys(key, searchKey);
                }
            }
            catch (System.Security.SecurityException)
            {
            }
        }
    }
    public static void Main(String[] args)
    {
        using (RegistryKey root = Registry.CurrentUser)
        {
            string myKey="Java";
            SearchSubKeys(root, myKey);
        }
    }
}


RegistryKey.SetValue

  
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32;
using System.Diagnostics;
public class MainClass{
   public static void Main(){
    RegistryKey regKey = Registry.CurrentUser;
    regKey = regKey.CreateSubKey("Software\\YourSoftwareName\\SubDomain");
    regKey.SetValue("CurrSize", 10);
    regKey.SetValue("CurrColor", "Blue");
   }
}