2011-10-04 63 views
2

我正在读取app.config文件中的设置,使用的代码几乎与我用过的代码相同在应用程序的其他部分。它在WinXP和Win Server 2003下工作正常,当我在Windows 7 64位下运行它时会产生一个异常:为什么config.Appsettings.Settings [“MySetting”]。值在Windows 7中失败,但不是其他版本

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

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 

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

我很迷惑,因为此代码在一个模块中工作,但不会产生任何生成错误。

+1

这是最有可能的问题的权利。用户无权写入程序文件。你需要升级才能做到这一点(这可能不是一个好主意)。没有为你设置用户特定的设置吗? – svick

+0

那么,我实际上试图从app.config中读取。用户组具有对该文件夹的读取权限。在这种情况下,我认为这不是一个权利问题。它在另一个模块中读取同一文件夹中的不同app.config时正常工作。 –

回答

0

尝试用

System.Configuration.ConfigurationSettings.AppSettings["MsgAlertWav"]; 

或者看看

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) 
0

System.Configuration.ConfigurationSettings 已被弃用,其目的是为解决方案的框架版本1.0和1.1。

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

System.Configuration.ConfigurationManager["MsgAlertWav"]; 

心连心,-covo

相关问题