Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在安装过程中更改App.config_C#_App Config - Fatal编程技术网

C# 在安装过程中更改App.config

C# 在安装过程中更改App.config,c#,app-config,C#,App Config,我有这样设置的XML文件 <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neut

我有这样设置的XML文件

<?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>
但在此位置获取错误“由于其保护级别而无法访问”:

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);
        }
    }
}

在一个类似的项目中,我用了一种稍微不同的方式:

  • 发送安装程序时不带“myapp.exe.config”文件
  • 相反,请发送一个“myapp.exe.config.default”文件,其中包含诸如“
    {Branch}
    ”之类的占位符
  • 在安装过程中,将“myapp.exe.config.default”作为字符串加载到内存中
  • 将占位符替换为实际值(例如您的“
    30000
    ”)
  • 将替换的字符串写入实际文件“myapp.exe.config”
  • 好处:在编写配置文件之前,请检查是否存在任何现有的配置文件,并将其复制为备份以保留以前的版本

  • 这在我们的应用程序中运行得非常顺利。

    您是否考虑过令牌替换或.NET 4转换?很抱歉,我不明白它是如何工作的,以及它如何帮助您阅读我提供的链接?当然可以。它是关于使用插件将XML转换为XSLT的。对但我不明白XSLT将如何帮助我。我再次道歉,没问题。所提供的是一种根据构建模式以不同方式构建配置的方法。因此,它是在构建时通过T4模板进行的令牌替换。因此,您可以为特定于环境的设置转换配置。这是.NET4的开箱即用。感谢您提供的解决方案。我一定会试试的。但我想知道是否有标准的方式来操作应用程序。config@gouph我想还有其他方法。对我来说,这只是个时间问题,结果证明我开发得很快,而且工作得很好。你如何排除myapp.exe.config?(对于在my中没有“myapp.exe.config”的ship安装程序)@gouph,我只是将“myapp.exe.config”从打包到安装程序中省略。是否可以在标准Visual Studio安装程序项目中排除配置?
    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);
            }
        }
    }