7

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:\Windows\Microsoft.NET\Framework\\...\Temporary ASP.NET Files\a1388a5e\\...\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.
vfilby
  • 9,938
  • 9
  • 49
  • 62
  • When it's called from "other console/windows apps" how does it know about the website? – Zhaph - Ben Duguid Mar 11 '10 at 20:20
  • > "How can I get the location of the binary, not asp.net's temporary cache?" Which binary? Your own, or the calling binary? – Mark Brackett Mar 11 '10 at 21:18
  • It doesn't, nor do I want it to. It's a c# library that has a 3rd party .exe that is copied into the final bin/[Config] directory along with the libraries .dll. The library has to run the .exe and since all different builds end up in different locations it has to either be configured or configure itself. Right now I am configuring it (in the global.asax because I have to find the bin dir for the website). I was hoping to have it just be able to detect where the bin is. – vfilby Mar 11 '10 at 21:18

3 Answers3

14

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 \Documents\Visual Studio 2008\Websites\ Solution \ Project \bin

Which is what I'd expect.

Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117
4

Server.MapPath("~\bin")

Bryan
  • 8,748
  • 7
  • 41
  • 62
  • This is in a supporting library that may be called from ASP.NET or may be called from regular c# windows apps. – vfilby Mar 11 '10 at 19:13
  • In that case, it needs to either be part of the config for the supporting library, or you need to pass in the desired path in one of its methods. – Bryan Mar 11 '10 at 21:09
0

Any reason not to just do

Server.MapPath("~/bin");

?

Paul
  • 35,689
  • 11
  • 93
  • 122
  • This is in a supporting library that may be called from ASP.NET or may be called from regular c# windows apps. – vfilby Mar 11 '10 at 19:15
  • what are you trying to do w/ the assembly(ies) once you've found it(/them)? Do you even need to know the physical file location? – Paul Mar 11 '10 at 20:40
  • Its an exe that I need to run that is located in bin/[Configuration]. Right now I am manually configuring the location. I was hoping to make it a bit more generic. – vfilby Mar 11 '10 at 20:55