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

Y2J
Release: 2017-05-16 10:19:26
Original
1312 people have browsed it

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文件名)
Copy after login

//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文件名)
Copy after login

//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文件所在的目录)
Copy after login

//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文件所在的目录+"\")
Copy after login

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

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

//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文件所在的目录)
Copy after login

//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文件名)
Copy after login

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

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

//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文件名)
Copy after login

//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文件名)
Copy after login

//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文件所在的目录)
Copy after login

//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文件所在的目录+"\")
Copy after login

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

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

//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文件所在的目录)
Copy after login

//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文件名)
Copy after login

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

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

Three codes for obtaining the current path in .NET

//Web Programming

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

//Windows Programming

 System.Environment.CurrentDirectory
Copy after login

//Mobile Programming

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

【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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!