C # Gets the current path in the following way:
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, I don't understand it either.
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 to the executable file that launched the application. The effect is the same as 2, 5. Just 5 of the string returned is a "\" more.
7. 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.
For Windows programs and Web applications, the path they run is different, so the key is to determine which program is currently running. So we can use the following code
- string Path = "";
- ?
- if (System.Environment.CurrentDirectory = = AppDomain.CurrentDomain.BaseDirectory)//windows The application is equal
- ?
- ... {
- ?
- Path = AppDomain.CurrentDomain.BaseDirectory;
- ?
- }
- ?
- Else
- ?
- ... {
- ?
- Path = AppDomain.CurrentDomain.BaseDirectory + "bin\";
- ?
- }
So if we write a class library, the class library uses the Assembly.LoadFrom, because it is a universal class library, it may be used in Windows programs may also be used in the Web, then the above code is very convenient.
1, Server.MapPath
2, System.Windows.Forms.StartupPath
3, Type.Assembly.Location
C # Gets the current path method 2 can be applied to the console application, the WinForm application, the Windows service, Method 1 can be applied to the Web application, and Method 3 is available.
But Method 3 is the path to load the application. In the case of a Web application, the path obtained is: C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary the ASP. Net Files directory. So the Web project still uses Server.MapPath. Otherwise, use Method 2 is recommended. If you create a new class library yourself. Can be used after a reference to System.Windows.Forms.StartupPath is added.
C # Get the current path method to summarize here, I hope to help you.
?
?
C # method to get the current path of the program
Gets the full path of the new process component and its main module associated with the currently active process, including the file name (process name).
String str = System.Diagnostics.Process.GetCurrentProcess (). Mainmodule.filename;
Result:x:\xxx\xxx\xxx.exe (. exe file in the same directory as +.exe filename)
Gets and sets the fully qualified path to the current directory (that is, the directory from which the process starts).
string str = System.Environment.CurrentDirectory;
Result:x:\xxx\xxx (the directory where the. exe file resides)
Gets the base directory of the current application domain for Thread, which is used by the Assembly resolver to probe assemblies.
string str = System.AppDomain.CurrentDomain.BaseDirectory;
Result:x:\xxx\xxx\ (the directory where the. exe file is located + "\")
Gets and sets the name of the directory that contains the application.
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
Result:x:\xxx\xxx\ (the directory where the. exe file is located + "\")
Gets the path to the executable file that launched the application, not including the name of the executable file.
string str = System.Windows.Forms.Application.StartupPath;
Result:x:\xxx\xxx (the directory where the. exe file resides)
Gets the path to the executable file that launched the application, including the name of the executable file.
string str = System.Windows.Forms.Application.ExecutablePath;
Result:x:\xxx\xxx\xxx.exe (. exe file in the same directory as +.exe filename)
Gets the current working directory of the application (unreliable).
String str = System.IO.Directory.GetCurrentDirectory ();
Result:x:\xxx\xxx (the directory where the. exe file resides)
?
?
C # Get relative path
First, get the path of 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, 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
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 to the executable file that launched the application. The effect is the same as 2, 5. Just 5 of the string returned is a "\" more.
7. 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.
Second, the operating environment variables
The System.Environment.GetEnvironmentVariable () method makes it easy to obtain system environment variables, such as:
System.Environment.GetEnvironmentVariable ("windir") Gets 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");
System.Environment.SystemDirectory; C:/windows/system32 Directory
Finally posted I do the above operation obtained the value of the variable, in advance, I wrote a WinForm program, the project file is stored in D:\Visual Studio projects\myapplication\lifeassistant, the compiled file is located in D : \visual Studio Projects\myapplication\lifeassistant\bin\debug, the final result is as follows:
1, System.Diagnostics.Process.GetCurrentProcess (). Mainmodule.filename=d:\visual Studio Projects\myapplication\lifeassistant\bin\debug\lifeassistant.exe
2. system.environment.currentdirectory=d:\visual Studio projects\myapplication\lifeassistant\bin\debug
3, System.IO.Directory.GetCurrentDirectory () =d:\visual Studio Projects\myapplication\lifeassistant\bin\debug
1 ASP. WebFormwith " Request.physicalapplicationpath Gets the physical path of the virtual directory where the site resides, and finally contains the "\" ;
2.C # WinFormwith
A: "Application.startuppath": Gets the path to the directory where the current application resides, and finally does not contain" \ ";
B: "Application.executablepath": Gets the path to the current application file, including the name of the file;
C: "AppDomain.CurrentDomain.BaseDirectory ": Gets the path to the directory where the current application resides, and finally contains" \ ";
D: "System.Threading.Thread.GetDomain (). BaseDirectory ": Gets the path to the directory where the current application resides, and finally contains" \ ";
E: "environment.currentdirectory": Gets the path of the current application, and finally does not contain" \ ";
F: " System.IO.Directory.GetCurrentDirectory": Gets the path of the current application and does not contain the" \ " ;
3.C # Windows service
with " AppDomain.CurrentDomain.BaseDirectory "
or " System.Threading.Thread.GetDomain (). BaseDirectory";
With "environment.currentdirectory" and
"System.IO.Directory.GetCurrentDirectory"will get the path to the" system32 "directory;
If you want to use theApplication.startuppath"or"Application.executablepath", you need to manually add a reference to" System.Windows.Forms.dll "and declare the reference with" using System.Windows.Forms "at the beginning of the program;
4. In the uninstall program get the directory of the system installation:
System.Reflection.Assembly Curpath = System.Reflection.Assembly.GetExecutingAssembly ();
string path=curpath.location;// gets The path to the Installer class Setuplibrary file. Gets the directory where the file path is located, which is the directory where the installer is installed ;
4. System.appdomain.currentdomain.basedirectory=d:\visual Studio projects\myapplication\lifeassistant\bin\ Debug\
5. System.appdomain.currentdomain.setupinformation.applicationbase=d:\visual Studio projects\myapplication\ Lifeassistant\bin\debug\
6. System.windows.forms.application.startuppath=d:\visual Studio projects\myapplication\lifeassistant\bin\ Debug
7. System.windows.forms.application.executablepath=d:\visual Studio projects\myapplication\lifeassistant\bin\ Debug\lifeassistant.exe
System.Environment.GetEnvironmentVariable ("windir") =c:\windows
System.Environment.GetEnvironmentVariable ("INCLUDE") =c:\program Files\Microsoft Visual Studio. NET 2003\sdk\ V1.1\include\
System.Environment.GetEnvironmentVariable ("TMP") =c:\docume~1\zhoufoxcn\locals~1\temp
System.Environment.GetEnvironmentVariable ("TEMP") =c:\docume~1\zhoufoxcn\locals~1\temp
System.Environment.GetEnvironmentVariable ("Path") =c:\windows\system32; C:\WINDOWS; C:\WINDOWS\System32\Wbem; C:\jdk1.5.0\bin; C:\MySQLServer5.0\bin; C:\Program files\symantec\pcanywhere\; C:\Program Files\Microsoft SQL Server\80\Tools\Binn
C # relative path system path
2007-12-22 09:53
Gets the path to the executable file that launched the application, not including the name of the executable file.
String Str5=application.startuppath;
Gets the file name of the EXE that is currently executing.
String str1 =process.getcurrentprocess (). Mainmodule.filename;
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 (such as "C:\mySubDirectory") without trailing backslashes.
String str2=environment.currentdirectory;
Gets the current working directory of the application.
String Str3=directory.getcurrentdirectory ();
Gets the base directory that is used by the Assembly resolver to probe assemblies.
String str4=appdomain.currentdomain.basedirectory;
Gets the path to the executable file that launched the application, not including the name of the executable file.
String Str5=application.startuppath;
Gets the path to the executable file that launched the application, including the name of the executable file.
String Str6=application.executablepath;
Gets or sets the name of the directory that contains the application.
String str7=appdomain.currentdomain.setupinformation.applicationbase;
Example
Application.startuppath;
Can get F:\learning\c#training\win\win\bin\debug
Pay attention to two of your own
application.startuppath+ "\\3.jpg";
? ? ? "." Represents the current directory, "..." On behalf of the previous level. For example, suppose I used vs2005 to create a project named controls in the D:\My documents\visual Studio 2005\projects directory, where there is a controls folder in the Projects folder, The controls folder has three files: Controls.sln Controls folder Gulfofstlawrence folder. D:\My Documents\Visual Studio 2005\projects\controls\controls\bin\debug is the executable file that this simple project can run Controls.exe Now I want to D:\My gulf_of_st._lawrence.mxd under the Documents\Visual Studio 2005\projects\controls\gulfofstlawrence folder (ArcGIS Desktop) project file path. then the relative path should be ". \.. \.. \gulfofstlawrence\gulf_of_st._lawrence.mxd " is string filename = @". \.. \.. \gulfofstlawrence\gulf_of_st._lawrence.mxd "; Experience: 1. Using a relative path can increase the portability of the project. Make a project simple during the transplant, saving a lot of time to lay out the files associated with the project. (If the absolute path is set). 2. Using relative paths also makes program code simple 3. One thing to note, however, is that you can use relative paths only in the same drive (for example: all in D:). |
Summarize the 7 ways in which C # gets the current path