本文介绍了在安装过程中更改的App.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的设置的XML文件
<?xml version =1.0encoding =utf- 8→>
< configuration>
< configSections>
< sectionGroup name =applicationSettingstype =System.Configuration.ApplicationSettingsGroup,System,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089>
< section name =UpdateReportService.Properties.Settingstype =System.Configuration.ClientSettingsSection,System,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089requirePermission =false/&
< / sectionGroup>
< / configSections>
< applicationSettings>
< UpdateReportService.Properties.Settings>
< setting name =PathserializeAs =String>
< value> C:\1< / value>
< / setting>
< setting name =BranchserializeAs =String>
< value> 200< / value>
< / setting>
< setting name =b204serializeAs =String>
< value> 192.168.1.55< / value>
< / setting>
< setting name =b200serializeAs =String>
< value> 192.168.0.83< / value>
< / setting>
< setting name =HourserializeAs =String>
< value> 11< / value>
< / setting>
< /UpdateReportService.Properties.Settings>
< / applicationSettings>
< / configuration>
我想将一些值更改为用户在安装程序中键入的值。
我发现VB上的示例并尝试将其转换为c#:
命名空间InstallConfigurator
{
[RunInstaller(true)]
public class SettingsClass:Installer
{
public override void Install(System.Collections.IDictionary stateSaver)
{
配置config = ConfigurationManager.OpenExeConfiguration(Context.Parameters [TARGETDIR]。ToString()+UpdateReportService.exe);
ClientSettingsSection applicationSettingsSection =(ClientSettingsSection)config.SectionGroups [applicationSettings]。部分[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);
}
}
}
但得到错误到其保护级别:
SettingElement Elem = applicationSettingsSection.Settings [Branch];
因此,是否可能在c#上访问App.config中的部分并更改它。 / p>
更新。 2012.02.10
我已经用这种方式解决了问题:
命名空间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导航器。选择(@/ configuration / applicationSettings / UpdateReportService.Properties.Settings / setting [@ name ='Branch'] / value))
{
nav.SetValue(Context.Parameters [BRANCH]。ToString());
}
foreach(导航器中的XPathNavigator导航器。选择(@/ configuration / applicationSettings / UpdateReportService.Properties.Settings / setting [@ name ='Path'] / value))
{
nav.SetValue(Context.Parameters [PATH]。ToString());
}
document.Save(xml);
}
}
}
解决方案>
在类似的项目中,我使用的方式略有不同:
- myapp.exe.config文件
- 而是运送包含占位符的myapp.exe.config.default文件,例如
{
- 在安装过程中,将myapp.exe.config.default作为字符串加载到内存中。
- 使用实际值替换占位符(例如,您的
30000
)。 - 将替换的字符串写为实际文件myapp.exe.config。
- 奖励:在写入配置之前,检查是否存在任何现有的配置文件,并将其复制为备份以保留以前的版本。
这在我们的应用程序中运行起来非常顺利。
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);
}
}
}
解决方案
In a similar project, I'm doing it in a slightly different way:
- Ship your setup with no "myapp.exe.config" file.
- Instead, ship a "myapp.exe.config.default" file that contains placeholders like "
{Branch}
". - During setup, load the "myapp.exe.config.default" as a string into memory.
- Replace the placeholders with the actual values (e.g. your "
30000
"). - Write the replaced string as the actual file "myapp.exe.config".
- Bonus: Before writing the config check whether any existing config file is present and copy it as a backup to keep the previous version.
This runs pretty smooth in our applications.
这篇关于在安装过程中更改的App.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-03 04:58