2013-04-24 39 views
0

我有一個簡單的C#控制檯應用程序,需要從app.config文件中提取幾個值。問題是它似乎打開一個單獨的,隱藏的配置文件,我無法找到或找出如何解決。錯誤的app.config加載

下面是我在做什麼,有一些額外的廢話添加和名稱改變我一直在使用試錯了這一點:

 
var configPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; 

var fileMap = new ExeConfigurationFileMap(); 

fileMap.RoamingUserConfigFilename = configPath; 
fileMap.ExeConfigFilename = configPath; 
fileMap.LocalUserConfigFilename = configPath; 

var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); 

ConfigurationSection section = config.GetSection("appSettings"); 

section.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToApplication; 

section.SectionInformation.UnprotectSection(); 
section.SectionInformation.ForceSave = true; 
string value = config.AppSettings.Settings["ValueHere"].Value; 
string otherValue = ConfigurationManager.AppSettings["ValueHere"].ToString(); 

的用configPath指向正確的路徑在哪裏包含app.config文件位於。 app.config的名稱是APPLICATION_NAME.exe.config。

如果我刪除配置文件,應用程序將愉快地返回值,所以我100%肯定它引用了一些其他文件,我只是不知道該文件在哪裏或如何解決它。

在調試中運行應用程序指向正確的路徑,但當然這位於bin - > debug文件夾中,並且很少使用。只有在安裝測試和生產時纔會發生此問題。

編輯:文件夾內容中只有一個app.config文件。應用程序正在搜索並在其他地方找到另一個。

+0

在「[Pragmatic Programmer](http://pragprog.com/the-pragmatic-programmer/extracts/wizards)」中,Hunt和Thomas說,如果不理解它,不要使用生成的代碼。我會補充一點,不要在不理解它的情況下使用生成的代碼。換句話說,請在製作自己的設置代碼之前查看設置設計器的功能。 – 2013-04-24 20:21:01

回答