【问题标题】:How to change the content of my app.config after installation of the application安装应用程序后如何更改我的 app.config 的内容
【发布时间】:2013-06-13 08:26:47
【问题描述】:

我正在开发 WPF 应用程序,该应用程序很快就必须准备好安装,但有一个功能允许用户对 app.config 文件进行更改,我不知道如何从代码隐藏中执行此操作应用程序。另外我不知道安装应用程序后这将如何工作。

简单地说:我有一个窗口,允许用户输入要在另一个应用程序的 web.config 中搜索的文本。所以在我的 app.config 中我有不同的搜索,我想要在安装用户能够(在窗口的文本框中输入值后)在应用程序的 app.config 中输入新值。

有人能告诉我这是否可能,以及我最终如何实现这一目标吗?

【问题讨论】:

标签: c# configuration installation app-config


【解决方案1】:

我在申请中的表现如何。我有 app.config 如下

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="StoreConnectionString"
          connectionString="Data Source=.\;MultipleActiveResultSets=True;Initial Catalog=Store;Integrated Security=False;"
          providerName="System.Data.SqlClient" />
    </connectionStrings>

    <appSettings>
        <add key="ExportPath" value="D:\" />
        <add key="CompanyName" value="My Company" />
        <add key="mail" value="email@mail.com" />
        <add key="phone" value="+992918254040" />
        <add key="ExpDate" value="Pink" />
        <add key="Print" value="No" />
        <add key="EnforcePassw" value="Yes"/>
    </appSettings>
</configuration>

所以我可以从我的应用程序中更改和保存应用程序设置,这里是代码

private void btnSave(object sender, RoutedEventArgs e)
        {
            //returns path of folder where your application is
            string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            //combines path of folder and file
            string configFile = System.IO.Path.Combine(appPath, "MyApp.exe.config");
            //Defines the configuration file mapping for an .exe application
            ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
            configFileMap.ExeConfigFilename = configFile;
            System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

            config.AppSettings.Settings["ExportPath"].Value = txtExport.Text;
            config.AppSettings.Settings["CompanyName"].Value = txtComapny.Text;
            config.AppSettings.Settings["mail"].Value = txtEmail.Text;
            config.AppSettings.Settings["phone"].Value = txtPhone.Text;
            config.AppSettings.Settings["Print"].Value = print;
            config.AppSettings.Settings["EnforcePassw"].Value = password;
            config.AppSettings.Settings["ExpDate"].Value = color;
            config.Save(); 

        }

希望对你有所帮助!

如果要添加新字符串,请使用此代码;

        config.AppSettings.Settings.Add("Key", "Value");

【讨论】:

  • 在您的情况下,您正在为文件中已经存在的元素设置值。我正在尝试在 部分中添加全新的元素。我已经将它格式化为 XElement,我只需要一个允许我添加到 的子元素的方法
  • 查看更新的答案,使用 config.AppSettings.Settings.Add("Key", "Value");添加更多字符串;
  • 谢谢你的回答!它似乎是这样工作的。我可以请您简要解释一下您的方法的前五行在做什么吗?
  • 有没有办法不使用 ExeConfigurationFileMap ?在过去的 10 分钟里,我一直在测试它,它开始以一种奇怪的方式表现。添加超过 1 个元素(当我尝试仅添加一个元素时),格式错误。
【解决方案2】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-06
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多