For my project I need a config that is stored in the same folder as the executable and can be easily accessed and edited by user. I found a solution to this problem in System.Configuration
package, but now I am running into a problem. The problem is that when I try to save config file, it creates it, but fills all values with what I assume to be default values (so empty string for string, or Black
for ConsoleColor
)
To save and later check the config I use the following code:
static void Main()
{
#if DEBUG
string applicationName =
Environment.GetCommandLineArgs()[0];
#else
string applicationName =
Environment.GetCommandLineArgs()[0]+ ".exe";
#endif
string exePath = System.IO.Path.Combine(
Environment.CurrentDirectory, applicationName);
// Get the configuration file. The file name has
// this format appname.exe.config.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(exePath);
try
{
// Create the custom section entry
// in <configSections> group and the
// related target section in <configuration>.
if (config.Sections["CustomSection"] == null)
{
ConsoleSection customSection = new ConsoleSection();
customSection.BackgroundColor = "Black";
customSection.ForegroundColor = "White";
config.Sections.Add("CustomSection", customSection);
// Save the configuration file.
customSection.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
Console.WriteLine("Created configuration file: {0}",
config.FilePath);
}
}
catch (ConfigurationErrorsException err)
{
Console.WriteLine("CreateConfigurationFile: {0}", err.ToString());
}
// Display feedback.
Console.WriteLine();
Console.WriteLine("Using OpenExeConfiguration(string).");
// Display the current configuration file path.
Console.WriteLine("Configuration file is: {0}",
config.FilePath);
ConsoleSection sect = config.GetSection("CustomSection") as ConsoleSection;
Console.WriteLine("FG Color: {0}",
sect.ForegroundColor);
Console.WriteLine("BG Color: {0}",
sect.BackgroundColor);
return;
}
And the ConsoleSection class:
public class ConsoleSection : ConfigurationSection
{
public ConsoleSection()
{
}
[ConfigurationProperty("BackgroundColor", IsRequired=true)]
public string BackgroundColor {
get { return (string)(this["BackgroundColor"]); }
set { this["BackgroundColor"] = value; }
}
[ConfigurationProperty("ForegroundColor", IsRequired = true)]
public string ForegroundColor
{
get { return (string)(this["ForegroundColor"]); }
set { this["ForegroundColor"] = value; }
}
}
I also noticed that during the first run (when it is supposed to save stuff), it reads the values just fine, so if you were to save this code and run it, the first run would produce expected output.
The code targets .NET Core 3.1, if it matters.
There are two points that are causing this error.
CustomSection
is already present?Please see the below code. If it working fine for all test cases.
void Main()
{
#if DEBUG
string applicationName = Environment.GetCommandLineArgs()[0];
#else
string applicationName = Environment.GetCommandLineArgs()[0] + ".exe";
#endif
string exePath = System.IO.Path.Combine(Environment.CurrentDirectory, applicationName);
// Get the configuration file. The file name has
// this format appname.exe.config.
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(@"E:\Temp\TwitterBot\TwitterBot\bin\Debug\TwitterBot.exe");
try
{
// Create the custom section entry
// in <configSections> group and the
// related target section in <configuration>.
if (config.Sections["CustomSection"] == null)
{
ConsoleSection customSection = new ConsoleSection();
customSection.BackgroundColor = "Black";
customSection.ForegroundColor = "White";
config.Sections.Add("CustomSection", customSection);
// Save the configuration file.
customSection.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
Console.WriteLine("Created configuration file: {0}", config.FilePath);
}
//Missing Else Part
else
{
config.Sections.Remove("CustomSection");
ConsoleSection customSection = new ConsoleSection();
customSection.BackgroundColor = "Red";
customSection.ForegroundColor = "Pink";
config.Sections.Add("CustomSection", customSection);
customSection.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
}
}
catch (ConfigurationErrorsException err)
{
Console.WriteLine("CreateConfigurationFile: {0}", err.ToString());
}
//After updating the values you need to refresh the section before reading it.
ConfigurationManager.RefreshSection("CustomSection");
// Display feedback.
Console.WriteLine();
Console.WriteLine("Using OpenExeConfiguration(string).");
// Display the current configuration file path.
Console.WriteLine("Configuration file is: {0}", config.FilePath);
ConsoleSection sect = config.GetSection("CustomSection") as ConsoleSection;
Console.WriteLine("FG Color: {0}", sect.ForegroundColor);
Console.WriteLine("BG Color: {0}", sect.BackgroundColor);
return;
}
public class ConsoleSection : ConfigurationSection
{
public ConsoleSection()
{
}
[ConfigurationProperty("BackgroundColor", IsRequired = true)]
public string BackgroundColor
{
get { return (string)(this["BackgroundColor"]); }
set { this["BackgroundColor"] = value; }
}
[ConfigurationProperty("ForegroundColor", IsRequired = true)]
public string ForegroundColor
{
get { return (string)(this["ForegroundColor"]); }
set { this["ForegroundColor"] = value; }
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.