Que pensez vous de la nouvelle version de Code-Source.NET ?

Choices

Read and write to the registry in C #

Read and write to the registry in C #

Member for

3 years 6 months

Sometimes we need to record information at the registry level.

Here is a sample code in C # that writes and reads from the registry.

using Microsoft.Win32;
using System;

namespace ReadWriteRegistry
{
    class Program
    {
        static void Main(string[] args)
       {
            RegistryKey RegistryWriting = Registry.CurrentUser;
            // Create a key on the registry 
            RegistryWriting = RegistryWriting.CreateSubKey(@"Code-Source.NET");
            RegistryWriting.SetValue("SiteName","Code-Source.NET");
            RegistryWriting.SetValue("SiteURL", "http://www.code-source.net");
            RegistryWriting.SetValue("Nombre",100);
            RegistryWriting.Close();
            RegistryKey RegistryReading = Registry.CurrentUser;
            // Open a registry key 
            RegistryReading = RegistryReading.OpenSubKey(@"Code-Source.NET");
            Object valeurSiteName = RegistryReading.GetValue("SiteName");
            Object valeurSiteURL = RegistryReading.GetValue("SiteURL");
            Object valeurNombre = RegistryReading.GetValue("Nombre");
            RegistryReading.Close();
            if ((string)valeurSiteName == "Code-Source.NET")
                Console.WriteLine("SiteName : {0}\nSiteURL : {1}\nNombre : {2}", valeurSiteName, valeurSiteURL, valeurNombre);
            else Console.WriteLine("Error when reading registry");
        }
    }
}