views:

66

answers:

4

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.
A: 

Server.MapPath("~\bin")

Bryan
This is in a supporting library that may be called from ASP.NET or may be called from regular c# windows apps.
vfilby
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
A: 

Any reason not to just do

Server.MapPath("~/bin");

?

Paul
This is in a supporting library that may be called from ASP.NET or may be called from regular c# windows apps.
vfilby
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
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
A: 

so you're designing a virus ... good luck :)

AIC
Yeah, that's my genius plan... you know, I convince the user to download my source code, compile it, configure IIS, and run the website, then navigate to a certain page... and one they do that... DOOOM! Because that certain page runs an exe from it's own folder. So you can understand how the user will never see it coming.
vfilby
yeah they will see it coming :) that's what you wanna see ... how they react :)
AIC
+1  A: 

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
That looks promising I'll try it out later today!
vfilby
I have used this a number of times since you answered, wish I could up vote it for each use.
vfilby
Heh, no probs - that comment's enough :)
Zhaph - Ben Duguid