Get the application root physical path (Web and Windows)

Source: Internet
Author: User

These two plans write a small class library that needs to be called arbitrarily under different projects. This class library needs to read and write the disk files, so it is necessary to get the disk path of the program execution, and simply study the method of acquiring the disk path.

With the help of the search engine, I have collected many methods from the Internet, which can get the root directory of application execution directly or indirectly. Roughly summed up, there are altogether the following 11 kinds:

Server.MapPath ("~")//Use the Server object in the HTTP context to get the root directory of the Web siteSystem.AppDomain.CurrentDomain.BaseDirectory//Use the application domain object to get the base directory of the application domain for the current threadSystem.AppDomain.CurrentDomain.SetupInformation.ApplicationBase//Use the application domain object to get the application directory in the configuration information for the current thread's application domainSystem.Diagnostics.Process.GetCurrentProcess (). Mainmodule.filename//Gets the file name (full path) of the main module of the current process. The directory where the assembly is located can be obtained by the file path)System.Environment.CurrentDirectory//gets the current directory of the application environmentSystem.IO.Directory.GetCurrentDirectory ()//get the path to the current program using the GetCurrentDirectory method under static class DirectorySystem.Reflection.Assembly.GetCallingAssembly (). Location//Gets the assembly that contains the method that called the method, and gets the assembly file path (the directory where the assembly is located by the file path)System.Reflection.Assembly.GetEntryAssembly (). Location//Gets the assembly (executable file) that contains the entry point for the application, and gets the path to the assembly file (the directory where the assembly is located by the file path)System.Reflection.Assembly.GetExecutingAssembly (). Location//gets the assembly that executes the method, and gets the file path to the assembly (the directory where the assembly is located by the file path)System.Windows.Forms.Application.StartupPath//gets the directory where the executable file for the startup application residesSystem.Windows.Forms.Application.ExecutablePath//gets the path to the executable file that launches the application (the directory where the application is located by the file path)

Get the application root physical path (Web and Windows)

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.