为什么config.Appsettings.Settings [" MySetting"。值在Windows 7中失败,而不是其他版本而不是、版本、Settings、Appsett

2023-09-06 14:09:46 作者:旧忆ゞ

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

  

System.NullReferenceException:未将对象引用设置到对象的实例

 字符串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

.net里面app.config configuration appSettings value里面链接不能填写,是有特殊字符吗

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:

System.NullReferenceException: Object reference not set to an instance of an object.

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