It is very common to operate files in programs, and operations on files inevitably require the path of the file and perform a series of operations on the path of the file, such as: determining whether a known path is a directory or a file, Lu Jin is a file, what is the name of the file, what is the extension name of the file, etc. In C#, the file path is not abstracted into a class, which means that the file path is an ordinary string. Operation on file paths, for example, to obtain a file name, you can obtain it by intercepting a string or using a regular expression.
In fact, in the .NET class library, there is a special function class that operates on strings representing files or in directory paths. The following is a few common operations that use Path to perform on file paths.
Determine whether the given path or file name is legal
() This method returns a char[] character that indicates that it cannot appear in the file name.
/// <summary> /// Determine whether the file name is legal /// </summary> /// <param name="fileName">File name</param> /// <returns>ture, legal; false, illegal</returns> public bool validFileName(string fileName) { if(!(fileName)) { char [] errChars = () ; foreach(char ch in errChars) { if((())) return false ; } } else { return false ; } return true ; }
If the file name is illegal, you can use the following method to remove the illegal characters.
/// <summary> /// Remove illegal characters from file names /// </summary> /// <param name="fileName"></param> /// <returns>Legal file name</returns> public string fixedFileName(string fileName) { char[] errChars = (); StringBuilder fileNameBuilder = new StringBuilder(fileName) ; foreach(char ch in errChars) { ((),); } return (); }
Similarly, use () to obtain characters that cannot appear in the directory path to determine whether the given directory path is legal and modify the illegal directory path.
Determine whether the given path is a directory path or a file path
If it is possible to determine that the given path already exists, then you can use () and () to determine whether the path is a directory or a file. It is not certain that the given path exists. You can use () to get the file name in the path. If you can get the file name, the path is obviously a file path. If you cannot, it is a path. Explanation: The last character of the directory path should end with '\'.
Merge paths
Some different parts of the path are taken from different places, which requires combining these parts to form a complete path. The processing of "\" is inevitably involved in the combination process. At this time, you can use () to combine different parts of the path together. () There are many forms of overloading to meet different needs. It should be noted that when using () does not accept illegal file names or path names, and when merging, if a certain part starts from an absolute path, the merge operation is reset to start from the absolute path, discarding the previous merged path. For example:
string[] paths = {@"d:\archives", "2001", "e:\\", "images"}; string fullPath = (paths); //fullPath is e:\images (fullPath); fullPathfore:\images
Get specific parts of the path, such as: file name, extension, file directory, etc.
(path);//Get file name (path); //Get file name without extension (path) ; //Get file extension (path) ; //Get file directory (path) ;//Get root directory information
For more specific purposes, please refer to MSDN/zh-cn/library/.path_methods.aspx
Path some other function functions
- () Get a random file name or directory name
- () Create a temporary file with a uniquely named zero byte on disk and return the full path to the file
- () Get the temporary directory path of the current user
- () Determines whether the path contains a file extension
Get the paths related to the application
- (). Get the full path to the module, including the file name.
- Get or set the fully qualified path to the application's current working directory
- () Get or set the application's current working directory, not necessarily the application's startup directory, but the directory that the application has operated for the last time.
Environment variable correlation
Use() to get file paths related to environment variables, for example:
- ("windir") Get the directory where the operating system is located
- ("INCLUDE") Get the directory where the header file is located
- ("TMP") Get temporary directory
- ("Path") Gets the file directory contained in the Path environment variable
Naturally, use() to set environment variables