J
Jeff0803
Guest
I want to create Configuration file when it does not exist.
Here is the default configuration file.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Code" value="AB" />
<add key="Port" value="1000" />
<add key="MachineID" value="M0001" />
</appSettings>
</configuration>
And here is the code generate it.
public bool CreateDefaultConfigFile(string configfile)
{
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = configfile;
try
{
// OpenApp.Config of executable
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
// Add an Application Setting if not exist
config.AppSettings.Settings.Add("Code", "AB");
config.AppSettings.Settings.Add("Port", "1000");
config.AppSettings.Settings.Add("MachineID", "M0001");
config.Save(ConfigurationSaveMode.Modified);
}
catch(ConfigurationErrorsException ex)
{
MessageBox.Show("Failed to create configuration file." + Environment.NewLine +
ex.Message);
return false;
}
return true;
}
However I want to add custom ConfigSection like following(Bold/Italic).
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name = "printer" type = "MyPrinterType"/>
<section name = "scanner" type = "MyScannerType"/>
</configSections>
<appSettings>
<add key="Code" value="AB" />
<add key="Port" value="1000" />
<add key="MachineID" value="M0001" />
</appSettings>
<scanner>
<deviceList>
</deviceList>
</scanner>
</configuration>
What should I add codes to the existing code?
Continue reading...
Here is the default configuration file.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Code" value="AB" />
<add key="Port" value="1000" />
<add key="MachineID" value="M0001" />
</appSettings>
</configuration>
And here is the code generate it.
public bool CreateDefaultConfigFile(string configfile)
{
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = configfile;
try
{
// OpenApp.Config of executable
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
// Add an Application Setting if not exist
config.AppSettings.Settings.Add("Code", "AB");
config.AppSettings.Settings.Add("Port", "1000");
config.AppSettings.Settings.Add("MachineID", "M0001");
config.Save(ConfigurationSaveMode.Modified);
}
catch(ConfigurationErrorsException ex)
{
MessageBox.Show("Failed to create configuration file." + Environment.NewLine +
ex.Message);
return false;
}
return true;
}
However I want to add custom ConfigSection like following(Bold/Italic).
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name = "printer" type = "MyPrinterType"/>
<section name = "scanner" type = "MyScannerType"/>
</configSections>
<appSettings>
<add key="Code" value="AB" />
<add key="Port" value="1000" />
<add key="MachineID" value="M0001" />
</appSettings>
<scanner>
<deviceList>
</deviceList>
</scanner>
</configuration>
What should I add codes to the existing code?
Continue reading...