C # Summary of methods for obtaining the current program path

Source: Internet
Author: User

A log program recently used the method for obtaining the Application Path. By the way, I sorted it out:

1.20.path with .exe

String str1 = process. getcurrentprocess (). mainmodule. filename; // obtain the file name of the currently executed exe. D: \ work \ projects \ addressmapping \ test \ bin \ debug \ test.vshost.exe

String str1 = application. executablepath; // obtain the path of the executable file that started the application, including the name of the executable file. D: \ work \ projects \ addressmapping \ test \ bin \ debug \ test. exe

String str1 = assembly. getexecutingassembly (). Location; // obtain the path of the executable file that started the application, including the name of the executable file. D: \ work \ projects \ addressmapping \ test \ bin \ debug \ test.exe [Recommended. the full path of the DLL is obtained regardless of the situation]

 

2.20.remove the path of .exe (\)

String str2 = environment. currentdirectory; // gets and sets the fully qualified path of the current directory (that is, the directory from which the process starts. D: \ work \ projects \ addressmapping \ test \ bin \ debug

For Windows Services, the path c: \ windows \ system32 is used.

String str2 = directory. getcurrentdirectory (); // obtain the current working directory of the application. D: \ work \ projects \ addressmapping \ test \ bin \ debug

For Windows Services, the path c: \ windows \ system32 is used.

String str2 = application. startuppath; // obtain the path of the executable file that started the application, excluding the name of the executable file. D: \ work \ projects \ addressmapping \ test \ bin \ debug

3.6.2 path (with \) for dropping .exe \)

String str3 = appdomain. currentdomain. basedirectory; // gets the base Directory, which is used by the Assembly conflict resolution program to detect the assembly. D: \ work \ projects \ addressmapping \ test \ bin \ debug \

String str3 = appdomain. currentdomain. setupinformation. applicationbase; // obtain or set the name of the directory containing the application. D: \ work \ projects \ addressmapping \ test \ bin \ debug \

 

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.