Click here to Skip to main content
15,891,657 members
Home / Discussions / C#
   

C#

 
QuestionDetection of Facial Feature points in OpenCV Pin
mkfrns29-Dec-10 19:45
mkfrns29-Dec-10 19:45 
AnswerRe: Detection of Facial Feature points in OpenCV Pin
JF201529-Dec-10 21:52
JF201529-Dec-10 21:52 
QuestionReading registry keys Pin
rakeshs31229-Dec-10 18:23
rakeshs31229-Dec-10 18:23 
AnswerRe: Reading registry keys Pin
Abhinav S29-Dec-10 18:31
Abhinav S29-Dec-10 18:31 
AnswerRe: Reading registry keys Pin
RaviRanjanKr29-Dec-10 18:50
professionalRaviRanjanKr29-Dec-10 18:50 
AnswerRe: Reading registry keys Pin
Anil Kumar.Arvapalli29-Dec-10 20:44
Anil Kumar.Arvapalli29-Dec-10 20:44 
GeneralRe: Reading registry keys Pin
PIEBALDconsult30-Dec-10 2:24
mvePIEBALDconsult30-Dec-10 2:24 
AnswerRe: Reading registry keys Pin
Kevin Marois30-Dec-10 5:40
professionalKevin Marois30-Dec-10 5:40 
Here ya go. Paste this code into a class and you're all set:

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

namespace Marois.Common.RegistryProcedures
{
    public static class RegistryProcedures
    {
        #region enum BaseKeys
        public enum BaseKey
        { 
            HKEY_CLASSES_ROOT,
            HKEY_CURRENT_CONFIG,
            HKEY_CURRENT_USER,
            HKEY_DYN_DATA,
            HKEY_LOCAL_MACHINE,
            HKEY_PERFORMANCE_DATA
        }
        #endregion

        #region Method CreateSubkey
        public static void CreateSubkey(BaseKey BaseKey, string KeyToAdd)
        {
            CreateSubkey(BaseKey, "", KeyToAdd);
        }
        public static void CreateSubkey(BaseKey BaseKey, string SubKey, string KeyToAdd)
        {
            RegistryKey baseKey;

            if (SubKey != "")
                baseKey = _GetRegistryBaseKey(BaseKey).OpenSubKey(SubKey, true);
            else
                baseKey = _GetRegistryBaseKey(BaseKey);


            if (baseKey == null && SubKey != "")
            {
                throw new Exception("Registry subkey not found");
            }
            else
            {
                baseKey.CreateSubKey(KeyToAdd);
            }
        }
        #endregion

        #region Method DeleteSubkey
        public static void DeleteSubkey(BaseKey BaseKey, string SubKey)
        {
            RegistryKey baseKey = _GetRegistryBaseKey(BaseKey);
            baseKey.DeleteSubKey(SubKey);
        }
        #endregion

        #region Method DeleteSubkeyTree
        public static void DeleteSubKeyTree(BaseKey BaseKey, string sSubKey)
        {
            RegistryKey baseKey = _GetRegistryBaseKey(BaseKey);
            baseKey.DeleteSubKeyTree(sSubKey);
        }
        #endregion

        #region Method DeleteSubkeyTreeValues
        public static void DeleteSubkeyTreeValues(BaseKey BaseKey, string sSubKey)
        {
            List<string> nodeNames = GetSubKeyNodeNames(BaseKey, sSubKey);

            if(nodeNames.Count > 0)
            {
                RegistryKey baseKey = _GetRegistryBaseKey(BaseKey);
                baseKey = baseKey.OpenSubKey(sSubKey, true);

                foreach(string nodeName in nodeNames)
                {
                    baseKey.DeleteValue(nodeName);                   
                }
            }
        }
        #endregion

        #region Method DeleteValue
        private static void DeleteValue(BaseKey BaseKey, string SubKey, string ValueToDelete)
        {
            RegistryKey baseKey;

            if (SubKey != "")
                baseKey = _GetRegistryBaseKey(BaseKey).OpenSubKey(SubKey, true);
            else
                baseKey = _GetRegistryBaseKey(BaseKey);

            if (baseKey == null && SubKey != "")
            {
                throw new Exception("Registry subkey not found");
            }
            else
            {
                baseKey.DeleteValue(ValueToDelete);
            }
        }
        #endregion Method DeleteValue

        #region Method GetSubkeyNames
        public static List<string> GetSubkeyNames(BaseKey BaseKey, string SubKey)
        {
            List<string> retVal = null;

            RegistryKey baseKey;

            if (SubKey != "")
                baseKey = _GetRegistryBaseKey(BaseKey).OpenSubKey(SubKey);
            else
                baseKey = _GetRegistryBaseKey(BaseKey);


            if (baseKey == null && SubKey != "")
            {
                throw new Exception("Registry subkey not found");
            }
            else
            {
                string[] names = baseKey.GetSubKeyNames();

                retVal = (from n in names
                          select n).ToList();
            }

            return retVal;
        }
        #endregion

        #region Method GetSubKeyNodeNames
        public static List<string> GetSubKeyNodeNames(BaseKey BaseKey, string SubKey)
        {
            List<string> retVal = null;

            RegistryKey baseKey;

            if (SubKey != "")
                baseKey = _GetRegistryBaseKey(BaseKey).OpenSubKey(SubKey);
            else
                baseKey = _GetRegistryBaseKey(BaseKey);


            if (baseKey == null && SubKey != "")
            {
                throw new Exception("Registry subkey not found");
            }
            else
            {
                string[] names = baseKey.GetValueNames();

                retVal = (from n in names
                          select n).ToList();
            }

            return retVal;
        }
        #endregion

