Can I use reflection to find the bin/[Configuration] folder in ASP.NET instead of the asp temporary folder

I have an ASP.NET website and I want to find the /bin/[Configuration] folder to use an external tool (an exe file). When I use reflection to get calling assemblies location it returns something similar to:

C:WindowsMicrosoft.NETFramework\...Temporary ASP.NET Filesa1388a5e\...my.dll

Since each dll has its own directory under the temp ASP.NET Files this fails for me.

How can I get the location of the compiled binary folder where the dll’s and the .exe is (i.e. bin/) instead of asp.net’s temporary cache?

Notes

  • This code is in a supporting library that can be called from ASP.NET websites or other console/windows apps.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You could try (taken from How to: Get the Application Directory):

Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

I set up a quick test with:

  • A Web Application project running under the VS dev server (compiled site, /bin directory, etc).
  • A class library project, referenced by the WA.
  • In the web application I created a page that called the following method from the library project:
    using System.IO;
    using System.Reflection;
    
    namespace TestHelpers {
      public class ClassHelpers {
        public static string PathToBin() {
          return Path.GetDirectoryName(
                   Assembly.GetExecutingAssembly().GetName().CodeBase);
        }
      }
    }

This resulted in the following output on the page:

file:C:Users UserName DocumentsVisual Studio 2008Websites Solution Project bin

Which is what I’d expect.

Method 2

Server.MapPath(“~bin”)

Method 3

Any reason not to just do

Server.MapPath("~/bin");

?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x