How to obtain the current path in C #

Source: Internet
Author: User

How to obtain the current path in C #

String apppath = System. Reflection. Assembly. GetExecutingAssembly (). GetName (). CodeBase; // obtain the path name of the entire file
Apppath = apppath. Substring (0, apppath. LastIndexOf (@ "\"); // remove the program name after/
MessageBox. Show (apppath );


1. System. Diagnostics. Process. GetCurrentProcess (). MainModule. FileName
Obtain the complete path of the module. Obtain the name of the currently executed exe file.

2. System. Environment. CurrentDirectory
Obtain and set the fully qualified directory of the current directory from which the process starts.

Note:
As defined, if the process is started locally or in the root directory of the network drive, the value of this attribute is the drive name followed by a backslash (such as "C :\"). If the process is enabled in a subdirectory, the value of this attribute is a drive and subdirectory path (such as "C: \ mySubDirectory") without a backslash ").
If another program calls a program that contains the method, the value obtained by this method is the path of another program.

3. System. IO. Directory. GetCurrentDirectory ()
Obtain the current working directory of the application. This is not necessarily the directory from which the program starts. It may be stored in C: \ www. This function may return C: \ Documents ents and Settings \ ZYB \, or C: \ Program Files \ Adobe \. Sometimes I don't know what to return.

Note: The value of this method is not fixed and changes with the directories determined by objects such as OpenFileDialog and SaveFileDialog.

2 and 3 have the same effect.
4. System. AppDomain. CurrentDomain. BaseDirectory
Obtain the base Directory of the program. Gets the base Directory, which is used by the Assembly conflict resolution program to detect the assembly.

5. System. AppDomain. CurrentDomain. SetupInformation. ApplicationBase
Obtain and set the name of the directory containing the application.

6. System. Windows. Forms. Application. StartupPath
Obtain the path of the executable file that started the application, excluding the name of the executable file. The effect is the same as that of 4 and 5. Only 5 returns an extra "\" behind the string.

7. System. Windows. Forms. Application. ExecutablePath
Obtain the path of the executable file that started the application, including the name of the executable file. Obtain the path and file name of the executable file that started the application. The effect is the same as that of 1.

Sample Code:
// Obtain the complete path of the new Process component and associate it with the active Process, including the file name (Process name ).
String str = System. Diagnostics. Process. GetCurrentProcess (). MainModule. FileName;
Result: X: \ xxx \ xxx.exe (.exefile directory. .exe file name)

// Obtain and set the full path of the current directory (that is, the directory from which the process starts.
String str = System. Environment. CurrentDirectory;
Result: X: \ xxx (directory of the .exe file)

// Obtain the base Directory of the current application domain of the current Thread, which is used by the Assembly conflict resolution program to detect the assembly.
String str = System. AppDomain. CurrentDomain. BaseDirectory;
Result: X: \ xxx \ (directory where the .exe file is located + "\")

// Obtain and set the name of the directory containing the application.
String str = System. AppDomain. CurrentDomain. SetupInformation. ApplicationBase;
Result: X: \ xxx \ (directory where the .exe file is located + "\")

// Obtain the path of the executable file that started the application, excluding the name of the executable file.
String str = System. Windows. Forms. Application. StartupPath;
Result: X: \ xxx (directory of the .exe file)

// Obtain the path of the executable file that started the application, including the name of the executable file.
String str = System. Windows. Forms. Application. ExecutablePath;
Result: X: \ xxx \ xxx.exe (.exefile directory. .exe file name)

// Obtain the current working directory of the application (unreliable ).
String str = System. IO. Directory. GetCurrentDirectory ();
Result: X: \ xxx (directory of the .exe file)

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.