public Config(string configFilePath) { var fileProvider = new PhysicalFileProvider(Path.GetDirectoryName(configFilePath)); var configSource = new IniConfigurationSource { Path = Path.GetFileName(configFilePath), FileProvider = fileProvider }; var iniReader = new IniConfigurationProvider(configSource); iniReader.Load(); iniReader.TryGet(FormatIniPath(Section.General, GeneralKeys.Frequency), out var frequency); iniReader.TryGet(FormatIniPath(Section.General, GeneralKeys.ApiUrl), out var url); FrequencyInSeconds = int.Parse(frequency); ApiUrl = url; Components = new List <Component>(); foreach (var componentKey in iniReader.GetChildKeys(new string[0], Section.Components)) { iniReader.TryGet(FormatIniPath(Section.Components, componentKey), out var dir); var patternExists = iniReader.TryGet(FormatIniPath(Section.FilePatterns, componentKey), out var pattern); pattern = patternExists ? pattern : "*"; Components.Add(new Component(componentKey, dir, pattern)); } }
static ConfigurationManager() { #if NET_CORE string configPath = Path.Combine(AppContext.BaseDirectory, "config.ini"); if (File.Exists(configPath)) { provider = new IniConfigurationProvider(new IniConfigurationSource { Path = configPath }); provider.Load(); } #else string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.ini"); if (File.Exists(configPath)) { string[] lines = File.ReadAllLines(configPath); foreach (var line in lines) { int firstSplitIndex = line.IndexOf('='); string key = line.Substring(0, firstSplitIndex); string value = line.Substring(firstSplitIndex + 1, line.Length - firstSplitIndex - 1); if (value.Contains(key)) { throw new ArgumentException($"There is a same key already: {key}"); } else { Values.Add(key, value); } } } #endif }
public void IniConfiguration_Does_Not_Throw_On_Optional_Configuration() { var configSource = new IniConfigurationProvider("NotExistingConfig.ini", optional: true); configSource.Load(); Assert.Throws <InvalidOperationException>(() => configSource.Get("key")); }
private void LoadConfigFile(string filename) { StreamReader sr = new StreamReader(filename); IniConfigurationSource iniSrouce = new IniConfigurationSource(); iniSrouce.Path = filename; IniConfigurationProvider iniFile = new IniConfigurationProvider(iniSrouce); iniFile.Load(sr.BaseStream); string value; iniFile.TryGet("General:LunchMenuURL", out value); LunchMenuUrl = value; iniFile.TryGet("Database:Server", out value); DbServer = value; iniFile.TryGet("Database:User", out value); DbUser = value; iniFile.TryGet("Database:Password", out value); DbPass = value; iniFile.TryGet("Database:Name", out value); DbName = value; }
private void LoadConfigFile(string filename) { StreamReader sr = new StreamReader(filename); IniConfigurationSource iniSrouce = new IniConfigurationSource(); iniSrouce.Path = filename; IniConfigurationProvider iniFile = new IniConfigurationProvider(iniSrouce); iniFile.Load(sr.BaseStream); string value; iniFile.TryGet("Website:homeurl", out value); WebsiteHomeUrl = value; iniFile.TryGet("Website:url", out value); WebsiteUrl = value; iniFile.TryGet("Website:nodeTitle", out value); WebsiteNodeTitle = value; iniFile.TryGet("Website:nodePublishedAt", out value); WebsiteNodePublishedAt = value; iniFile.TryGet("Website:nodeMessageBody", out value); WebsiteNodeMessageBody = value; iniFile.TryGet("Email:adminEmail", out value); EmailAdminEmail = value; iniFile.TryGet("Email:recipentsSourceType", out value); EmailRecipentsSourceType = value; iniFile.TryGet("Email:recipentsSource", out value); EmailRecipentsSource = value; iniFile.TryGet("Email:fromName", out value); EmailFromName = value; iniFile.TryGet("Email:fromEmail", out value); EmailFromEmail = value; iniFile.TryGet("SmtpClient:host", out value); SmtpClientHost = value; iniFile.TryGet("SmtpClient:port", out value); SmtpClientPort = int.Parse(value); iniFile.TryGet("SmtpClient:usessl", out value); SmtpClientUseSsl = bool.Parse(value); iniFile.TryGet("SmtpClient:username", out value); SmtpClientUsername = value; iniFile.TryGet("SmtpClient:password", out value); SmtpClientPassword = value; }
public void LoadMethodCanHandleEmptyValue() { var ini = @"DefaultKey="; var iniConfigSrc = new IniConfigurationProvider(TestStreamHelpers.ArbitraryFilePath); iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)); Assert.Equal(string.Empty, iniConfigSrc.Get("DefaultKey")); }
public void LoadMethodCanHandleEmptyValue() { var ini = @"DefaultKey="; var iniConfigSrc = new IniConfigurationProvider(new IniConfigurationSource()); iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)); Assert.Equal(string.Empty, iniConfigSrc.Get("DefaultKey")); }
public static IniConfigurationProvider LoadSettings() { IniConfigurationSource source = new IniConfigurationSource(); source.Path = "Settings.ini"; IniConfigurationProvider settings = (IniConfigurationProvider)source.Build(new ConfigurationBuilder()); settings.Load(); return(settings); }
public void ShouldRemoveLeadingAndTrailingWhiteSpacesFromSectionName() { var ini = "[ \t section \t ]\n" + "key=value"; var iniConfigSrc = new IniConfigurationProvider(new IniConfigurationSource()); iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)); Assert.Equal("value", iniConfigSrc.Get("section:key")); }
public void ShouldRemoveLeadingAndTrailingWhiteSpacesFromKeyAndValue() { var ini = "[section]\n" + " \t key \t = \t value\t "; var iniConfigSrc = new IniConfigurationProvider(new IniConfigurationSource()); iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)); Assert.Equal("value", iniConfigSrc.Get("section:key")); }
public void DoubleQuoteIsPartOfValueIfNotPaired() { var ini = "[ConnectionString]\n" + "DefaultConnection=\"TestConnectionString\n" + "Provider=SqlClient\""; var iniConfigSrc = new IniConfigurationProvider(new IniConfigurationSource()); iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)); Assert.Equal("\"TestConnectionString", iniConfigSrc.Get("ConnectionString:DefaultConnection")); Assert.Equal("SqlClient\"", iniConfigSrc.Get("ConnectionString:Provider")); }
public void DoubleQuoteIsPartOfValueIfAppearInTheMiddleOfValue() { var ini = "[ConnectionString]\n" + "DefaultConnection=Test\"Connection\"String\n" + "Provider=Sql\"Client"; var iniConfigSrc = new IniConfigurationProvider(TestStreamHelpers.ArbitraryFilePath); iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)); Assert.Equal("Test\"Connection\"String", iniConfigSrc.Get("ConnectionString:DefaultConnection")); Assert.Equal("Sql\"Client", iniConfigSrc.Get("ConnectionString:Provider")); }
public void DoubleQuoteIsPartOfValueIfAppearInTheMiddleOfValue() { var ini = "[ConnectionString]\n" + "DefaultConnection=Test\"Connection\"String\n" + "Provider=Sql\"Client"; var iniConfigSrc = new IniConfigurationProvider(new IniConfigurationSource()); iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)); Assert.Equal("Test\"Connection\"String", iniConfigSrc.Get("ConnectionString:DefaultConnection")); Assert.Equal("Sql\"Client", iniConfigSrc.Get("ConnectionString:Provider")); }
public void ThrowExceptionWhenFoundInvalidLine() { var ini = @" ConnectionString "; var iniConfigSrc = new IniConfigurationProvider(new IniConfigurationSource()); var expectedMsg = Resources.FormatError_UnrecognizedLineFormat("ConnectionString"); var exception = Assert.Throws <FormatException>(() => iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini))); Assert.Equal(expectedMsg, exception.Message); }
public void DoubleQuoteIsPartOfValueIfNotPaired() { var ini = "[ConnectionString]\n" + "DefaultConnection=\"TestConnectionString\n" + "Provider=SqlClient\""; var iniConfigSrc = new IniConfigurationProvider(TestStreamHelpers.ArbitraryFilePath); iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)); Assert.Equal("\"TestConnectionString", iniConfigSrc.Get("ConnectionString:DefaultConnection")); Assert.Equal("SqlClient\"", iniConfigSrc.Get("ConnectionString:Provider")); }
public void ThrowExceptionWhenFoundBrokenSectionHeader() { var ini = @" [ConnectionString DefaultConnection=TestConnectionString "; var iniConfigSrc = new IniConfigurationProvider(TestStreamHelpers.ArbitraryFilePath); var expectedMsg = Resources.FormatError_UnrecognizedLineFormat("[ConnectionString"); var exception = Assert.Throws <FormatException>(() => iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini))); Assert.Equal(expectedMsg, exception.Message); }
public void IniConfiguration_Throws_On_Missing_Configuration_File() { var configSource = new IniConfigurationProvider("NotExistingConfig.ini", optional: false); Assert.Throws<FileNotFoundException>(() => { try { configSource.Load(); } catch (FileNotFoundException exception) { Assert.Equal(Resources.FormatError_FileNotFound("NotExistingConfig.ini"), exception.Message); throw; } }); }
public void LoadKeyValuePairsFromValidIniFileWithQuotedValues() { var ini = "[DefaultConnection]\n" + "ConnectionString=\"TestConnectionString\"\n" + "Provider=\"SqlClient\"\n" + "[Data:Inventory]\n" + "ConnectionString=\"AnotherTestConnectionString\"\n" + "Provider=\"MySql\""; var iniConfigSrc = new IniConfigurationProvider(new IniConfigurationSource()); iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)); Assert.Equal("TestConnectionString", iniConfigSrc.Get("DefaultConnection:ConnectionString")); Assert.Equal("SqlClient", iniConfigSrc.Get("DefaultConnection:Provider")); Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Get("Data:Inventory:ConnectionString")); Assert.Equal("MySql", iniConfigSrc.Get("Data:Inventory:Provider")); }
public void LoadKeyValuePairsFromValidIniFileWithQuotedValues() { var ini = "[DefaultConnection]\n" + "ConnectionString=\"TestConnectionString\"\n" + "Provider=\"SqlClient\"\n" + "[Data:Inventory]\n" + "ConnectionString=\"AnotherTestConnectionString\"\n" + "Provider=\"MySql\""; var iniConfigSrc = new IniConfigurationProvider(TestStreamHelpers.ArbitraryFilePath); iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)); Assert.Equal("TestConnectionString", iniConfigSrc.Get("DefaultConnection:ConnectionString")); Assert.Equal("SqlClient", iniConfigSrc.Get("DefaultConnection:Provider")); Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Get("Data:Inventory:ConnectionString")); Assert.Equal("MySql", iniConfigSrc.Get("Data:Inventory:Provider")); }
public void LoadKeyValuePairsFromValidIniFile() { var ini = @"[DefaultConnection] ConnectionString=TestConnectionString Provider=SqlClient [Data:Inventory] ConnectionString=AnotherTestConnectionString SubHeader:Provider=MySql"; var iniConfigSrc = new IniConfigurationProvider(new IniConfigurationSource()); iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)); Assert.Equal("TestConnectionString", iniConfigSrc.Get("defaultconnection:ConnectionString")); Assert.Equal("SqlClient", iniConfigSrc.Get("DEFAULTCONNECTION:PROVIDER")); Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Get("Data:Inventory:CONNECTIONSTRING")); Assert.Equal("MySql", iniConfigSrc.Get("Data:Inventory:SubHeader:Provider")); }
public void ThrowExceptionWhenKeyIsDuplicated() { var ini = @" [Data:DefaultConnection] ConnectionString=TestConnectionString Provider=SqlClient [Data] DefaultConnection:ConnectionString=AnotherTestConnectionString Provider=MySql "; var iniConfigSrc = new IniConfigurationProvider(new IniConfigurationSource()); var expectedMsg = Resources.FormatError_KeyIsDuplicated("Data:DefaultConnection:ConnectionString"); var exception = Assert.Throws <FormatException>(() => iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini))); Assert.Equal(expectedMsg, exception.Message); }
public void LoadKeyValuePairsFromValidIniFileWithoutSectionHeader() { var ini = @" DefaultConnection:ConnectionString=TestConnectionString DefaultConnection:Provider=SqlClient Data:Inventory:ConnectionString=AnotherTestConnectionString Data:Inventory:Provider=MySql "; var iniConfigSrc = new IniConfigurationProvider(new IniConfigurationSource()); iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)); Assert.Equal("TestConnectionString", iniConfigSrc.Get("DefaultConnection:ConnectionString")); Assert.Equal("SqlClient", iniConfigSrc.Get("DefaultConnection:Provider")); Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Get("Data:Inventory:ConnectionString")); Assert.Equal("MySql", iniConfigSrc.Get("Data:Inventory:Provider")); }
public void IniConfiguration_Throws_On_Missing_Configuration_File() { var configSource = new IniConfigurationProvider("NotExistingConfig.ini", optional: false); Assert.Throws <FileNotFoundException>(() => { try { configSource.Load(); } catch (FileNotFoundException exception) { Assert.Equal(Resources.FormatError_FileNotFound("NotExistingConfig.ini"), exception.Message); throw; } }); }
protected override (IConfigurationProvider Provider, Action Initializer) LoadThroughProvider( TestSection testConfig) { var iniBuilder = new StringBuilder(); SectionToIni(iniBuilder, "", testConfig); var provider = new IniConfigurationProvider( new IniConfigurationSource { Optional = true }); var ini = iniBuilder.ToString(); return(provider, () => provider.Load(TestStreamHelpers.StringToStream(ini))); }
public void LoadKeyValuePairsFromValidIniFile() { var ini = @"[DefaultConnection] ConnectionString=TestConnectionString Provider=SqlClient [Data:Inventory] ConnectionString=AnotherTestConnectionString SubHeader:Provider=MySql"; var iniConfigSrc = new IniConfigurationProvider(TestStreamHelpers.ArbitraryFilePath); iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)); Assert.Equal("TestConnectionString", iniConfigSrc.Get("defaultconnection:ConnectionString")); Assert.Equal("SqlClient", iniConfigSrc.Get("DEFAULTCONNECTION:PROVIDER")); Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Get("Data:Inventory:CONNECTIONSTRING")); Assert.Equal("MySql", iniConfigSrc.Get("Data:Inventory:SubHeader:Provider")); }
public void SupportAndIgnoreComments() { var ini = @" ; Comments [DefaultConnection] # Comments ConnectionString=TestConnectionString / Comments Provider=SqlClient [Data:Inventory] ConnectionString=AnotherTestConnectionString Provider=MySql "; var iniConfigSrc = new IniConfigurationProvider(new IniConfigurationSource()); iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)); Assert.Equal("TestConnectionString", iniConfigSrc.Get("DefaultConnection:ConnectionString")); Assert.Equal("SqlClient", iniConfigSrc.Get("DefaultConnection:Provider")); Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Get("Data:Inventory:ConnectionString")); Assert.Equal("MySql", iniConfigSrc.Get("Data:Inventory:Provider")); }
public void SupportAndIgnoreComments() { var ini = @" ; Comments [DefaultConnection] # Comments ConnectionString=TestConnectionString / Comments Provider=SqlClient [Data:Inventory] ConnectionString=AnotherTestConnectionString Provider=MySql "; var iniConfigSrc = new IniConfigurationProvider(TestStreamHelpers.ArbitraryFilePath); iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)); Assert.Equal("TestConnectionString", iniConfigSrc.Get("DefaultConnection:ConnectionString")); Assert.Equal("SqlClient", iniConfigSrc.Get("DefaultConnection:Provider")); Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Get("Data:Inventory:ConnectionString")); Assert.Equal("MySql", iniConfigSrc.Get("Data:Inventory:Provider")); }
public void ThrowExceptionWhenKeyIsDuplicated() { var ini = @" [Data:DefaultConnection] ConnectionString=TestConnectionString Provider=SqlClient [Data] DefaultConnection:ConnectionString=AnotherTestConnectionString Provider=MySql "; var iniConfigSrc = new IniConfigurationProvider(TestStreamHelpers.ArbitraryFilePath); var expectedMsg = Resources.FormatError_KeyIsDuplicated("Data:DefaultConnection:ConnectionString"); var exception = Assert.Throws<FormatException>(() => iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini))); Assert.Equal(expectedMsg, exception.Message); }
public void ThrowExceptionWhenFoundInvalidLine() { var ini = @" ConnectionString "; var iniConfigSrc = new IniConfigurationProvider(TestStreamHelpers.ArbitraryFilePath); var expectedMsg = Resources.FormatError_UnrecognizedLineFormat("ConnectionString"); var exception = Assert.Throws<FormatException>(() => iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini))); Assert.Equal(expectedMsg, exception.Message); }
public void LoadKeyValuePairsFromValidIniFileWithoutSectionHeader() { var ini = @" DefaultConnection:ConnectionString=TestConnectionString DefaultConnection:Provider=SqlClient Data:Inventory:ConnectionString=AnotherTestConnectionString Data:Inventory:Provider=MySql "; var iniConfigSrc = new IniConfigurationProvider(TestStreamHelpers.ArbitraryFilePath); iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)); Assert.Equal("TestConnectionString", iniConfigSrc.Get("DefaultConnection:ConnectionString")); Assert.Equal("SqlClient", iniConfigSrc.Get("DefaultConnection:Provider")); Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Get("Data:Inventory:ConnectionString")); Assert.Equal("MySql", iniConfigSrc.Get("Data:Inventory:Provider")); }
public void IniConfiguration_Does_Not_Throw_On_Optional_Configuration() { var configSource = new IniConfigurationProvider("NotExistingConfig.ini", optional: true); configSource.Load(); Assert.Throws<InvalidOperationException>(() => configSource.Get("key")); }