hMailServer.Configuration.IniFile.Read C# (CSharp) Méthode

Read() public méthode

public Read ( string section, string key ) : string
section string
key string
Résultat string
        public string Read(string section, string key)
        {
            if (section == null)
                throw new ArgumentNullException(nameof(section));
            if (key == null)
                throw new ArgumentNullException(nameof(key));

            var value = new StringBuilder(255);
            GetPrivateProfileString(section, key, "", value, 255, _fullPath);
            return value.ToString();
        }

Usage Example

        public static ServiceConfiguration Read()
        {
            string installLocation;

            using (var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32))
            using (var subKey = baseKey.OpenSubKey("SOFTWARE\\hMailServer"))
            {
                if (subKey == null)
                    throw new InvalidOperationException("Unable to locate SOFTWARE\\hMailServer registry key");

                installLocation = (string) subKey.GetValue("InstallLocation", string.Empty, RegistryValueOptions.None);
            }

            var binDirectory = Path.Combine(installLocation, "Bin");
            var inifilePath = Path.Combine(binDirectory, "hMailServer.ini");

            var iniFile = new IniFile(inifilePath);

            var databaseType = iniFile.Read("Database", "Type");
            
            var serviceConfiguration = new ServiceConfiguration();

            switch (databaseType.ToLowerInvariant())
            {
                case "mysql":
                {
                    var username = iniFile.Read("Database", "Username");
                    var password = iniFile.Read("Database", "Password");
                    uint port = uint.Parse(iniFile.Read("Database", "Port"));
                    var server = iniFile.Read("Database", "Server");
                    var database = iniFile.Read("Database", "Database");

                    serviceConfiguration.DatabaseConfiguration = new DatabaseConfiguration
                        {
                            Database = database,
                            Password = password,
                            Port = port,
                            Server = server,
                            Username = username
                        };
                    break;
                }
                default:
                    throw new NotImplementedException(string.Format("Database type {0} is not supported.", databaseType));
            }

            serviceConfiguration.TempDirectory = iniFile.Read("Directories", "TempFolder");
            serviceConfiguration.DataDirectory = iniFile.Read("Directories", "DataFolder");

            return serviceConfiguration;
        }