1 回答

TA貢獻1856條經驗 獲得超17個贊
在嘗試打開之前AnotherApp.exe.config,ConfigurationManager.OpenExeConfiguration檢查AnotherApp.exe磁盤上是否存在。這是來源:
// ...
else {
applicationUri = Path.GetFullPath(exePath);
if (!FileUtil.FileExists(applicationUri, false))
throw ExceptionUtil.ParameterInvalid("exePath");
applicationFilename = applicationUri;
}
// Fallback if we haven't set the app config file path yet.
if (_applicationConfigUri == null) {
_applicationConfigUri = applicationUri + ConfigExtension;
}
如您所見,exePath最終被傳遞到FileUtils.FileExists,最終檢查是否exePath代表磁盤上的文件。在你的情況,這是AnotherApp.exe,這并沒有存在。該throw ExceptionUtil.ParameterInvalid("exePath");語句是您的錯誤的來源。
在我上面包含的源代碼中,您可以看到_applicationConfigUri設置為AnotherApp.exe.config(它是絕對路徑,但為了簡單起見,我使用了相對路徑)。當您將 設置exePath為 時AnotherApp.exe.config,代碼最終會檢查AnotherApp.exe.config它找到的(它認為這是 exe 本身)是否存在。在此之后,_applicationConfigUri被設置為AnotherApp.exe.config.config它不會不存在,但配置系統不在這種情況下錯誤輸出(而不是返回一個空的配置對象)。
看來解決這個問題可能有兩種選擇:
包括
AnotherApp.exe
在旁邊AnotherApp.exe.config
。使用
ConfigurationManager.OpenMappedExeConfiguration
,它允許您提供自己ExeConfigurationFileMap
的指示配置系統如何定位.config
文件。如果您需要這方面的幫助,請告訴我,我將提供一個示例,說明這應該如何工作。
- 1 回答
- 0 關注
- 219 瀏覽
添加回答
舉報