C#, asp.net get current, relative, absolute path

1. How to get the current path in C#:

1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName

- Get the full path to the module.

2. System.Environment.CurrentDirectory

- Gets and sets the fully qualified directory of the current directory (the directory from which the process was started).

3. System.IO.Directory.GetCurrentDirectory()

- Get the current working directory of the application. This is not necessarily the directory from which the program is started. It is possible that the program is placed in C:\www, and this function may return C:\Documents and Settings\ZYB\, or C:\Program Files\Adobe\.

4. System.AppDomain.CurrentDomain.BaseDirectory

- Get the base directory of the program.

5. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase

- Gets and sets the name of the directory containing the application.

6. System.Windows.Forms.Application.StartupPath

- Get the path to the executable file that launched the application.

7. System.Windows.Forms.Application.ExecutablePath

- Get the path and file name of the executable file that started the application.

2. Operating environment variables
Using the System.Environment.GetEnvironmentVariable() method, the system environment variables can be easily obtained, such as:
System.Environment.GetEnvironmentVariable("windir") to obtain the path of the windows system directory.
The following are some commonly used environment variable values:
System.Environment.GetEnvironmentVariable("windir")=C:\WINDOWS
System.Environment.GetEnvironmentVariable("INCLUDE")=C:\Program Files\Microsoft Visual Studio .NET 2003\SDK \v1.1\include\
System.Environment.GetEnvironmentVariable("TMP")=C:\DOCUME~1\zhoufoxcn\LOCALS~1\Temp
System.Environment.GetEnvironmentVariable("TEMP")=C:\DOCUME~1\ zhoufoxcn\LOCALS~1\Temp
System.Environment.GetEnvironmentVariable("Path")=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\jdk1.5.0\bin;C: \MySQLServer5.0\bin;C:\Program Files\Symantec\pcAnywhere\;C:

3. About "\"

1 asp.net webform

A: Request.PhysicalApplicationPath gets the physical path of the virtual directory where the site is located, and contains "\" at the end;
2.c# winform
A: "Application.StartupPath": Gets the path of the directory where the current application is located, and does not contain "\" at the end;
B: "Application.ExecutablePath": Get the path of the current application file, including the name of the file; such as: export.exe
C: "AppDomain.CurrentDomain.BaseDirectory": Get the path of the directory where the current application is located, including "\" at the end;
D : "System.Threading.Thread.GetDomain().BaseDirectory": Get the path of the directory where the current application is located, including "\" at the end;
E: "Environment.CurrentDirectory": Get the path of the current application, not including "\" at the end ";
F: "System.IO.Directory.GetCurrentDirectory": Get the path of the current application, excluding "\" at the end;

4. Uninstall the program to obtain the system installation directory

System.Reflection.Assembly curPath = System.Reflection.Assembly.GetExecutingAssembly();
string path=curPath.Location;//Get the path of the installer class SetupLibrary file, get the directory where the file path is located to get the installer directory;

Five, asp.net get the path

1.Request.ApplicationPath->In the current application directory
Jsp, ApplicationPath refers to the current application (application) directory, which is also the meaning in ASP.NET.
Correspondingly -- for example, there are two web application domain names on my server, both of which are mockte.com, one is mapped to the directory mockte.com/1/ and the other is mapped to http://mockte.com/2/
then mockte.com/1 / is the ApplicationPath of the first application. Similarly mockte.com/2/ is the ApplicationPath of the second application.

2.Request.FilePath->corresponds to the virtual directory of iis
such as URL http://mockte.com/1/index.html/pathinfo
FilePath = /1/index.html

3.Request.Path->The virtual path
Path of the current request is the concatenation of the tail of FilePath and PathInfo. For example URL http://mockte.com/1/index.html/pathinfo
then Path = /1/index.html/pathinfo

4.Request.MapPath(string url)->map the url to the virtual directory on IIS
This directory is relative to the root directory of the application.
Compared with Server.MapPath, it will not contain a path like c:/ It
is understandable is a relative path (Compared Server.MapPath is an absolute path)

5.Server.MapPath(string url)->map the url to the physical path on the server
For example http://mockte.com/1/index.html Suppose your application is in c:/iis/MySite
then it is c :/iis/MySite/1/index.html

Path conversion code:
//Convert local path to URL relative path
private string urlconvertor(string imagesurl1)
{
string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//Get program root Directory
string imagesurl2 = imagesurl1.Replace(tmpRootDir, ""); //Convert to relative path
imagesurl2 = imagesurl2.Replace(@"/", @"/");
//imagesurl2 = imagesurl2.Replace(@"Aspx_Uc/" , @"");
return imagesurl2;
}
//The relative path is converted to the server's local physical path
private string urlconvertorlocal(string imagesurl1)
{
string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString() );//Get the program root directory
string imagesurl2 = tmpRootDir + imagesurl1.Replace(@"/", @"/");//Convert to absolute path
return imagesurl2;
}

 

1. Use filePath="/Logs/abc.txt", which is considered to be the root directory, that is, the drive letter where the web page files are located. The default is C drive, then this path is interpreted as "C:/Logs/abc. TXT"

2. Use filePath="~/Logs/abc.txt", which is considered to be the directory of the server

3. Use filePath="./Logs/abc.txt", still in the server directory

 

 

Taken from csdn  hyunbar

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325078750&siteId=291194637