标签: configurationmanager

如何在单个项目中从c#中的多个配置文件中读取值?

在我的项目中,我有两个名为app.config和的应用程序配置文件accessLevel.config.现在使用OpenExeConfiguration我能够访问app.config.exe file但不是accessLevel.config.请帮忙.

我有2个配置文件的主要原因是显示差异并使代码简单.我需要从accessLevel.config我的C#代码中读取值.

尝试以下代码但没有用:

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
config.AppSettings.File = "App2.config";
Run Code Online (Sandbox Code Playgroud)

c# configurationmanager app-config config configuration-files

9
推荐指数
1
解决办法
2万
查看次数

WPF中的ConfigurationManager

我在wpf项目中有一个配置文件来存储connectionstring.但是当我尝试获取AppSettings和ConnectionStrings时,我得到null.

WEB.config文件是这样的:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="Trackboard" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=Trackboard;Integrated Security=True;AttachDbFileName=E:\Users\Sean\Workspace\DATABASE\Trackboard.mdf"/>
  </connectionStrings>
  <appSettings>
    <add key="Trackboard" value="Data Source=(localdb)\v11.0;Initial Catalog=Trackboard;Integrated Security=True;AttachDbFileName=E:\Users\Sean\Workspace\DATABASE\Trackboard.mdf"/>
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我尝试过几种方式:

W1: ConnStr = ConfigurationManager.ConnectionStrings["Trackboard"].ConnectionString;
W2: ConnStr = ConfigurationManager.ConnectionStrings[0].ConnectionString;
W3: ConnStr = ConfigurationManager.AppSettings["Trackboard"];
W4: ConnStr = ConfigurationManager.AppSettings[0];
Run Code Online (Sandbox Code Playgroud)

他们都没有工作.

但这个工作:

ConnStr = @"Data Source=(localdb)\v11.0;Initial Catalog=Trackboard;Integrated Security=True;AttachDbFileName=E:\Users\Sean\Workspace\DATABASE\Trackboard.mdf";
Run Code Online (Sandbox Code Playgroud)

(这意味着我不能使用配置文件,这违背我的意愿)我需要帮助.

c# sql wpf configurationmanager appsettings

9
推荐指数
2
解决办法
3万
查看次数

有人可以提供快速的App.config/Web.config教程吗?

我之前已多次使用过这两个配置文件,但我从未花时间充分了解它们的工作原理.正如大多数人所做的那样,我理解了如何调用WebConfigurationManager.AppSettings["key"]获取配置值的基础知识.

以下是我提出的一些问题:

  1. 在类库中引用配置值时会发生什么,并且库是更大解决方案的一部分?是否需要将app.config复制到输出目录才能找到变量?(我假设是的)
  2. 你能直接在另一个类库中使用app.config的配置值吗?
  3. 假设问题3为"是",如果来自不同库的多个app.config文件包含具有相同键的配置值,会发生什么?
  4. 引用web.config但在类库中会发生什么?
  5. 当您引用app.config但在网站或Web应用程序项目中时会发生什么?

c# configurationmanager appsettings application-settings webconfigurationmanager

8
推荐指数
1
解决办法
4372
查看次数

ConfigurationManager.AppSettings在.Net Standard 1.5上生成编译时错误

我正在使用.Net Standard 1.5并希望从我的配置中读取一些值.但是以下行

ConfigurationManager.AppSettings["Foo"]
Run Code Online (Sandbox Code Playgroud)

给我以下编译时错误:

CS7069引用类型'NameValueCollection'声称它在'System'中定义,但找不到它

我想也许在.Net Standard中有另一种方法可以从AppSettings中读取,但我还没有找到关于该主题的任何内容.

configurationmanager appsettings namevaluecollection .net-standard .net-standard-1.5

8
推荐指数
0
解决办法
1231
查看次数

在运行时更改App.config

我正在为我们正在开发的系统编写测试WinForms/C#/ .NET 3.5应用程序,我们不需要在运行时切换.config文件,但这变成了一场噩梦.

这是场景:WinForms应用程序旨在测试WebApp,分为5个子系统.测试过程适用于在子系统之间发送的消息,并且为了使该过程成功,每个子系统都有自己的.config文件.

对于我的测试应用程序,我写了5个单独的配置文 我希望我能够在运行时在这5个文件之间切换,但问题是:我可以编程方式编辑应用程序.config文件很多次,但这些更改只会生效一次.我一直在寻找一个表格来解决这个问题,但我仍然没有成功.

我知道问题定义可能有点令人困惑,但如果有人帮助我,我会非常感激.

提前致谢!

---更新01-06-10 ---

我之前没有提到过.最初,我们的系统是一个Web应用程序,每个子系统之间都有WCF调用.出于性能测试的原因(我们使用的是ANTS 4),我们必须创建程序集的本地副本并从测试项目中引用它们.听起来有点不对劲,但我们找不到令人满意的方法来衡量远程应用程序的性能.

---结束更新---

这是我正在做的事情:

public void UpdateAppSettings(string key, string value)
{
    XmlDocument xmlDoc = XmlDocument.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

    foreach (XmlElement item in xmlDoc.DocumentElement)
    {
        foreach (XmlNode node in item.ChildNodes)
        {
            if (node.Name == key)
            {
                node.Attributes[0].Value = value;
                break;
            }
        }
    }

    xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

    System.Configuration.ConfigurationManager.RefreshSection("section/subSection");    
}
Run Code Online (Sandbox Code Playgroud)

