C # Seven methods to obtain the current path

Source: Internet
Author: User

C # The method for obtaining the current path is as follows:

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

-Obtain the complete path of the module.

2. System. Environment. CurrentDirectory

-Get and set the fully qualified directory of the current directory (the directory from which the process starts.

3. System. IO. Directory. GetCurrentDirectory ()

-Obtain the current working directory of the application. This is not necessarily the directory from which the program starts. It may be stored in C: \ www. This function may return C: \ Documents ents and Settings \ ZYB \, or C: \ Program Files \ Adobe \. Sometimes I don't know what to return.

4. System. AppDomain. CurrentDomain. BaseDirectory

-Obtain the base Directory of the program.

5. System. AppDomain. CurrentDomain. SetupInformation. ApplicationBase

-Obtain and set the Directory Name of the application.

6. System. Windows. Forms. Application. StartupPath

-Obtain the executable file path of the application. The effect is the same as that of 2 and 5. Only 5 returns an extra "\" behind the string.

7. System. Windows. Forms. Application. ExecutablePath

-Obtain the path and file name of the executable file that started the application. The effect is the same as that of 1.

For Windows and Web applications, the path they run is different, so the key is to determine which program is currently running. So we can use the following code:

String path = ""; if (System. environment. currentDirectory = AppDomain. currentDomain. baseDirectory) // Windows applications are equal... {path = AppDomain. currentDomain. baseDirectory;} else... {path = AppDomain. currentDomain. baseDirectory + "Bin \";}. If we write a class library, Assembly is used in the class library. loadFrom, because it is a common class library, it may be used in Windows programs or Web, so it is very convenient to use the above Code.

1. Server. MapPath

2. System. Windows. Forms. StartupPath

3. Type. Assembly. Location

C # obtain the current path. method 2 can be applied to console applications, WinForm applications, and Windows Services. method 1 can be applied to Web applications, and method 3 can be applied.

But method 3 is the path to load the application. For a Web application, the obtained path is: C: \ WINDOWS \ Microsoft. NET \ Framework \ v1.1.4322 \ Temporary ASP. NET Files directory. Therefore, we recommend that you use Server. MapPath for Web projects. Otherwise, method 2 is recommended. If you create a new class library. You can add a reference to System. Windows. Forms. StartupPath for use.

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.