Go Path-related operations in C #

Source: Internet
Author: User

Using System.IO

1, determine whether a given path is valid, legal
An illegal path/file name character is obtained by Path.getinvalidpathchars or Path.getinvalidfilenamechars method, which can be used to determine if the path contains illegal characters;

2. How to determine whether a path string represents a directory or a file
Using the Directory.Exists or File.exist method, if the former is true, the path represents the directory, and if the latter is true, the path represents the file
One drawback of the above approach is that you cannot handle files or directories that do not exist. At this point you can consider using the Path.getfilename method to obtain the file name it contains, if a path is not empty, and the file name is empty then it represents the directory, otherwise represents the file;
3. Get a specific part of the path
Path.getdirectoryname: Returns directory information for the specified path string.
Path.getextension: Returns the extension of the specified path string.
Path.getfilename: Returns the file name and extension of the specified path string.
Path.getfilenamewithoutextension: Returns the file name of a path string that does not have an extension.
Path.getpathroot: Gets the root directory information for the specified path.
4. Merge two paths accurately without worrying about the annoying "\" character
Using the Path.Combine method, it will help you deal with annoying "\".
5. Get the path to the system directory
Environment.systemdirectory property: Gets the fully qualified path to the system directory
Environment.getfolderpath method: The method accepts the parameter type is Environment.SpecialFolder enumeration, through this method can obtain a large number of system folder path, such as My computer, desktop, system directory, etc.
Path.gettemppath method: Returns the path of the temporary folder for the current system
6, determine whether a path is an absolute path or a relative path
Using the Path.ispathrooted method
7. Read or set the current directory
Using the GetCurrentDirectory and SetCurrentDirectory methods of the Directory class
8. Using relative paths
After setting the current directory (see the previous question), you can use a relative path. For a relative path, we can use the Path.GetFullPath method to get its fully qualified path (absolute path).
Note: If you intend to use relative paths, it is recommended that you set the working directory as a common starting point for each interactive file, or you may introduce some hidden security vulnerabilities that can be exploited by malicious users to access system files.

9. Folder browse dialog box (FolderBrowserDialog Class)
Main properties: Description: The descriptive text displayed on the TreeView control, such as "Select Directory-Practice" in, RootFolder: Gets or sets the root folder from which to start browsing, as set in the previous image of my Computer (desktop by default)         ; SelectedPath: Gets or sets the path selected by the user, if this property is set, the dialog box is opened to the specified path, the default is the root folder, and when the dialog box is closed, the path selected by the user user is obtained; Shownewfolderbutton: Gets or sets whether the New dialog box button is displayed;
Main method: ShowDialog: Opens the dialog box, the return value is DialogResult type value, if DialogResult.OK, you can get the path selected by the user by the SelectedPath property;

Go Path-related operations in C #

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.