I have XML-file with settings like this
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="UpdateReportService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<UpdateReportService.Properties.Settings>
<setting name="Path" serializeAs="String">
<value>C:\1</value>
</setting>
<setting name="Branch" serializeAs="String">
<value>200</value>
</setting>
<setting name="b204" serializeAs="String">
<value>192.168.1.55</value>
</setting>
<setting name="b200" serializeAs="String">
<value>192.168.0.83</value>
</setting>
<setting name="Hour" serializeAs="String">
<value>11</value>
</setting>
</UpdateReportService.Properties.Settings>
</applicationSettings>
</configuration>
And I'd like to change some values to values typed by user during install program.
I find example on VB and try convert it to c#:
namespace InstallConfigurator
{
[RunInstaller(true)]
public class SettingsClass : Installer
{
public override void Install(System.Collections.IDictionary stateSaver)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(Context.Parameters["TARGETDIR"].ToString() + "UpdateReportService.exe");
ClientSettingsSection applicationSettingsSection = (ClientSettingsSection)config.SectionGroups["applicationSettings"].Sections["UpdateReportService.Properties.Settings"];
SettingElement Elem = applicationSettingsSection.Settings["Branch"];
applicationSettingsSection.Settings.Remove(Elem);
Elem.Value.ValueXml.InnerXml = "30000";
applicationSettingsSection.Settings.Add(Elem);
config.Save(ConfigurationSaveMode.Full);
}
}
}
But get error "inaccessible due to its protection level" at this place:
SettingElement Elem = applicationSettingsSection.Settings["Branch"];
So, is it possible on c# to access to section in App.config and to change it.
Upd. 2012.02.10
i've solved problem this way:
namespace InstallConfigurator
{
[RunInstaller(true)]
public class SettingsClass : Installer
{
public override void Install(System.Collections.IDictionary stateSaver)
{
string xml = Context.Parameters["TARGETDIR"].ToString() + "UpdateReportService.exe.config";
XmlDocument document = new XmlDocument();
document.Load(xml);
XPathNavigator navigator = document.CreateNavigator();
XmlNamespaceManager ns = new XmlNamespaceManager(navigator.NameTable);
foreach (XPathNavigator nav in navigator.Select(@"/configuration/applicationSettings/UpdateReportService.Properties.Settings/setting[@name='Branch']/value"))
{
nav.SetValue(Context.Parameters["BRANCH"].ToString());
}
foreach (XPathNavigator nav in navigator.Select(@"/configuration/applicationSettings/UpdateReportService.Properties.Settings/setting[@name='Path']/value"))
{
nav.SetValue(Context.Parameters["PATH"].ToString());
}
document.Save(xml);
}
}
}
You cannot use multiple configuration files (i.e. one per library project) without coding.
To add an application configuration file to a C# projectIn the middle pane, select the Application Configuration File template. Select the Add button. A file named App. config is added to your project.
The application configuration file usually lives in the same directory as your application. For web applications, it is named Web. config. For non-web applications, it starts life with the name of App.
Web. Config is used for asp.net web projects / web services. App. Config is used for Windows Forms, Windows Services, Console Apps and WPF applications.
The app.config file is a basic piece of the .NET Framework, yet I’ve seen several projects putting their configuration in other places (like plain text files or the registry). Unless you have a very good reason to do so, it’s more convenient and familiar to use the app.config file.
An app.config file is automatically added to your project when you create a new application under the Windows Classic Desktop header in Visual Studio: When you open the file, there’s not much in it: You can start typing under the <configuration> section and Visual Studio’s Intellisense will provide you with the possible options:
When you try to modify the app.config at runtime, if you don’t do it right, you’ll run into a few problems: System.Configuration.ConfigurationErrorsException: The configuration is read only.
If you try to update the app.config at runtime while running the exe from /Program Files/ you’ll run into the following error: System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Access to the path is denied. You can run your app as admin or put proper permissions to overcome this problem.
In a similar project, I'm doing it in a slightly different way:
{Branch}
".30000
").This runs pretty smooth in our applications.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With