Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file path of content in class library

I have an Excel file that has a Build Action of content in a class library. The library is referenced by both an ASP.NET WebForms application and a console application, and the Excel file is used by both. Is there a way I can programmatically get the file path of this file?

I know in ASP I can do this:

HttpContext.Current.Server.MapPath(@"bin")

And in the console app I can do this:

Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)

Those work, but I'm wondering if there's better, consolidated way to find the file from the assembly and return the path.

like image 558
Dave Zych Avatar asked Oct 30 '12 17:10

Dave Zych


1 Answers

The second approach works fine in all environments you just should change it so that it does not look for the entry assembly but the specific assembly.

Easiest way to do this is:

typeof(Some.Type.From.That.Assembly).Assembly.Location

An alternative would be to use embedded resources so that you can load the file directly from the assembly. Of course, this would not work if the file has to be writable by the user.

like image 111
Knaģis Avatar answered Oct 13 '22 13:10

Knaģis