从外部路径执行时资源出现 FileNotFoundException
我们遇到的问题是在 msbuild 中注意到的。我们在任务中有一个可执行文件抛出 FileNotFoundException。这个LoadData.exe使用NHibernate来初始化数据,因此项目根目录下有一个NHibernate.config文件。
配置上的属性设置为“始终内容/复制”。我可以确认,在构建时,此配置文件被复制到输出目录。
在控制台窗口中,您可以从 bin/debug 目录中启动此可执行文件,并且它将正常工作而不会出现错误。从该目录外部,您会在配置文件上收到 FileNotFoundException。
该错误表明 NHibernate.config 的加载位置是相对于我们在 BUILD 目录中执行 MSBUILD 的位置,而不是 LoadData.exe 目录。这是为什么?我们不能将其绝对化为exe吗?
有为此的 BuildAction 设置吗?
The problem we are experiencing was noticed in msbuild. We have an executable in a task that is throwing a FileNotFoundException. This LoadData.exe uses NHibernate to initialize data, so there is an NHibernate.config file in the root of the project.
The properties on the config are set to Content/Copy Always. I can confirm that, on build, this config file is copied to the output directory.
In a console window, you can launch this executable from within the bin/debug directory, and it will work without an error. From outside of this directory, you get a FileNotFoundException on the config file.
The error indicates that the NHibernate.config is being loaded relative to where we are executing MSBUILD in the BUILD directory, and not the LoadData.exe directory. Why is that? Can't we make it absolute to the exe?
Is there a BuildAction setting for this?
更改代码以从程序集位置加载文件。
Change your code to load the file from the location of your assembly.