如何从AppSettings.Config文件中获取键值?

时间:2011-07-21 11:01:48

标签: c# appsettings

我正在尝试在appsettings.Config文件中设置我的键值,但似乎无效。

这就是我为此写的。代码从MDI文件的构造函数调用,并且仅返回null值。有谁知道为什么?

     var getValue = ConfigurationSettings.AppSettings["ShowQueryTextbox"];

我也尝试过使用ConfigurationManager.AppSettings。那也没有用。

我的AppSettings代码如下。

<configuration>
  <appSettings>
    <add key="ShowQueryTextbox" value="true"/>
  </appSettings>
</configuration>

8 个答案:

答案 0 :(得分:24)

ConfigurationSettings.AppSettings已过时,请尝试

ConfigurationManager.AppSettings["ShowQueryTextbox"];

答案 1 :(得分:13)

请记住要使用:

ConfigurationManager.AppSettings["MyKey"];

您需要在项目中添加对 System.Configuration 的引用。

答案 2 :(得分:4)

将App.Config文件重命名为AppSettings.Config会出现此问题。感谢所有的指导和帮助。

答案 3 :(得分:1)

我能够这样:

System.Configuration.ConfigurationManager.AppSettings.Get("KEY").ToString();

答案 4 :(得分:0)

假设您已将其添加到所需的配置文件中,您是否可以检查您尝试访问它的密钥是否区分大小写,因此如果您键入了不同的情况,则不会返回预期值。

答案 5 :(得分:0)

如果您在错误的配置文件中有appsettings,也会出现此错误 - 例如在WCF应用程序中,它应该是托管项目中的那个

答案 6 :(得分:0)

检查public class MainActivity extends Activity { Button AN,PL; AutoCompleteTextView autocomplete; Button clear; ListView lv; SearchView search_view; String[] animals_names; ArrayList<Main> animalslist; AnimalsAdapter adapter; @SuppressWarnings("rawtypes") Class[] classes = { A.class, B.class, C.class, D.class, E.class, F.class, G.class, H.class, I.class, }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.b_activity_main); AN= (Button) findViewById(R.id.button1); PL= (Button) findViewById(R.id.button2); lv = (ListView) findViewById(R.id.list_view); autocomplete = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1); clear = (Button) findViewById(R.id.clear); clear.setVisibility(View.INVISIBLE); animals_names = getResources().getStringArray(R.array.animals_names); animalslist = new ArrayList<Main>(); for (int i = 0; i < animals_names.length; i++) { Main country = new Main(i,animals_names[i], classes[i]); animalslist.add(country); } adapter = new AnimalsAdapter(getApplicationContext(), animalslist); final ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1, animals_names); autocomplete.setAdapter(this.adapter); autocomplete.setThreshold(1); autocomplete.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Main item = (Main) parent.getAdapter().getItem(position); Intent d = new Intent(MainActivity.this, item.getClazz()); startActivity(d); /** Fading Transition Effect */ MainActivity.this.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); } }); autocomplete.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable s) { //do nothing } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { //do nothing } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if(s.length() != 0) { clear.setVisibility(View.VISIBLE); } else { clear.setVisibility(View.GONE); } } }); autocomplete.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { clear.setVisibility(View.VISIBLE); } @Override public void onNothingSelected(AdapterView<?> arg0) { clear.setVisibility(View.GONE); } }); } public void clear(View view) { autocomplete.setText(""); clear.setVisibility(View.GONE); }

答案 7 :(得分:0)

ConfigurationManager仍然是最新的 - 2017年。

顺便说一句,如果您只是想将appsettings配置值从string转换为bool,那么请使用Convert.ToBoolean

    if (Convert.ToBoolean(ConfigurationManager.AppSettings["EnableLoggingInfo"]))
    {
        log.Info(message);
    }

在您的appsettings配置(web.config)

<appSettings>
    <add key="EnableLoggingInfo" value="true" />

  </appSettings>