【问题标题】:ConfigurationManager function errorConfigurationManager 功能错误
【发布时间】:2011-12-29 09:56:32
【问题描述】:

我编写了一个 C# 项目,我配置了 app.config 文件如下:

<appSettings>
   <add key="smUserName" value= "XXX"/>
   <add key="smPassword" value= "XXX"/>
</appSettings>

但是我无法从 app.config 中获取 smUserNamesmPassword 值:

using System.Configuration;

// more code snipped...

public static string smUserName = ConfigurationManager.AppSettings.Get("smUserName").ToString();
public static string smPassword = ConfigurationManager.AppSettings.Get("smPassword").ToString();

尽管有using System.Configuration 语句,编译此代码会给出错误“当前上下文中不存在名称'ConfigurationManager'”。

【问题讨论】:

  • 试试ConfigurationManager.AppSettings["smUserName"]ConfigurationManager.AppSettings["smPassword"]
  • 您遇到了哪个错误?运行时错误,或者项目没有编译?
  • 错误是什么,很可能是错误在告诉你问题是什么!
  • 错误是“当前上下文中不存在名称'ConfigurationManager'”

标签: c# web-services app-config console-application


【解决方案1】:

你应该在 C# 中这样编写代码

public static string smUserName = ConfigurationSettings.AppSettings["smUserName"];
public static string smPassword = ConfigurationSettings.AppSettings["smPassword"];

并且还要添加对System.Configuration的引用

编辑:

你也可以像下面这样尝试

try
{
    AppSettingsSection appSettings =
        config.AppSettings as AppSettingsSection;
    Console.WriteLine("Section name: {0}",
            appSettings.SectionInformation.SectionName);

    // Get the AppSettings section elements.
    Console.WriteLine();
    Console.WriteLine("Using AppSettings property.");
    Console.WriteLine("Application settings:");
    // Get the KeyValueConfigurationCollection 
    // from the configuration.
    KeyValueConfigurationCollection settings =
      config.AppSettings.Settings;

    // Display each KeyValueConfigurationElement.
    foreach (KeyValueConfigurationElement keyValueElement in settings)
    {
        Console.WriteLine("Key: {0}", keyValueElement.Key);
        Console.WriteLine("Value: {0}", keyValueElement.Value);
        Console.WriteLine();
    }
}
catch (ConfigurationErrorsException e)
{
    Console.WriteLine("Using AppSettings property: {0}",
        e.ToString());
}

【讨论】:

  • 我也一直使用索引器,但不应该等同于Get吗?
  • ConfigurationSettings.AppSettings 已过时,已被 ConfigurationManager.AppSettings 取代..
  • @PaoloTedesco,是的,它确实相当于 get 方法。
【解决方案2】:

如果问题是代码无法编译,请尝试添加对System.Configuration 程序集的引用。

【讨论】:

  • 感谢您的帮助。我添加了参考,问题解决了。但首先我编码,我使用 Syste.Configuration 添加但没有添加为什么?
  • @brsvdn:添加 using 语句和程序集引用是两个完全不同的东西。添加 using 语句时不会自动添加程序集引用(可以在多个程序集中使用相同的命名空间)。
【解决方案3】:

给你什么错误?

你也可以试试这个:

System.Configuration.AppSettingsReader reader;
public static string smPassword = reader.GetValue("smUserName", typeof(string)).ToString();

编辑

如果你的错误是

错误是“名称‘ConfigurationManager’不存在于 当前上下文”

如评论中所述,您需要将 System.Configuration 的引用添加到您的项目中。

【讨论】:

    【解决方案4】:
    using System.Configuration;
    // all other using as required.
    
    public partial class Admin_UserControls_GridDisplay : System.Web.UI.UserControl
    {
        public static string smUserName = ConfigurationManager.AppSettings["smUserName"].ToString();
        public static string smPassword= ConfigurationManager.AppSettings["smPassword"].ToString();
    }
    

    如果代码不起作用,请添加对网站的引用,希望它是您尝试访问配置管理器的网站页面。

    【讨论】:

      猜你喜欢
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 2013-02-13
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      相关资源
      最近更新 更多