ConfigurationManager函数错误

发布于 2024-12-15 18:08:08 字数 657 浏览 0 评论 0原文

我编写了一个 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();

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

I coded a C# project, and I configured app.config file as follows:

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

But then I cannot get the smUserName and smPassword values from app.config:

using System.Configuration;

// more code snipped...

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

Despite the using System.Configuration statement, compiling this code gives the error "The name 'ConfigurationManager' does not exist in the current context".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梨涡少年 2024-12-22 18:08:08

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

If the problem is that the code does not compile, try adding a reference to the System.Configuration assembly.

初相遇 2024-12-22 18:08:08

给你什么错误?

您也可以尝试这样做:

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

编辑

如果您的错误是

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

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

What error is giving to you?

You could also try with this:

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

EDIT

IF your error is

the error is "The name 'ConfigurationManager' does not exist in the
current context"

as stated in the comment, you need to add the reference to System.Configuration to your project.

信愁 2024-12-22 18:08:08
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();
}

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

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

If the code does not work add reference to the website, hope it is website page where u are trying to access the configuration Manager.

醉城メ夜风 2024-12-22 18:08:08

您应该在 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());
}

You should write your code like this in C#

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

And also add a reference to System.Configuration

EDIT:

You can also try like follows

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());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文