C # how to read the current project path

Source: Internet
Author: User

Previously, I was very depressed. I couldn't find a way to get the path of the current project (I could find it indirectly, but it was awkward). Today I accidentally saw a blog and reprinted it.

// Obtain the path or UNC location of the loaded file that contains the list.
Public static string sApplicationPath = Assembly. GetExecutingAssembly (). Location;

// Result: X: xxxxxxxxx. dll (directory where the. dll file is located +. dll file name)

// Obtain the complete path of the current process, including the file name (process name ).

String str = this. GetType (). Assembly. Location;

// Result: X: xxxxxxxxx.exe (.exefile directory. .exe file name)

// 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: xxxxxxxxx.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: xxxxxx (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: xxxxxx (the 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: xxxxxx (the 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: xxxxxx (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: xxxxxxxxx.exe (.exefile directory. .exe file name)

// Obtain the current working directory of the application (unreliable ).

String str = System. IO. Directory. GetCurrentDirectory ();

// Result: X: xxxxxx (directory of the .exe file)

The best way to retrieve paths in system services

String stmp = Assembly. GetExecutingAssembly (). Location;

Stmp = stmp. Substring (0, stmp. LastIndexOf (\); // delete the file name

If (pathType = 1)

Return stmp + @ "inputLog. xml ";

Else if (pathType = 2)

Return stmp + @ "MiddleDB. xml ";

Else

Return stmp + @ "AppNo. xml ";

Using System. IO;

String path = "d: asdfasdf.bmp ";

String fileName = Path. GetFileName (path); // file name

String ext = Path. GetExtension (path); // Extension

1. determine whether a given path is valid and valid
Use the Path. GetInvalidPathChars or Path. GetInvalidFileNameChars method to obtain invalid Path/file name characters. You can determine whether the Path contains invalid characters;

2. How to determine whether a path string represents a directory or a file
The Directory. Exists or File. Exist method is used. If the former is true, the path indicates the Directory. If the former is true, the path indicates the File.
The disadvantage of the above method is that it cannot process non-existing files or directories. In this case, you can use the Path. GetFileName method to obtain the file name. If a Path is not empty but the file name is empty, it indicates a directory; otherwise, it indicates a file;
3. Obtain a specific part of the path
Path. GetDirectoryName: returns the directory information of the specified Path string.
Path. GetExtension: returns the extension of the specified Path string.
Path. GetFileName: returns the name and extension of the specified Path string.
Path. GetFileNameWithoutExtension: returns the name of the Path string without the extension.
Path. GetPathRoot: Get the root directory information of the specified Path.
4. accurately merge two paths without worrying about the annoying "" Character
Using the Path. Combine method will help you deal with annoying "".
5. Obtain the path of the system directory
Environment. SystemDirectory attribute: Get the fully qualified path of the system directory
Environment. GetFolderPath method: the parameter type accepted by this method is Environment. SpecialFolder enumeration. Through this method, you can obtain a large number of system folder paths, such as my computer, desktop, and system directory.
Path. GetTempPath method: return the Path of the temporary folder of the current system
6. Determine whether a path is an absolute or relative path.
Use Path. IsPathRooted
7. Read or set the current directory
Use the GetCurrentDirectory and SetCurrentDirectory methods of the Directory class
8. Use relative path
After setting the current directory (see the previous question), you can use the relative path. For a relative Path, we can use Path. GetFullPath to obtain its fully qualified Path (absolute Path ).
Note: If you plan to use a relative path, we recommend that you set the working directory as the common starting point for each interaction file. Otherwise, some security risks that are difficult to detect may be introduced, attackers exploit this vulnerability to access system files.

9. Folder browsing dialog box (FolderBrowserDialog class)
Main attribute: Description: The Description text displayed on the Tree View control, for example, "Select Directory-exercise"; RootFolder: gets or sets the root folder from which to browse, for example, set in my computer (desktop by default); SelectedPath: gets or sets the path selected by the user. If this attribute is set, the specified path is located when the dialog box is opened, the default value is the root folder. When the dialog box is closed, the path selected by the user is obtained based on this attribute. ShowNewFolderButton: Get or set whether to display the new dialog box button;
Main method: ShowDialog: In this dialog box, the returned value is DialogResult type. If DialogResult. OK is returned, the user-selected path can be obtained through the SelectedPath attribute;
========================================================== ==================================

1. System. Diagnostics. Process. GetCurrentProcess (). MainModule. FileName-obtain the complete path of the module.

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

3. System. IO. Directory. GetCurrentDirectory ()-Get the current working Directory of the application. This is not necessarily the directory from which the Program starts. It may be stored in C: xxx. This function may return C: statements and SettingsYB, or C: Program FilesAdobe, sometimes it is not necessarily returned.

4. System. AppDomain. CurrentDomain. BaseDirectory-get the base Directory of the program.

5. System. AppDomain. CurrentDomain. SetupInformation. ApplicationBase-obtain and set the Directory Name of the application.

6. System. Windows. Forms. Application. StartupPath-obtain the executable file path of the Application. The effect is the same as that of 2 and 5. Only five strings are followed by one more "".

7. System. Windows. Forms. Application. ExecutablePath-obtain the path and file name of the executable file that started the Application.

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.