        #region Method GetSubKeyValue
        public static object GetSubKeyValue(BaseKey BaseKey, string SubKey, string Node)
        {
            object retVal = null;
            RegistryKey baseKey;

            if (SubKey != "")
                baseKey = _GetRegistryBaseKey(BaseKey).OpenSubKey(SubKey);
            else
                baseKey = _GetRegistryBaseKey(BaseKey);


            if (baseKey == null && SubKey != "")
            {
                throw new Exception("Registry subkey not found");
            }
            else
            {
                retVal = baseKey.GetValue(Node);
            }

            return retVal;
        }
        #endregion

        #region Method SetKeyValue
        public static void SetKeyValue(BaseKey BaseKey, string SubKey, string NodeName, string Value)
        {
            RegistryKey baseKey = _GetRegistryBaseKey(BaseKey);
            baseKey = baseKey.OpenSubKey(SubKey, true);

            if (baseKey == null && SubKey != "")
            {
                throw new Exception("Registry subkey not found");
            }
            else
            {
                baseKey.SetValue(NodeName, Value);
            }
        }
        public static void SetKeyValue(BaseKey BaseKey, string SubKey, string NodeName, int Value)
        {
            RegistryKey baseKey = _GetRegistryBaseKey(BaseKey);
            baseKey = baseKey.OpenSubKey(SubKey, true);


            if (baseKey == null && SubKey != "")
            {
                throw new Exception("Registry subkey not found");
            }
            else
            {
                baseKey.SetValue(NodeName, Value);
            }
        }
        #endregion

        #region Method _GetRegistryBaseKey
        private static RegistryKey _GetRegistryBaseKey(BaseKey BaseKey)
        {
            RegistryKey key = null; ;

            switch (BaseKey)
            {
                case BaseKey.HKEY_CLASSES_ROOT:
                    key = Registry.ClassesRoot;
                    break;

                case BaseKey.HKEY_CURRENT_CONFIG:
                    key = Registry.CurrentConfig;
                    break;

                case BaseKey.HKEY_CURRENT_USER:
                    key = Registry.CurrentUser;
                    break;

                case BaseKey.HKEY_DYN_DATA:
                    key = Registry.DynData;
                    break;

                case BaseKey.HKEY_LOCAL_MACHINE:
                    key = Registry.LocalMachine;
                    break;

                case BaseKey.HKEY_PERFORMANCE_DATA:
                    key = Registry.PerformanceData;
                    break;
            }

            return key;
        }
        #endregion
    }
}

Everything makes sense in someone's mind

QuestionListView scroll while typing Pin
Figmo229-Dec-10 16:52
Figmo229-Dec-10 16:52 
QuestionHot to draw ellipse line intersection Pin
ferry24029-Dec-10 9:35
ferry24029-Dec-10 9:35 
AnswerRe: Hot to draw ellipse line intersection Pin
fjdiewornncalwe29-Dec-10 9:48
professionalfjdiewornncalwe29-Dec-10 9:48 
GeneralRe: Hot to draw ellipse line intersection Pin
ferry24029-Dec-10 10:33
ferry24029-Dec-10 10:33 
GeneralRe: Hot to draw ellipse line intersection Pin
fjdiewornncalwe29-Dec-10 10:49
professionalfjdiewornncalwe29-Dec-10 10:49 
QuestionWindows Phone 7 passing multiple items between pages Pin
Paul Harsent29-Dec-10 8:06
Paul Harsent29-Dec-10 8:06 
AnswerRe: Windows Phone 7 passing multiple items between pages Pin
Pete O'Hanlon29-Dec-10 9:09
mvePete O'Hanlon29-Dec-10 9:09 
GeneralRe: Windows Phone 7 passing multiple items between pages Pin
Paul Harsent29-Dec-10 10:47
Paul Harsent29-Dec-10 10:47 
GeneralRe: Windows Phone 7 passing multiple items between pages Pin
Pete O'Hanlon29-Dec-10 10:49
mvePete O'Hanlon29-Dec-10 10:49 
QuestionProblem with reading Bytes Pin
Honeyboy_2029-Dec-10 7:49
Honeyboy_2029-Dec-10 7:49 
AnswerRe: Problem with reading Bytes Pin
Honeyboy_2029-Dec-10 7:50
Honeyboy_2029-Dec-10 7:50 
GeneralRe: Problem with reading Bytes Pin
Richard MacCutchan29-Dec-10 9:23
mveRichard MacCutchan29-Dec-10 9:23 
GeneralRe: Problem with reading Bytes Pin
Luc Pattyn29-Dec-10 9:34
sitebuilderLuc Pattyn29-Dec-10 9:34 
AnswerRe: Problem with reading Bytes Pin
carbon_golem29-Dec-10 8:34
carbon_golem29-Dec-10 8:34 
GeneralRe: Problem with reading Bytes Pin
Honeyboy_2029-Dec-10 8:57
Honeyboy_2029-Dec-10 8:57 
GeneralRe: Problem with reading Bytes Pin
Henry Minute29-Dec-10 9:29
Henry Minute29-Dec-10 9:29 
GeneralRe: Problem with reading Bytes Pin
carbon_golem29-Dec-10 10:30
carbon_golem29-Dec-10 10:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.