c# configurationmanager app-config configuration-files .net-3.5

7
推荐指数
2
解决办法
3万
查看次数

配置管理器中缺少配置设置

我经常在我的解决方案的Configuration Manager中创建自定义构建配置.当我将先前创建的项目包含到解决方案中时,它们不会自动包含这些新配置.我发现使用适当的配置设置回填这些项目的唯一方法是手动编辑项目文件.

有没有办法强制解决方案中的所有项目都使用同一组Configuration Manager配置?

configurationmanager visual-studio

7
推荐指数
1
解决办法
5580
查看次数

如何使用C#检索.config文件中的自定义配置节列表?

当我尝试使用时检索.config文件中的部分列表

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Run Code Online (Sandbox Code Playgroud)

config.Sections集合包含一堆系统部分,但我没有在configSections标记中定义文件的部分.

c# configurationmanager configsection

7
推荐指数
1
解决办法
3110
查看次数

ConfigurationManager.AppSettings - 返回Null

我试图从我的app.config读取设置,我确定它以前工作,但现在它返回nullReferenceException.

获取设置的代码如下:

codeValueUtilRx = ConfigurationManager.AppSettings["CODEVALUE_UTIL_RX"].Split(';').ToList();
Run Code Online (Sandbox Code Playgroud)

我的app-congfig如下:

<appSettings>
    <add key ="LOGFILELOCATION" value ="C:\\RuleEditor\\"/>
    <add key ="CODEVALUE_UTIL_RX"  value="GCN;GRP;NDC;SPEC;TCC"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)

我感觉它看起来很明显,我只是想不通.我已经尝试将app-config移动到解决方案中的不同项目,并且我重新创建了文件,但没有运气.有任何想法吗?

c# configurationmanager app-config appsettings

7
推荐指数
1
解决办法
1万
查看次数

使用LINQ Where查询只获取一些ConfigurationManager.ConnectionStrings

我的目标是在控制台应用程序中WhereConfigurationManager.ConnectionStrings集合使用LINQ 查询(假设添加了System.Configuration引用的新的.NET 4.5控制台应用程序,以及相应的using声明).

我开始用这个,这并没有工作:

var relevantSettings =
        ConfigurationManager.ConnectionStrings.Where(s => s.Name.StartsWith("Xyz"));
Run Code Online (Sandbox Code Playgroud)

它告诉我:

方法' IEnumerable<TSource> System.Linq.Enumerable.Where<TSource(this IEnumerable<TSource>, Func<TSource,bool>)' 的类型参数不能从用法中推断出来.尝试明确指定参数.

这抓住了我措手不及,所以我想这个要检查我的理智,但是这并没有工作之一:

foreach (var settingsin ConfigurationManager.ConnectionStrings)
{
    if (settings.Name.StartsWith("Xyz")) Console.WriteLine("Found one!");
}
Run Code Online (Sandbox Code Playgroud)

它抱怨没有有关foreach声明,但对.Name位,出现错误:

无法解析符号'名称'

据我所知,有可能是一些防止编译器推断var的类型,仔细检查,我想这其中的工作:

foreach (ConnectionStringSettings settings in ConfigurationManager.ConnectionStrings)
{
    if (connectionString.Name.StartsWith("Xyz")) Console.WriteLine("Found one!");
}
Run Code Online (Sandbox Code Playgroud)

然而,除了解决我的直接问题外,这对我没什么帮助.我想了解这里发生了什么.

我想要做的就是使用一个简单的LINQ Where语句来获取app.config中连接字符串的子集.为什么编译器阻止我这样做?

c# linq configurationmanager connection-string

7
推荐指数
1
解决办法
2055
查看次数

Adventure Works访问客户信息

我正在使用AdventureWorks数据库进行项目,我正在尝试访问数据库,以便用户可以更改其帐户信息.该网站的用户是使用ASP.NET配置管理器设置的,所以我不确定如何去做.我有一个GETCUSTOMER选择方法设置为取(此时)刚刚在Person.Contact表的客户的名字和姓氏.

[DataObject(true)]
public static class CustomerDB
{
    [DataObjectMethod(DataObjectMethodType.Select)]
    public static List<Customer> GetCustomer()
    {
    List<Customer> CustomerList = new List<Customer>();
    SqlConnection con = new SqlConnection(GetConnectionString());


    string sel = "SELECT Person.Contact.FirstName, Person.Contact.LastName " + 
    "FROM Person.Contact " +
    "JOIN Sales.Individual ON Person.Contact.ContactID = Sales.Individual.ContactID " + 
    "JOIN Sales.Customer ON Sales.Individual.CustomerID = Sales.Customer.CustomerID " +
    "WHERE Sales.Customer.CustomerType = Sales.Individual " +
    "ORDER BY Person.Contact.LastName, Person.Contact.FirstName";

    SqlCommand cmd = new SqlCommand(sel, con);
    con.Open();
    SqlDataReader rdr =
    cmd.ExecuteReader(CommandBehavior.CloseConnection);
    Customer customer;
    while (rdr.Read())
    {
        customer = new …
Run Code Online (Sandbox Code Playgroud)

c# asp.net configurationmanager sql-server-2008

7
推荐指数
1
解决办法
254
查看次数