First, get the path to the current file
1. System.Diagnostics.Process.GetCurrentProcess (). Mainmodule.filename
Gets the full path of the module, including the file name.
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, it is possible that the program is placed in the c:www, this function may return c:documents and Settingszyb, or C:Program filesadobe, sometimes not necessarily return something, This is the last operation of any application directory, such as you use Word to open the E:docmy.doc this file, this time to execute this method returned to the E:doc.
4. System.AppDomain.CurrentDomain.BaseDirectory
Gets the base directory of the program.
5. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
Gets and sets the name of the directory that contains the application.
6. System.Windows.Forms.Application.StartupPath
Gets the path of the executable file that started the application. The effect is the same as 2, 5. Only 5 returned a string with an extra "".
7. System.Windows.Forms.Application.ExecutablePath
Gets the path and file name of the executable file that started the application, with the same effect as 1.
II. Operating Environment variables
The System.Environment.GetEnvironmentVariable () method makes it easy to obtain system environment variables, such as:
System.Environment.GetEnvironmentVariable ("windir") allows you to obtain a path to the Windows system directory.
The following are some commonly used environment variable values:
System.Environment.GetEnvironmentVariable ("windir");
System.Environment.GetEnvironmentVariable ("INCLUDE");
System.Environment.GetEnvironmentVariable ("TMP");
System.Environment.GetEnvironmentVariable ("TEMP");
System.Environment.GetEnvironmentVariable ("Path");
Finally posted the value of the variables I obtained in the above operation, in advance, I wrote a WinForm program, the project file is stored in d:visual Studio projectsmyapplicationlifeassistant, the compiled file is located in D: Visual Studio Projectsmyapplicationlifeassistantbindebug, the final results are as follows:
1, System.Diagnostics.Process.GetCurrentProcess (). Mainmodule.filename=d:visual Studio ProjectsMyApplicationLifeAssistantbinDebugLifeAssistant.exe
2, System.environment.currentdirectory=d:visual Studio projectsmyapplicationlifeassistantbindebug
3, System.IO.Directory.GetCurrentDirectory () =d:visual Studio Projectsmyapplicationlifeassistantbindebug
4, System.appdomain.currentdomain.basedirectory=d:visual Studio projectsmyapplicationlifeassistantbindebug
5. System.appdomain.currentdomain.setupinformation.applicationbase=d:visual Studio Projectsmyapplicationlifeassistantbindebug
6, System.windows.forms.application.startuppath=d:visual Studio projectsmyapplicationlifeassistantbindebug
7. System.windows.forms.application.executablepath=d:visual Studio ProjectsMyApplicationLifeAssistantbinDebugLifeAssistant.exe
System.Environment.GetEnvironmentVariable ("windir") =c:windows
System.Environment.GetEnvironmentVariable ("INCLUDE") =c:program filesmicrosoft Visual Studio. NET 2003sdkv1.1include
System.Environment.GetEnvironmentVariable ("TMP") =c:docume~1zhoufoxcnlocals~1temp
System.Environment.GetEnvironmentVariable ("TEMP") =c:docume~1zhoufoxcnlocals~1temp
System.Environment.GetEnvironmentVariable ("Path") =c:windowssystem32; C:windows; C:windowssystem32wbem; C:jdk1.5.0bin; C:mysqlserver5.0bin; C:Program Filessymantecpcanywhere; C:Program FilesMicrosoft SQL Server80toolsbinn
This article is from "Zhou Gong's column" blog, please be sure to keep this source http://zhoufoxcn.blog.51cto.com/792419/167215