First, get the path of the current file
string str1=process.getcurrentprocess (). Mainmodule.filename;//can get the file name of the EXE currently executing.
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
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. The effect is the same 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