Asp tutorial. net c # FAQ path, file, directory, io summary
Main content:
1. Path-related operations, such as determining whether the path is valid, path type, specific part of the path, merged path, and system folder path;
2. Related general file dialog box, which can help us operate the files and directories in the file system;
3. Operations on files, directories, and drives, such as obtaining basic information about them, obtaining and setting properties of files and directories, and file version information,
Search for files and directories, file determination, etc., copy, move, delete, rename files and directories;
4. Read and write files, including temporary files and random file names;
5. File system monitoring;
Write down the first two parts.
1. Path-related operations
Question 1: How to determine whether a given path is valid/valid;
Solution: Use path. getinvalidpathchars or path. getinvalidfilenamechars to obtain invalid path/file name characters.
Determine whether the path contains invalid characters;
Question 2: How to determine whether a path string represents a directory or a file;
Solution:
1. Use directory. exists or file. exist. If the former is true, the path indicates the directory. If the former is true, the path indicates the file;
2. The disadvantage of the above method is that it cannot process non-existing files or directories. You can use the path. getfilename method to obtain
It contains the file name. If a path is not empty but the file name is empty, it indicates the directory; otherwise, it indicates the file;
Question 3: how to obtain a specific part of a path (such as a file name or extension );
Solution:
The following are some related methods:
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: obtains the root directory information of the specified path;
(For more information, see msdn)
Question 4: How to accurately merge two paths without worrying about the annoying "" character;
Solution:
Using the path. combine method, it will help you deal with annoying "";
Question 5: how to obtain the path of the system directory (such as desktop, my documents, and temporary folders );
Solution:
The following describes the attributes and methods of the system. environment class:
Environment. systemdirectory attributes: obtain the fully qualified path of the system directory;
Environment. getfolderpath method: the parameter type accepted by this method is environment. specialfolder enumeration,
This method can be used to obtain a large number of system folders, such as my computer, my computer, desktop, and system directory;
(For more information, see msdn );
Path. gettemppath method: return the path of the temporary folder of the current system;