Home >Backend Development >C#.Net Tutorial >Share N kinds of .NET examples of obtaining the current path

Share N kinds of .NET examples of obtaining the current path

Y2J
Y2JOriginal
2017-05-16 10:19:261363browse

This article summarizes various methods for obtaining the current path in .NET (including ASP.NET/WinForm, etc.), which has certain reference value. Let’s take a look at it with the editor.

//Get the current path The full path of the process, including the file name (process name).

string str = this.GetType().Assembly.Location;   result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)

//Get the new Process component and associate it with the full path of the main module of the currently active process, including the file name (process name).

 string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;   result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)

//Gets and sets the fully qualified path of the current directory (that is, the directory from which the process was started).

 string str = System.Environment.CurrentDirectory;   result: X:\xxx\xxx (.exe文件所在的目录)

//Get the base directory of the current application domain of the current Thread, which is used by the assembly conflict resolver to detect the assembly.

 string str = System.AppDomain.CurrentDomain.BaseDirectory;   result: X:\xxx\xxx\ (.exe文件所在的目录+"\")

//Get and set the name of the directory containing the application.

 string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;   result: X:\xxx\xxx\ (.exe文件所在的目录+"\")

//Get the path to the executable file that started the application, excluding the name of the executable file.

  string str = System.Windows.Forms.Application.StartupPath;   result: X:\xxx\xxx (.exe文件所在的目录)

//Get the path to the executable file that started the application, including the name of the executable file.

 string str = System.Windows.Forms.Application.ExecutablePath;   result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)

//Get the current working directory of the application (unreliable).

 string str = System.IO.Directory.GetCurrentDirectory();   result: X:\xxx\xxx (.exe文件所在的目录)

//Get the full path of the current process, including the file name (process name).

string str = this.GetType().Assembly.Location;result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)

//Get the new Process component and associate it with the full path of the main module of the currently active process, including the file name (process name).

string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)

//Gets and sets the fully qualified path of the current directory (i.e. the directory from which the process was started).

string str = System.Environment.CurrentDirectory;result: X:\xxx\xxx (.exe文件所在的目录)

//Get the base directory of the current application domain of the current Thread, which is used by the assembly conflict resolver to detect the assembly.

string str = System.AppDomain.CurrentDomain.BaseDirectory;result: X:\xxx\xxx\ (.exe文件所在的目录+"\")

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

string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;result: X:\xxx\xxx\ (.exe文件所在的目录+"\")

//Get the path to the executable file that started the application, excluding the name of the executable file.

string str = System.Windows.Forms.Application.StartupPath;result: X:\xxx\xxx (.exe文件所在的目录)

//Get the path to the executable file that started the application, including the name of the executable file.

string str = System.Windows.Forms.Application.ExecutablePath;result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)

//Get the current working directory of the application (unreliable).

string str = System.IO.Directory.GetCurrentDirectory();result: X:\xxx\xxx (.exe文件所在的目录)

Three codes for obtaining the current path in .NET

//Web Programming

 HttpContext.Current.Server.MapPath("FileName")   System.Web.HttpContext.Current.Request.Path

//Windows Programming

 System.Environment.CurrentDirectory

//Mobile Programming

 Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

【Related Recommendations】

1. Special Recommendations:"php programmer tools "Box" V0.1 version download

2. ASP free video tutorial

3. Li Yanhui ASP basic video tutorial

The above is the detailed content of Share N kinds of .NET examples of obtaining the current path. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn