ConfigurationManager函数错误
我编写了一个 C# 项目,并按如下方式配置了 app.config 文件:
<appSettings>
<add key="smUserName" value= "XXX"/>
<add key="smPassword" value= "XXX"/>
</appSettings>
但是我无法从 app.config 获取 smUserName
和 smPassword
值:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果问题是代码无法编译,请尝试添加对 System.Configuration 程序集的引用。
If the problem is that the code does not compile, try adding a reference to the
System.Configuration
assembly.给你什么错误?
您也可以尝试这样做:
编辑
如果您的错误是
如评论中所述,您需要将对 System.Configuration 的引用添加到您的项目中。
What error is giving to you?
You could also try with this:
EDIT
IF your error is
as stated in the comment, you need to add the reference to System.Configuration to your project.
如果代码不起作用,请添加对网站的引用,希望它是您尝试访问配置管理器的网站页面。
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.
您应该在 C# 中编写这样的代码
,并添加对 System.Configuration 的引用
编辑:
您也可以尝试如下
You should write your code like this in C#
And also add a reference to
System.Configuration
EDIT:
You can also try like follows