2009-08-03 39 views
5

我最近移動到Vista x64,突然,我的machine.config appSettings塊沒有被任何.NET程序集讀取。右configSections後appSettings不從Vista中的machine.config中讀取

,並在configProtectedData前C:\ WINDOWS \ Microsoft.NET \框架\ V2.0.50727 \ CONFIG \ machine.config中,我有:

<appSettings> 
    <add key="foo" value="blah"/> 
</appSettings> 
<system.runtime.remoting> 
    <customErrors mode="Off"/> 
</system.runtime.remoting> 

只好通過運行記事本保存++作爲管理員,因爲它被鎖定,否則可能是出於很好的原因。跑在SnippetCompiler或VS .NET 2008下面的代碼:

foreach(var s in ConfigurationManager.AppSettings.AllKeys) 
    { 
     Console.WriteLine(s); 
    } 

    AppSettingsReader asr = new AppSettingsReader(); 

    Console.WriteLine(asr.GetValue("foo", typeof(string))); 

寫出無鍵和失敗,出現以下異常:

--- 
The following error occurred while executing the snippet: 
System.InvalidOperationException: The key 'foo' does not exist in the appSettings configuration section. 
    at System.Configuration.AppSettingsReader.GetValue(String key, Type type) 
    at MyClass.RunSnippet() 
    at MyClass.Main() 
--- 

我寫的應用程序使用的machine.config作爲認定後備如果無法在app.config中找到用戶應該運行哪個環境,那麼我想避免重寫我的應用程序以找出應該與2000年相同的應用程序,並且XP。

回答

7

與下面的代碼行解決它:

ConfigurationManager.OpenMachineConfiguration().FilePath 

其返回:代替

C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Config\machine.config 

C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config 

忘記我使用64位現在。在適當的配置文件中添加appSettings部分解決了問題。

相關問題