Summarize. NET how to get the current path method

Source: Internet
Author: User
This article summarizes. NET (including Asp.net/winform, etc.) to obtain the current path of the various methods, with a certain reference value, followed by a small series to see it together

The following summarizes the various ways that. NET (including Asp.net/winform, etc.) gets the current path

Gets 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 file in the same directory as +.exe filename)

Gets the full path of the new process component and its main module associated with 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 file in the same directory as +.exe filename)

Gets and sets the fully qualified path to the current directory (that is, the directory from which the process starts).
string str = System.Environment.CurrentDirectory;
Result:x:\xxx\xxx (the directory where the. exe file resides)

Gets the base directory of the current application domain for Thread, which is used by the Assembly resolver to probe assemblies.
string str = System.AppDomain.CurrentDomain.BaseDirectory;
Result:x:\xxx\xxx\ (the directory where the. exe file is located + "\")

Gets and sets the name of the directory that contains the application.
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
Result:x:\xxx\xxx\ (the directory where the. exe file is located + "\")

Gets the path to the executable file that launched the application, not including the name of the executable file.
string str = System.Windows.Forms.Application.StartupPath;
Result:x:\xxx\xxx (the directory where the. exe file resides)

Gets the path to the executable file that launched the application, including the name of the executable file.
string str = System.Windows.Forms.Application.ExecutablePath;
Result:x:\xxx\xxx\xxx.exe (. exe file in the same directory as +.exe filename)

Gets the current working directory of the application (unreliable).
String str = System.IO.Directory.GetCurrentDirectory ();
Result:x:\xxx\xxx (the directory where the. exe file resides)

Gets 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 file in the same directory as +.exe filename)

Gets the full path of the new process component and its main module associated with 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 file in the same directory as +.exe filename)

Gets and sets the fully qualified path to the current directory (that is, the directory from which the process starts).
string str = System.Environment.CurrentDirectory;
Result:x:\xxx\xxx (the directory where the. exe file resides)

Gets the base directory of the current application domain for Thread, which is used by the Assembly resolver to probe assemblies.
string str = System.AppDomain.CurrentDomain.BaseDirectory;
Result:x:\xxx\xxx\ (the directory where the. exe file is located + "\")

Gets and sets the name of the directory that contains the application.
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
Result:x:\xxx\xxx\ (the directory where the. exe file is located + "\")

Gets the path to the executable file that launched the application, not including the name of the executable file.
string str = System.Windows.Forms.Application.StartupPath;
Result:x:\xxx\xxx (the directory where the. exe file resides)

Gets the path to the executable file that launched the application, including the name of the executable file.
string str = System.Windows.Forms.Application.ExecutablePath;
Result:x:\xxx\xxx\xxx.exe (. exe file in the same directory as +.exe filename)

Gets the current working directory of the application (unreliable).
String str = System.IO.Directory.GetCurrentDirectory ();
Result:x:\xxx\xxx (the directory where the. exe file resides)

. NET three ways to get the current path of code

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 Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.