C # Current Application Path and Environment Variables

Source: Internet
Author: User

1. Obtain the path of the current file

1. system. Diagnostics. process. getcurrentprocess (). mainmodule. filename

Obtain the complete path of the module. Obtain the name of the currently executed EXE file.

2. system. environment. currentdirectoryObtain and set the fully qualified directory of the current directory from which the process starts. Obtain and set the full path of the current directory (that is, the directory from which the process starts. (Note: As defined, if the process is started in the local or root directory of the network drive, the value of this attribute is the drive name followed by a backslash (such as "C: \"). If the process is started in a subdirectory, the value of this attribute is a drive and subdirectory path [such as "C: \ mysubdirectory"] without a backslash.

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 not necessarily return anything. This is the last operation directory of any application. For example, if you open the file E: \ doc \ my.doc in word, then, E: \ doc is returned.

4. system. appdomain. currentdomain. basedirectoryGets the base Directory of the program, which is used by the Assembly conflict resolution program to detect the assembly.5. system. Windows. Forms. application. startuppathObtain the path of the executable file that started the application, excluding the name of the executable file. The effect is the same as that of 2. Only 5 returns an extra "\" after the string.6. system. Windows. Forms. application. executablepathObtain the path and file name of the executable file that started the application. The effect is the same as that of 1.7. system. appdomain. currentdomain. setupinformation. applicationbaseObtain and set the name of the directory containing the application.

 

8.This. GetType (). Assembly. Location

Obtain the complete path of the current process, including the file name.

 

Ii. Operating Environment Variables

 

The system. environment. getenvironmentvariable () method can be used to conveniently obtain system environment variables, such as system. environment. getenvironmentvariable ("WINDIR. The following are common environment variable values: system. environment. getenvironmentvariable ("WINDIR"); system. environment. getenvironmentvariable ("include"); system. environment. getenvironmentvariable ("tmp"); system. environment. getenvironmentvariable ("Temp"); system. environment. getenvironmentvariable ("path ");

3. Application Instances

A winform program is compiled. The project file is stored in D: \ projects \ demo, and the compiled file is in D: \ projects \ demo \ bin \ Debug. The final result is as follows:

1, System. Diagnostics. process. getcurrentprocess (). mainmodule. filename = D: \ projects \ demo \ bin \ debug \ demo.vshost.exe2, System. environment. currentdirectory = D: \ projects \ demo \ bin \ debug3, System. Io. Directory. getcurrentdirectory () = D: \ projects \ demo \ bin \ debug4, System. appdomain. currentdomain. basedirectory = D: \ projects \ demo \ bin \ debug \5, System. appdomain. currentdomain. setupinformation. applicationbase = D: \ projects \ demo \ bin \ debug \6, System. Windows. Forms. application. startuppath = D: \ projects \ demo \ bin \ debug7, System. Windows. Forms. application. executablepath = D: \ projects \ demo \ bin \ debug \ demo. exe

System. environment. getenvironmentvariable ("WINDIR") =C: \ WINDOWSSystem. environment. getenvironmentvariable ("include") =C: \ Program Files \ Microsoft Visual Studio. NET 2005 \ SDK \ V2.0 \ include \System. environment. getenvironmentvariable ("tmp") =C: \ release E ~ 1 \ admini ~ 1 \ locals ~ 1 \ TempSystem. environment. getenvironmentvariable ("Temp") =C: \ release E ~ 1 \ admini ~ 1 \ locals ~ 1 \ TempSystem. environment. getenvironmentvariable ("path") =C: \ windows \ system32; C: \ WINDOWS; c: \ windows \ system32 \ WBEM; C: \ Program Files \ Microsoft SQL Server \ 90 \ tools \ binn \

 

4. In DLL, you sometimes need to use resources in the main calling program. In this case, you must correctly obtain the name and path of the calling program. This must be distinguished from the name and path of the DLL itself!

 

This involves the use of the system. reflection. Assembly class.

Getexecutingassembly: Gets the Assembly containing the Code currently executed

Getcallingassembly: Returns the system. reflection. Assembly that calls the method currently being executed.

From: http://blog.csdn.net/jzxcheng/article/details/31361263

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.