There are many methods to obtain a path in c #. The following are commonly used:
For example, the current executable path is: E:/temp/Pocket PC CopyFile/bin/Debug/Pocket PC CopyFile. EXE.
1. // obtain the current working directory of the application.
String path1 = System. IO. Directory. GetCurrentDirectory ();
MessageBox. Show ("Get the current working directory of the application:" + path1 );
// Return value: E: \ temp \ Pocket PC CopyFile \ bin \ Debug
2. // obtain the base Directory of the program.
String path2 = System. AppDomain. CurrentDomain. BaseDirectory;
MessageBox. Show ("Get the program's base Directory:" + path2 );
// Return value: E: \ temp \ Pocket PC CopyFile \ bin \ Debug \
3. // obtain and set the Directory Name of the application.
String path3 = System. AppDomain. CurrentDomain. SetupInformation. ApplicationBase;
MessageBox. Show ("Get and set the name of the directory containing the application:" + path3 );
// Return value: E: \ temp \ Pocket PC CopyFile \ bin \ Debug \
4. // obtain the path of the executable file that has started the application, excluding the name of the executable file.
String path4 = System. Windows. Forms. Application. StartupPath;
MessageBox. Show ("Get the path of the executable file that started the application, excluding the executable file name:" + path4 );
// Return value: E: \ temp \ Pocket PC CopyFile \ bin \ Debug
5. // obtain the executable file path and file name of the application.
String path5 = System. Windows. Forms. Application. ExecutablePath;
MessageBox. Show ("Get the path and file name of the executable file that started the application:" + path5 );
// Return value: E: \ temp \ Pocket PC CopyFile \ bin \ Debug \ Pocket PC CopyFile. EXE
6. // obtain or set the program URL
String path6 = System. Reflection. Assembly. GetExecutingAssembly (). GetName (). CodeBase;
MessageBox. Show ("Get or set the program URL:" + path6 );
// Return value: file: // E:/temp/Pocket PC CopyFile/bin/Debug/Pocket PC CopyFile. EXE
From # Define