C # Get the current application's path and environment variables

Source: Internet
Author: User

First, get the path of the current file

string str1=process.getcurrentprocess (). Mainmodule.filename ;//Gets the file name of the EXE that is currently executing. &NBSP
string str2=environment.currentdirectory ;//Gets and sets the fully qualified path to the current directory (that is, the directory from which the process starts). (Note: By definition, if the process is started in the root directory of a local or network drive, the value of this property is the drive name followed by a trailing backslash (such as "C: \"). If the process is started in a subdirectory, the value of this property is the drive and subdirectory path without trailing backslashes [such as "C:\mySubDirectory"]).  
string Str3=directory.getcurrentdirectory ();   //Gets the current working directory of the application.  
string str4=appdomain.currentdomain.basedirectory ;//Gets the base directory, which is used by the Assembly resolver to probe assemblies.  
string Str5=application.startuppath ;// Gets the path to the executable file that launched the application, not including the name of the executable file.  
string Str6=application.executablepath ;// Gets the path to the executable file that launched the application, including the name of the executable file.  
string Str7=appdomain.currentdomain.setupinformation.applicationbase ;// Gets or sets the name of the directory that contains the application.

1. System.Diagnostics.Process.GetCurrentProcess (). MAINMODULE.FILENAME&NBSP
   Gets the full path of the module.
2. system.environment.currentdirectory 
   gets and sets the fully qualified directory of the current directory (the directory from which the process starts).
3. System.IO.Directory.GetCurrentDirectory ()  
   Gets the current working directory of the application. This is not necessarily the directory from which the program starts, ah, it is possible that the program is placed in C:\www, this function may return C:\Documents and settings\zyb\, or C:\Program files\adobe\, sometimes not necessarily return something, This is the last directory that any application has ever operated on, such as when you open the E:\doc\my.doc file with Word, and you return to E:\doc when you execute this method.
4. System.AppDomain.CurrentDomain.BaseDirectory
   get the base directory for the program.
5. system.windows.forms.application.startuppath 
   Gets the path to the executable file that launched the application. The effect is the same as 2, 5. Only 5 of the string returned is followed by a "\".
6. system.windows.forms.application.executablepath 
   Gets the path and file name of the executable file that launched the application, with the same effect as 1.
7. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
   Gets and sets the name of the directory that contains the application.

Second, the operating environment variables

The System.Environment.GetEnvironmentVariable () method makes it easy to obtain system environment variables, such as: System.Environment.GetEnvironmentVariable (" Windir ") to obtain the path to the Windows system directory.
Here are some common environment variable values:
System.Environment.GetEnvironmentVariable ("windir");
System.Environment.GetEnvironmentVariable ("INCLUDE");
System.Environment.GetEnvironmentVariable ("TMP");
System.Environment.GetEnvironmentVariable ("TEMP");
System.Environment.GetEnvironmentVariable ("Path");

Third, application examples

Wrote a WinForm program, the project file is stored in D:\Projects\Demo, the compiled file is located 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.exe
2 , System.environment.currentdirectory=d:\projects\demo\bin\debug
3 , System.IO.Directory.GetCurrentDirectory () =d:\projects\demo\bin\debug
4 , 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\debug
7 , System.windows.forms.application.executablepath=d:\projects\demo\bin\debug\demo.exe

System.Environment.GetEnvironmentVariable ("windir") = C:\WINDOWS
System.Environment.GetEnvironmentVariable ("INCLUDE") = C:\Program Files\Microsoft Visual Studio.NET 2005\sdk\v2.0\include\
System.Environment.GetEnvironmentVariable ("TMP") = C:\docume~1\admini~1\locals~1\temp
System.Environment.GetEnvironmentVariable ("TEMP") = C:\docume~1\admini~1\locals~1\temp
System.Environment.GetEnvironmentVariable ("Path") = C:\WINDOWS\system32; C:\WINDOWS; C:\WINDOWS\System32\Wbem; C:\Program Files\Microsoft SQL Server\90\tools\binn\

C # Get the current application's path and environment variables

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.