2011-10-04 61 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

相關問題