本文介绍了为什么config.Appsettings.Settings [" MySetting"。值在Windows 7中失败,而不是其他版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读设置了使用code几乎相同,这是我在应用程序的其它部分使用过的app.config文件中。它winxp下和Win Server 2003中正常工作,当我运行Windows 7的64位它会产生一个例外情况:

 字符串exePath = System.IO.Path.Combine(Environment.CurrentDirectory,的applicationName);

//获取配置文件。文件名具有以下格式appname.exe.config。

System.Configuration.Configuration utilConfig = ConfigurationManager.OpenExeConfiguration(exePath);
字符串文件名= utilConfig.AppSettings.Settings [MsgAlertWav]值。 //<<失败在这里
 

这是简化的code,但在Windows 7下生成错误这是编译为32位目标在.NET 3.0的项目。我有同样的code在另一个模块,并能正常工作在Windows 7下。

我很迷惑,因为这code工作在一个模块,而不是另一个,不生成生成错误。

解决方案

System.Configuration.ConfigurationSettings 是德precated ,然后是为在框架1.0和1.1版本解决方案。

由于您使用的是你应该用System.Configuration.ConfigurationManager 3.0。是pretty的mcuh同样的事情,具有相同的使用

  System.Configuration.ConfigurationManager [MsgAlertWav];
 

心连心,-covo

I'm reading a setting out of the app.config file using code nearly identical to that which I've used in other portions of the app. It works fine under WinXP and Win Server 2003, when I run it under Windows 7 64-bit it generates an exception:

string exePath = System.IO.Path.Combine(Environment.CurrentDirectory, applicationName);

// Get the configuration file. The file name has this format appname.exe.config.

System.Configuration.Configuration utilConfig = ConfigurationManager.OpenExeConfiguration(exePath);
string fileName = utilConfig.AppSettings.Settings["MsgAlertWav"].Value; //<<Fails here

This is simplified code, but generates the error under Windows 7. It's a .NET 3.0 project compiled for 32-bit target. I have this same code in another module and it works fine under Windows 7.

I am mystified since this code works in one module, but not another and generates no build errors.

解决方案

System.Configuration.ConfigurationSettings is deprecated and is meant for solutions on framework versions 1.0 and 1.1.

Since you are using a 3.0 you should be using System.Configuration.ConfigurationManager. Is pretty mcuh the same thing, has the same usage

System.Configuration.ConfigurationManager["MsgAlertWav"];

hth, -covo

这篇关于为什么config.Appsettings.Settings [&QUOT; MySetting&QUOT;。值在Windows 7中失败,而不是其他版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:19