Summarize 7 ways to get the current path in C # (reprint)

Source: Internet
Author: User

Summarize the 7 ways in which C # gets the current path

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

1 string path = "";

2

3 If (System.Environment.CurrentDirectory = = AppDomain.CurrentDomain.BaseDirectory)//windows application is equal

4

5 ... {

6

7 Path = AppDomain.CurrentDomain.BaseDirectory;

8

9}

10

One else

12

13 ... {

14

Path = AppDomain.CurrentDomain.BaseDirectory + "bin\";

16

17}

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 # How 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, it is possible that the program is placed in the C:\www, this function may return C:\Documents andsettings\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 variable value, in advance, I wrote a WinForm program, project files stored in D:\Visual studioprojects\myapplication\lifeassistant, the compiled file is located in D : \visual Studioprojects\myapplication\lifeassistant\bin\debug, the final result is as follows:
1, System.Diagnostics.Process.GetCurrentProcess (). Mainmodule.filename=d:\visualstudio Projects\myapplication\lifeassistant\bin\debug\lifeassistant.exe
2, System.environment.currentdirectory=d:\visual Studioprojects\myapplication\lifeassistant\bin\debug
3, System.IO.Directory.GetCurrentDirectory () =d:\visualstudio Projects\myapplication\lifeassistant\bin\debug
1 ASP. WebFormUse "Request.physicalapplicationpath to get the physical path of the virtual directory where the site resides, and finally include" \ ";
2.C # WinFormUse
A: "Application.startuppath": Gets the path of the directory where the current application resides, and finally does not contain "\";
B: "Application.executablepath": Gets the path of the current application file, including the name of the file;
C: "AppDomain.CurrentDomain.BaseDirectory": Gets the path of 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 include "\" at the end;
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 of "system32" directory;
If you want to use "Application.startuppath" or "Application.executablepath", you need to manually add a reference to "System.Windows.Forms.dll", and at the beginning of the program use " UsingSystem.Windows.Forms "declares the reference;
4. In the uninstall program get the directory of the system installation:
System.Reflection.AssemblycurPath = System.Reflection.Assembly.GetExecutingAssembly ();
String path=curpath.location;//get the path to the Installer class Setuplibrary file, get the directory where the file path is located, and get the directory of the installer;

4, System.appdomain.currentdomain.basedirectory=d:\visualstudio projects\myapplication\lifeassistant\bin\debug\
5, System.appdomain.currentdomain.setupinformation.applicationbase=d:\visualstudio Projects\MyApplication\ Lifeassistant\bin\debug\
6, System.windows.forms.application.startuppath=d:\visualstudio Projects\myapplication\lifeassistant\bin\debug
7, System.windows.forms.application.executablepath=d:\visualstudio projects\myapplication\lifeassistant\bin\ Debug\lifeassistant.exe
System.Environment.GetEnvironmentVariable ("windir") =c:\windows
System.Environment.GetEnvironmentVariable ("INCLUDE") =c:\programfiles\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:\ProgramFiles\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";

 

 

 

In C #, the relative path is "." and ".." Indicates that the
"." 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 in the: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. But one thing must be noted: (only in the same drive (e.g., all in D:) relative paths are used).

7 Ways to summarize C # to get the current path (reprinted)

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.