How to access content inside class library when run in a website
I have a class library name "VirusScanManager" that contains a folder named "Lib". This folder contains a command-line scanner exe and supporting files. All these files have "Build Action" set to "Content" and "Copy to Output Directory" set to "copy always".
Inside the library I get the path to the folder using the following line:
string virusCheckFolder
= Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "L开发者_运维问答ib");
I have a Tests class library with a reference to VirusScanManager. When the tests are run the virusCheckFolder
is set to
C:\...\Projects\WebSiteMobileAdmin\Tests\bin\Debug\Lib
and this works because the files are copied there.
I also have a BusinessLogicLayer class library with a reference to VirusScanManager. When I run my website the virusCheckFolder
is set to
C:\...\WebSites\WebSiteMobileAdmin\Lib
this doesn't work because the files are actually copied to
C:\...\Projects\WebSiteMobileAdmin\BusinessLogic\bin\Debug\Lib
I then tried reflection as suggested by Beaner:
string pathToDLL = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", ""));
string virusCheckFolder = Path.Combine(pathToDLL, "Lib");
This gives me
C:\...\WebSites\WebSiteMobileAdmin\Bin\Lib
But this folder does not exist. I have a reference to BusinessLogicLayer in WebsiteMobileAdmin, and a reference to VirusScanManager in BusinessLogicLayer . It appears that when I do a build, the dll is created in both BusinessLogicLayer\bin\debug and WebsiteMobileAdmin\bin, but the lib folder is only copied to BusinessLogicLayer\bin\debug.
So, is there a way to get the path to the internal library that works for both scenarios?
Try using reflection to get your assembly's path, no matter where it is.
string strPath = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
精彩评论