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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With