【问题标题】:How to access content inside class library when run in a website在网站中运行时如何访问类库中的内容
【发布时间】:2010-01-13 18:04:14
【问题描述】:

我有一个名为“VirusScanManager”的类库,其中包含一个名为“Lib”的文件夹。此文件夹包含命令行扫描程序 exe 和支持文件。所有这些文件都将“构建操作”设置为“内容”,将“复制到输出目录”设置为“始终复制”。

在库中,我使用以下行获取文件夹的路径:

string virusCheckFolder 
= Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lib");

我有一个引用 VirusScanManager 的测试类库。运行测试时,virusCheckFolder 设置为

C:\...\Projects\WebSiteMobileAdmin\Tests\bin\Debug\Lib

这是有效的,因为文件被复制到那里。

我还有一个引用 VirusScanManager 的 BusinessLogicLayer 类库。当我运行我的网站时,virusCheckFolder 设置为

C:\...\WebSites\WebSiteMobileAdmin\Lib

这不起作用,因为文件实际上已复制到

C:\...\Projects\WebSiteMobileAdmin\BusinessLogic\bin\Debug\Lib

然后我按照 Beaner 的建议尝试了反射:

string pathToDLL = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", ""));
string virusCheckFolder = Path.Combine(pathToDLL, "Lib");

这给了我

C:\...\WebSites\WebSiteMobileAdmin\Bin\Lib

但是这个文件夹不存在。 我在 WebsiteMobileAdmin 中有对 BusinessLogicLayer 的引用,在 BusinessLogicLayer 中有对 VirusScanManager 的引用。看来,当我进行构建时,会在 BusinessLogicLayer\bin\debug 和 WebsiteMobileAdmin\bin 中创建 dll,但 lib 文件夹仅复制到 BusinessLogicLayer\bin\debug。

那么,有没有办法获得适用于这两种情况的内部库的路径?

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    尝试使用反射来获取程序集的路径,无论它在哪里。

    string strPath = System.IO.Path.GetDirectoryName(
        System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
    

    【讨论】:

    • 请看我的编辑。我试过这个,但似乎有 2 个 dll 副本,并且它使用的是没有 Lib 的版本
    • 我很抱歉,因为我无法理解您在做什么,但如果我没看错,您的 Tests 类位于 WebSiteMobileAdmin 命名空间中。它应该在 WebSiteMobileAdmin\BusinessLogic 命名空间中吗?
    • 有 3 个类库,分别名为 BusinessLogicLayer、Tests 和 VirusScanManager,每个类库都有自己的命名空间。 WebSiteMobileAdmin 是一个网站,它引用了 BusinessLogicLayer。 BusinessLogicLayer 引用了 VirusScanManager。 VirusScanManager 使用 exe,需要在文件夹中创建文件。所以我在 VirusScanManager 项目中创建了一个子文件夹。我想在 VirusScanManager 内部获取文件夹的路径,这样我就可以重新使用类库,而不必在调用代码中创建文件夹并传递路径。这有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 2021-06-02
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多