如何使用ConfigurationManager [英] How to use ConfigurationManager

查看:221
本文介绍了如何使用ConfigurationManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用App.config来存储一些设置。
我试图使用下一个代码从配置文件中获取参数。

  private string GetSettingValue(string paramName )
{
return String.Format(ConfigurationManager.AppSettings [paramName]);
}

我也添加了 System.Configuration 为它(我使用一个单独的类),在App.config文件中我有:

 <?xml version =1.0encoding =utf-8?> 
< configuration>
< startup>
< supportedRuntime version =v4.0sku =。NETFramework,Version = v4.5/>
< / startup>
< appSettings>
< add key =key1value =Sample/>
< / appSettings>
< / config>

但我在尝试使用 ConfigurationManager时遇到错误 - ConfigurationManager不能存在于上下文中,但我已经添加了 System.Configuration



class with config(full) view)

 使用System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

命名空间浏览器
{
class ConfigFile
{
private string GetSettingValue(string paramName)
{
return String.Format (ConfigurationManager.AppSettings [paramName]);
}
}
}

EDIT2



添加外观





这意味着在使用 ConfigurationManger 但之前 - 程序说它不知道这样的元素,因为我理解错误 - 元素配置管理器不存在于这样的上下文



EDIT3





编辑4



解决方案

Okay,我花了一段时间看到这个,但是没有办法编译:

  return String。 AppSettings [paramName]); 

你甚至不需要调用 String 类型。只需这样做:

 返回ConfigurationManager.AppSettings [paramName]; 

AppSettings KVP已返回字符串。如果名称不存在,它将返回 null






根据您的修改,您尚未< System.Configuration 程序集中添加引用


I want to use App.config for storing some setting. I tried to use the next code for getting a parameter from a config file.

private string GetSettingValue(string paramName)
{
    return String.Format(ConfigurationManager.AppSettings[paramName]);
}

I also added System.Configuration for it (I used a separate class), and in App.config file I have:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <add key ="key1" value ="Sample" />
  </appSettings>
</configuration>

But I got an error while trying to use ConfigurationManager - ConfigurationManager can't exist in such context, but I already added System.Configuration. Or did I miss something?

EDIT:

class with config (full view)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace browser
{
    class ConfigFile
    {
        private string GetSettingValue(string paramName)
        {
            return String.Format(ConfigurationManager.AppSettings[paramName]);
        }
    }
}

EDIT2

Add how it looks

This means the problem is not during using ConfigurationManger but before - the program "says" that it "doesn't know such element" as I understand the error - the "Element ConfigurationManager" doesn't exist in such context"

EDIT3

EDIT 4

解决方案

Okay, it took me a while to see this, but there's not way this compiles:

return String.(ConfigurationManager.AppSettings[paramName]);

You're not even calling a method on the String type. Just do this:

return ConfigurationManager.AppSettings[paramName];

The AppSettings KVP already returns a string. If the name doesn't exist, it will return null.


Based on your edit you have not yet added a Reference to the System.Configuration assembly for the project you're working in.

这篇关于如何使用ConfigurationManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