English 中文(简体)
uniform way to get application path both for windows application and asp.net application
原标题:

is there a uniform way in .NET to get application path (physical) both for windows applications and asp.net applications ??

问题回答

I don t know if it gives exactly what you are looking for, but AppDomain.CurrentDomain.BaseDirectory could be a candidate.

Update
As @AZ pointed out, there are cases when this will not return what you would typically consider the "application directory", so in the end I would say that no, there is no uniform way that will securely give you what you expect.

[Assuming you mean the bin folder] This is how I do it.

If your environment is typically Windows apps and Asp.Net, then you should:

  • Check to see if AppDomain.CurrentDomain.SetupInformation.PrivateBinPath is not empty or null.
    • If not, then check to see if it is rooted or not
      • If not, then remember this value as BinPath (it ll be bin in Asp.Net) - but beware that more custom AppDomain hosting environments might have more than one path here and could have a rooted path.

Then, either

  • It s AppDomain.CurrentDomain.BaseDirectory
  • Or Path.Combine(AppDomain.CurrentDomain.BaseDirectory, BinPath) (where BinPath is remembered from earlier).

There are some other advanced scenarios with this - Test Runners hosting a temporary domain for unit tests, and other strange things. Asp.Net also has it s dynamic folder which has been mentioned elsewhere on this answer - but that s a red-herring if you re just looking to get at the bin folder reliably.

You certainly can t just use the Entry Assembly s path - because in an Asp.Net site that will be dynamically generated and will reside in that dynamic folder.

Equally - there are things like Sql Server-hosted domains to consider, which changes the ballgame again (in this case you d get the MSSQL Server folder).

So any solution that can reliably determine this for any AppDomain is actually going to be best produced by trial and error I m afriad - until somebody comes up with a complete answer for every hosting scenario! So stick with your most common ones.

Assembly.GetExecutingAssembly().Location. It always gets you the path of the executing dll or exe (will be in bin folder for web apps) then you can deduce the app directory from it

I had the same question because I am writing a framework component that runs in unit test assemblies as well as ASP.NET web sites.

I did not test all combinations, but I did test the following code under IIS Express 7.5 and in Visual Studio while running unit tests using the CodeRush plug-in, and it worked fine.

I ll give credit to an old section of the MSDN documentation for putting me on this path: http://msdn.microsoft.com/en-us/library/aa457089.aspx#howtoexecutingapppath_topic3

Anyway, this is the code. Hope it helps:

string binPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
string appPath = binPath.Substring(0, binPath.LastIndexOf("bin", StringComparison.InvariantCultureIgnoreCase));




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签