【问题标题】:Configuration Manager is not saving in app.config配置管理器未保存在 app.config 中
【发布时间】:2013-08-01 13:49:13
【问题描述】:

我有这个 app.config :

<?xml version="1.0"?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
  <add name="conexx" connectionString="Data Source=192.168.1.2 ;Initial Catalog   =ifdcontroladoria3 ;uid =sa;pwd = admin2012" providerName="System.Data.SqlClient" />
</connectionStrings>
<startup><supportedRuntime version="v4.0"   sku=".NETFramework,Version=v4.0,Profile=Client"/></startup></configuration>

我正在尝试用这个 C# 更新这个连接:

  coneydstr = @"Data Source=" + comboBox1.Text + ";Initial Catalog =" + cmbBancos.Text + ";uid =" + txtUsuario.Text + ";pwd =" + txtPassword.Text;
        try
         { coneyd.ConnectionString = coneydstr; 
           coneyd.Open(); 
           funciona = true;
           lblStringSalida.Text = coneydstr;
         }
         catch (Exception ex)
        {  MessageBox.Show(coneyd + ex.Message.ToString());  }

        if (funciona)
        { 
          Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
          config.ConnectionStrings.ConnectionStrings["conexx"].ConnectionString = coneydstr;
          config.Save();   
        } 

但没有更新,

 using System.Configuration;

在header和reference中,有什么问题??????

我写错了问题,我想将连接字符串存储在某个地方,可能是 app.config 或其他文件,并在另一个安装或服务器或用户更改时更改字符串。这样做的正确方法是什么???

【问题讨论】:

  • “没有更新”是什么意思?

标签: c# visual-studio-2010 configurationmanager


【解决方案1】:

嗯,您的问题很可能是您正在查看错误的文件。查看调试器中的 config.FilePath 属性 - 它会准确地告诉您您现在正在处理哪个文件。

当您在 Visual Studio 中运行此代码时,您的代码将更改 project\bin\debug 目录中的 Yourapplication.exe.config - 它会不会更改 app.config你的项目目录!

因此,当您运行代码时 - 在 config.Save() 之后,查看项目的 \bin\debug 目录和其中的配置文件 - 这些值是否已正确更新?

【讨论】:

  • 那么在安装程序的时候也会出现类似的问题——“普通用户”是不允许随意更改/program文件中的东西的。然后会在用户文件夹的某个地方神奇地出现一个新的配置文件(不记得路径)。使调试更加困难;每次安装时,此路径的最后一个文件夹都会以伪随机方式更改。
  • 它不是伪随机的,它是从AppDomain's Evidence property 生成的。如果您明确设置它,您可能会控制它。
猜你喜欢
  • 2023-03-21
  • 2013-10-16
  • 2015-01-31
  • 1970-01-01
  • 2016-04-26
  • 2010-11-14
  • 2015-09-11
  • 2011-05-24
  • 1970-01-01
相关资源
最近更新 更多