How to get the path to the content file in the class library

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

I know in ASP, I can do this:

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

And in a console application, I can do this:

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

Those work, but I wonder if a better, consolidated way to find the file from the assembly and return the path.

+6
source share
1 answer

The second approach works fine in all environments that you just have to change so that it doesn't look for an assembly, but a specific assembly.

The easiest way to do this:

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

An alternative would be to use built-in resources so that you can download the file directly from the assembly. Of course, this will not work if the file should be writable by the user.

+1
source

All Articles