期間我有XML的文件,設置這樣的變化的App.config安裝
<?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>
而且我想改變某些值由用戶輸入的過程中安裝程序值。
我在VB找到例子,並嘗試將其轉換爲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);
}
}
}
,但得到錯誤「無法訪問由於其保護級別」在這個地方:
SettingElement Elem = applicationSettingsSection.Settings["Branch"];
那麼,有沒有可能在c#上訪問App.config中的部分並對其進行更改。
Upd。 2012.02.10
我已經解決的問題是這樣的:
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);
}
}
}
你有沒有考慮令牌替換或.NET 4個變換http://www.hanselman.com/blog/SlowCheetahWebconfigTransformationSyntaxNowGeneralizedForAnyXMLConfigurationFile.aspx? – 2012-02-09 09:47:23
我很抱歉,但我不明白它是如何工作的,它是如何幫助我 – Andrey 2012-02-09 10:11:30
你是否通過我提供的鏈接閱讀? – 2012-02-09 10:20:18