In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the operation of the path in C#, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
1. Determine whether a given C # path is valid and legal.
Illegal C # path / file name characters can be obtained by Path.GetInvalidPathChars or Path.GetInvalidFileNameChars method, which can be used to determine whether the path contains illegal characters.
2. How to determine whether a C # 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; if the latter is true, the path represents the file
The drawback of the above method is that it cannot deal with files or directories that do not exist. At this point, you can consider using the Path.GetFileName method to get the file name it contains. If a path is not empty and the file name is empty, it represents a directory, otherwise it represents a file.
3. Get a specific part of the C# 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 of the specified path.
4. Merge the 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 of the system directory
Environment.SystemDirectory attribute: gets the fully qualified path to the system directory
Environment.GetFolderPath method: the parameter type accepted by this method is Environment.SpecialFolder enumeration. Through this method, you can get the paths of a large number of system folders, such as my computer, desktop, system directory, etc.
Path.GetTempPath method: returns the path to the temporary folder of the current system
6. Determine whether a C# path is absolute or relative.
Use the Path.IsPathRooted method
7. Read or set the current directory
Use the GetCurrentDirectory and SetCurrentDirectory methods of the Directory class
8. Use relative paths
After setting the current directory (see the previous question), you can use the relative path. For a relative path, we can use the Path.GetFullPath method to get its fully qualified path (absolute path).
Note: if you plan to use relative paths, it is recommended that you set the working directory as the common starting point for various interactive files, otherwise some security risks that are not easy to find may be introduced and used by malicious users to access system files.
9. Folder browsing dialog box (FolderBrowserDialog class)
Main properties: Description: the description text displayed on the tree view control, such as "Select Catalog-exercise" in the image above; RootFolder: get or set the root folder from which you start browsing, such as my computer (desktop by default) set up in the image above SelectedPath: gets or sets the path selected by the user. If this property is set, the specified path is located when the dialog box is opened. The default is the root folder. When the dialog box is closed, the path selected by the user is obtained according to this attribute. ShowNewFolderButton: gets or sets whether the new dialog box button is displayed.
Main method: ShowDialog: open the dialog box and return the value of type DialogResult. If it is DialogResult.OK, the path selected by the user can be obtained by the SelectedPath attribute.
Thank you for reading this article carefully. I hope the article "how to operate the path in C#" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.