我像这样得到我的 MEF 特定 dll:
string exeFile = (new Uri(Assembly.GetEntryAssembly().CodeBase)).AbsolutePath;
string exeDir = Path.GetDirectoryName(exeFile);
using (DirectoryCatalog catalog = new DirectoryCatalog(Path.Combine(exeDir,"Custom")))
{
using (CompositionContainer container = new CompositionContainer(catalog))
{
container.ComposeParts(this);
}
}
如果我正在开发中,这就可以工作,但是如果我构建并获取构建输出并将其放在名为 c:test 1
的文件夹中,当我从 c:test
运行应用程序时,它说它找不到c:test 1custom
目录。
与EXE
位于同一路径中的Custom
文件夹
我注意到,只有当 directoy 像 test 1
一样有空格时,它才找不到它,但如果它只是test1
,它确实可以正常工作
如果我用空格运行它,我会收到错误:
Could not find part of the path 'C:TEST%202CUSTOM'.
Uri.UnescapeDataString
工作吗?
我按如下方式使用:
using (DirectoryCatalog catalog = new DirectoryCatalog(Uri.UnescapeDataString(path)))...
试试这个:
string exeFile = Assembly.GetEntryAssembly().Location;
string exeDir = Path.GetDirectoryName(exeFile);
string path = Path.Combine(exeDir, "Custom");
using (DirectoryCatalog catalog = new DirectoryCatalog(path))
{
using (CompositionContainer container = new CompositionContainer(catalog))
{
container.ComposeParts(this);
}
}
应使用位置而不是代码库 (msdn)。
MSDN 备注:
若要获取加载的包含清单的文件的绝对路径,请使用 属性改为程序集位置。