1 回答

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊
在嘗試打開(kāi)之前AnotherApp.exe.config,ConfigurationManager.OpenExeConfiguration檢查AnotherApp.exe磁盤(pán)上是否存在。這是來(lái)源:
// ...
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;
}
如您所見(jiàn),exePath最終被傳遞到FileUtils.FileExists,最終檢查是否exePath代表磁盤(pán)上的文件。在你的情況,這是AnotherApp.exe,這并沒(méi)有存在。該throw ExceptionUtil.ParameterInvalid("exePath");語(yǔ)句是您的錯(cuò)誤的來(lái)源。
在我上面包含的源代碼中,您可以看到_applicationConfigUri設(shè)置為AnotherApp.exe.config(它是絕對(duì)路徑,但為了簡(jiǎn)單起見(jiàn),我使用了相對(duì)路徑)。當(dāng)您將 設(shè)置exePath為 時(shí)AnotherApp.exe.config,代碼最終會(huì)檢查AnotherApp.exe.config它找到的(它認(rèn)為這是 exe 本身)是否存在。在此之后,_applicationConfigUri被設(shè)置為AnotherApp.exe.config.config它不會(huì)不存在,但配置系統(tǒng)不在這種情況下錯(cuò)誤輸出(而不是返回一個(gè)空的配置對(duì)象)。
看來(lái)解決這個(gè)問(wèn)題可能有兩種選擇:
包括
AnotherApp.exe
在旁邊AnotherApp.exe.config
。使用
ConfigurationManager.OpenMappedExeConfiguration
,它允許您提供自己ExeConfigurationFileMap
的指示配置系統(tǒng)如何定位.config
文件。如果您需要這方面的幫助,請(qǐng)告訴我,我將提供一個(gè)示例,說(shuō)明這應(yīng)該如何工作。
- 1 回答
- 0 關(guān)注
- 218 瀏覽
添加回答
舉報(bào)