Several methods are as follows:
Non-web programs
1.appdomain.currentdomain.basedirectory
2.environment.currentdirectory
3.httpruntime.bindirectory
The path to the current application ' S/bin directory.
Web Program
HttpCurrent.Context.Server.Mappath ();
---------------------------------------------------------------
HttpContext.Current
Returns the HttpContext object for the current request. So we can directly access the request, Response, Session, application and other objects, and page access equivalent.
We no longer have to pass page parameters to our class library objects.
httpcontext.current.session["name"] = "pig";
String name = httpcontext.current.request.param["Name"];
HttpContext.Current.Response.Write ("Pig lazy!");
HttpRuntime
Provides a set of ASP. NET Runtime services for the current application. This class allows us to obtain some information about the current ASP.
Httpruntime.appdomainappvirtualpath: Project Virtual path
Httpruntime.appdomainapppath: Project Physical path
HttpRuntime.BinDirectory:BIN Directory physical Path
HttpRuntime.ClrInstallDirectory:CLR installation path (can be used to obtain the CLR version number)
Response.Write (String. Format ("Appdomainappid: {0}<br>", httpruntime.appdomainappid));
Response.Write (String. Format ("Appdomainapppath: {0}<br>", Httpruntime.appdomainapppath));
Response.Write (String. Format ("Appdomainappvirtualpath: {0}<br>", Httpruntime.appdomainappvirtualpath));
Response.Write (String. Format ("AppDomainID: {0}<br>", Httpruntime.appdomainid));
Response.Write (String. Format ("Aspinstalldirectory: {0}<br>", httpruntime.aspinstalldirectory));
Response.Write (String. Format ("Bindirectory: {0}<br>", httpruntime.bindirectory));
Response.Write (String. Format ("Clrinstalldirectory: {0}<br>", httpruntime.clrinstalldirectory));
Response.Write (String. Format ("Codegendir: {0}<br>", Httpruntime.codegendir));
Response.Write (String. Format ("Isonuncshare: {0}<br>", HttpRuntime.IsOnUNCShare.ToString ()));
Response.Write (String. Format ("Machineconfigurationdirectory: {0}<br>", httpruntime.machineconfigurationdirectory));
Output:
Appdomainappid:/lm/w3svc/1/root/learn.test.web
Appdomainapppath:d:\system\my Documents\Visual Studio projects\learn.test\learn.test.web\
Appdomainappvirtualpath:/learn.test.web
AppDomainID:/lm/w3svc/1/root/learn.test.web-9-127652564154400560
aspinstalldirectory:c:\windows\microsoft.net\framework\v1.1.4322
Bindirectory:d:\system\my Documents\Visual Studio projects\learn.test\learn.test.web\bin\
clrinstalldirectory:c:\windows\microsoft.net\framework\v1.1.4322
Codegendir:c:\windows\microsoft.net\framework\v1.1.4322\temporary ASP. files\learn.test.web\41680132\7c880883
Isonuncshare:false
Machineconfigurationdirectory:c:\windows\microsoft.net\framework\v1.1.4322\config
Hostingenvironment
string siteName = Hostingenvironment.sitename;
string appPath = Hostingenvironment.applicationvirtualpath;
string phypath = Hostingenvironment.applicationphysicalpath;
String adminpath = Hostingenvironment.mappath ("~/admin");
Applicationmanager.getapplicationmanager (). Shutdownapplication (Hostingenvironment.applicationid);
Flexible application Techniques:
When using a non-web program or using an asynchronous call, you can use the following code to obtain a directory under the root directory:
Httpruntime.bindirectory + ". /directory Name ";
There are several ways to get the root directory of a Web site, such as:
Server.MapPath (request.servervariables["Path_info"])
Server.MapPath ("/")
Server.MapPath ("")
Server.MapPath (".")
Server.MapPath (".. /")
Server.MapPath ("..")
Page.Request.ApplicationPath
Operation Result:
C:\Inetpub\wwwroot\EnglishClub\manage\WebForm1.aspx
C:\Inetpub\wwwroot\
C:\Inetpub\wwwroot\EnglishClub\manage
C:\Inetpub\wwwroot\EnglishClub\manage
C:\Inetpub\wwwroot\EnglishClub\
C:\Inetpub\wwwroot\EnglishClub
The above methods can be accessed in. aspx, but if you are in. CS file is not available.
HttpContext.Current.Server.MapPath ();
System.Web.HttpContext.Current.Request.PhysicalApplicationPath
Can be used in a. cs file.
But HttpContext.Current.Server.MapPath (); This gets the path to the file rather than the root directory.
Only System.Web.HttpContext.Current.Request.PhysicalApplicationPath this is the root directory to get, in the write to get the database path is should use this, the others have problems.
Tested, this can be used in CS files for some Web projects. But it is best to use System.Web.HttpContext.Current.Server.MapPath ("/systemmanage/hotel/loclist.xml");
In a class library project, this is usually not available, and this time it is necessary to reference the assembly, referencing the namespace system.web;
System.Environment.CurrentDirectory + @ "\ipms. Web\aboutme.txt "
C # getting directories for Web and non-web programs