在安装过程中更改 App.config [英] Change App.config during installation

查看:39
本文介绍了在安装过程中更改 App.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样设置的 XML 文件

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.

我在 VB 上找到示例并尝试将其转换为 C#:

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"];

那么,是否可以在 c# 上访问 App.config 中的部分并对其进行更改.

So, is it possible on c# to access to section in App.config and to change it.

更新.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:

  1. 使用myapp.exe.config"文件发送您的设置.
  2. 相反,提供一个myapp.exe.config.default"文件,其中包含诸如{Branch}"之类的占位符.
  3. 在设置过程中,将myapp.exe.config.default"作为字符串加载到内存中.
  4. 将占位符替换为实际值(例如您的30000").
  5. 将替换后的字符串写入实际文件myapp.exe.config".
  6. 奖励:在编写配置之前检查是否存在任何现有的配置文件并将其复制为备份以保留以前的版本.
  1. Ship your setup with no "myapp.exe.config" file.
  2. Instead, ship a "myapp.exe.config.default" file that contains placeholders like "{Branch}".
  3. During setup, load the "myapp.exe.config.default" as a string into memory.
  4. Replace the placeholders with the actual values (e.g. your "30000").
  5. Write the replaced string as the actual file "myapp.exe.config".
  6. 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