Csharp/CSharp Tutorial/Windows/Registry Save

Материал из .Net Framework эксперт
Версия от 12:20, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

delete a key from Registry

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

public class MainClass{
    static void Main(string[] args){
         string strKey;
         string strAns;
         Console.WriteLine("Enter the key under HKEY_CURRENT_USER to delete");
         strKey = Console.ReadLine();
         Console.WriteLine("Do you really mean to delete: " + strKey + " and its settings?");
         strAns = Console.ReadLine();
         strAns = strAns.ToUpper();
         if (strAns == "YES" | strAns == "Y")
         {
               Registry.CurrentUser.DeleteSubKeyTree(strKey);
               Console.WriteLine("Key " + strKey + " deleted");
         }
         else
         {
               Console.WriteLine("No deletion performed");
         }
    }
}

Delete a setting in Registry

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

public class MainClass{
    static void Main(string[] args){
        string strKey;
        string strSetting;
        string strAns;
        Console.WriteLine("Enter the key");
        strKey = Console.ReadLine();
        RegistryKey TestProject = Registry.CurrentUser.OpenSubKey(strKey, true);
        Console.WriteLine("Enter the setting");
        strSetting = Console.ReadLine();
        Console.WriteLine("Do you really mean to delete: " + strKey + "\\" + strSetting + "?");
        strAns = Console.ReadLine();
        strAns = strAns.ToUpper();
        if (strAns == "Y" | strAns == "YES")
        {
             TestProject.DeleteValue(strSetting);
             Console.WriteLine("Key setting " + strKey + "\\" + strSetting + " deleted");
        }
        else
        {
             Console.WriteLine("No deletion performed; key closed");
             TestProject.Close();
        }
    }
}

Save int value to Registry

using System;
using Microsoft.Win32;
class MainClass
{
    public static void Main(String[] args)
    {
        Registry.SetValue(
            @"HKEY_CURRENT_USER\Software\CompanyName\SoftwareName",
            "IntValueName", 101, RegistryValueKind.DWord);
    }
}

Save user prefs to registry

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

Set DateTime value to the Registry

using System;
using Microsoft.Win32;
class MainClass
{
    public static void Main(String[] args)
    {
        Registry.SetValue(
            @"HKEY_CURRENT_USER\Software\CompanyName\SoftwareName",
            "DateTimeValue", DateTime.Now.ToString(), RegistryValueKind.String);
    }
}

Set String value to the Registry

using System;
using Microsoft.Win32;
class MainClass
{
    public static void Main(String[] args)
    {
        Registry.SetValue(
            @"HKEY_CURRENT_USER\Software\CompanyName\SoftwareName",
            "User", Environment.UserName, RegistryValueKind.String);
    }
}