Csharp/C Sharp/Windows/Registry — различия между версиями

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

Текущая версия на 11:42, 26 мая 2010

Enumerating Registry Keys

using System;
using Microsoft.Win32;
   
  class Class1
  {
    static void Main(string[] args)
    {
      RegistryKey myRegKey=Registry.LocalMachine;
      myRegKey=myRegKey.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
      String [] subkeyNames = myRegKey.GetSubKeyNames();
      foreach (String s in subkeyNames) {
        RegistryKey UninstallKey=Registry.LocalMachine;
        UninstallKey=UninstallKey.OpenSubKey ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + s);
        Object oValue=UninstallKey.GetValue("DisplayName");
        Console.WriteLine(oValue.ToString());
      }
    }
  }


Get registry value from LocalMachine key

using System;
using System.Windows.Forms;
using Microsoft.Win32;
public class FormSettingStore {
    public static void Main(){
        RegistryKey key = Registry.LocalMachine.CreateSubKey("KeyName");
        key.SetValue("Height", "100");
        key.SetValue("Width", "100");
        key.SetValue("Left", "100");
        key.SetValue("Top", "100");
        Console.WriteLine((int)key.GetValue("Height", "150"));
        Console.WriteLine((int)key.GetValue("Width",  "150"));
        Console.WriteLine((int)key.GetValue("Left",  "150"));
        Console.WriteLine((int)key.GetValue("Top",  "150"));
    }
}


Read value from registry

  using System;
  using System.Drawing;
  using System.Collections;
  using System.Windows.Forms;
  using System.Resources;
  using Microsoft.Win32;
  // For event log.
  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 regKey1 = Registry.CurrentUser;
      regKey1 = regKey1.CreateSubKey("Software\\Intertech\\YourKeyRes");
      Console.WriteLine(regKey1.GetValue("CurrSize", "30"));
      Console.WriteLine(regKey1.GetValue("CurrColor", "Blue"));
    }
  }


Retrieve the CPU Type and Speed from the Registry

using System;
using Microsoft.Win32;
   
  class Class1
  {
    static void Main(string[] args)
    {
      RegistryKey RegKey = Registry.LocalMachine;
      RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
      Object cpuSpeed = RegKey.GetValue("~MHz");
      Object cpuType  = RegKey.GetValue("VendorIdentifier");
      Console.WriteLine("You have a {0} running at {1} MHz.",cpuType,cpuSpeed);
    }
  }


Save value to registery

  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");
    }
  }


Write a Text and DWord Value to the Registry

using System;
using Microsoft.Win32;
   
  class Class1
  {
    static void Main(string[] args)
    {
      RegistryKey RegKeyWrite = Registry.CurrentUser;
      RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\CSHARP\\WriteRegistryValue");
      RegKeyWrite.SetValue("StringValue","TRUE");
      RegKeyWrite.SetValue("Number",100);
      RegKeyWrite.Close();
   
      RegistryKey RegKeyRead = Registry.CurrentUser;
      RegKeyRead = RegKeyRead.OpenSubKey  ("Software\\CSHARP\\WriteRegistryValue");
      Object regSuccessful = RegKeyRead.GetValue("StringValue");
      Object regAttemptNumber = RegKeyRead.GetValue("Number");
      RegKeyRead.Close();
   
      if ((string)regSuccessful == "TRUE")
        Console.WriteLine("Succeeded on attempt # {0}",regAttemptNumber);
      else
        Console.WriteLine("Failed!");
    }
  }