Tuesday 4 June 2013

C# - Equivalent to the function GetSetting() in VB .NET

Getsetting DLL Download

using Microsoft.Win32;


public static string GetSetting(string applicationName, string sectionName, string key, string valeurParDéfaut)
        {
            StringBuilder path;
            RegistryKey registryKey;

            path = new StringBuilder(@"Software\VB and VBA Program Settings");

            // Generate the path of the key
            if (string.IsNullOrEmpty(applicationName) == false)
            {
                path.Append('\\');
                path.Append(applicationName);

                if (string.IsNullOrEmpty(sectionName) == false)
                {
                    path.Append('\\');
                    path.Append(sectionName);
                }
            }
            // Open key
            registryKey = Registry.CurrentUser.OpenSubKey(path.ToString());


            // If the key does not exist, return the default Open key
            if (registryKey == null)
            {
                return valeurParDéfaut;
            }
            try
            {
                //Read the value contained in the key
                return (string)registryKey.GetValue(key, valeurParDéfaut);
            }
            finally
            {
                //registryKey.Dispose();
            }
        }

public static string GetSetting(string applicationName, string sectionName, string key)
        {
            return GetSetting(applicationName, sectionName, key, null);
        }


//It is now possible to use this class like this:
string s;
            s= Getsetting.Form1.GetSetting("JRFleetini", "DatabasePathini", "dbPathini", null);
            MessageBox.Show(s);

No comments:

Post a Comment