获取参考本地大会的位置

问题描述 投票:-1回答:2

我在我的解决方案中有ProjectB引用的ProjectA。我正在尝试在运行ProjectB时获得ProjectA的“路径”。

我试过这个:

在ProjectB中运行

// HomeController is in ProjectA
// This code is executed from ProjectB
var assembly = typeof(HomeController ).Assembly;
string filePath = new Uri(assembly.CodeBase).LocalPath;

返回ProjectB路径,而不是ProjectA路径。

编辑:这显示路径中的UnitTestProject1。我想获得WebApplication1的路径。

enter image description here

c# asp.net .net
2个回答
0
投票

要获取引用汇编的位置,请使用此选项

System.Reflection.Assembly.GetAssembly(typeof(SmsController)).Location;

0
投票

我认为您必须确保要引用的文件被复制到项目B(主项目)的bin文件夹中。然后你可以使用这段代码:

    string code = Assembly.GetExecutingAssembly().CodeBase;
    UriBuilder uri = new UriBuilder(code);
    string path = Uri.UnescapeDataString(uri.Path);
    return Path.GetDirectoryName(path);

这将返回bin文件夹的路径。

我认为您必须将要引用的文件添加为嵌入资源或链接资源:https://msdn.microsoft.com/en-us/library/ht9h2dk8(v=vs.100).aspx

© www.soinside.com 2019 - 2024. All rights reserved.