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


Read and write to the registry in C #

Read and write to the registry in C #

Member for

3 years 4 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("SiteURL", "http://www.code-source.net");
            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");
            if ((string)valeurSiteName == "Code-Source.NET")
                Console.WriteLine("SiteName : {0}\nSiteURL : {1}\nNombre : {2}", valeurSiteName, valeurSiteURL, valeurNombre);
            else Console.WriteLine("Error when reading registry");