To manipulate a file/folder, you need the full path of a file
I. File operation using static method of file
Use the static method of File to replicate file.copy (path, destpath); Use the static method of file to delete a file file.delete (path) under the path; Use the static method of file to move a file under Path file.move (path, destpath); File.readalltext (path); Open a text file *.txt, read the data in the file, and then close the file //write File.writealltext (path, "string to write to the file");//Create a file, write data to it, If there is a file with the same name under this path,
PS: Write to the file, if the path has the same name file will overwrite, so it is best to make a judgment, with the user to interact with the overwrite
Second, the instantiation of the FileInfo operation
FileInfo myfile = new FileInfo (path); Declares an object to operate on a file myfile. CopyTo (destpath); Copy the file and copy the path to DestPath myfile. MoveTo (destpath); MyFile the move Operation . Delete (); To delete an operation
Get details about a file or folder (creation date, last modification date, and so on)
Gets the details of a file, or folder. (creation date, file name, etc.) FileInfo myfile = new FileInfo (path);//Declare an object to operate on a file DateTime dt = myfile. CreationTime; Gets or sets the creation date of the File/folder string filepath = myfile. DirectoryName; Can only be used for FileInfo, get the full pathname, path + file name bool file = myfile. Exists; The value of this property indicates whether the file or folder exists, and the existence returns True string fullname = MyFile. FullName; Gets the full path name of the file or folder DateTime lasttime = myfile. LastAccessTime; Gets or sets the last time the file or folder was accessed by datetime lastwrite = myfile. LastWriteTime; Gets or sets the last time the folder or folder was modified by string name = MyFile. Name; Get file name, cannot modify oh long = myfile. Length; The byte size of the returned file //creationtime,lastaccesstime,lastwritetime can be modified.
How do I choose which class to use? File or FileInfo
Directory/file only contains static methods, cannot be instantiated, only need to provide the appropriate file system object path can be used, the use of high efficiency
This is useful when only one operation is performed on a file or folder.
Directoryinfo/fileinfo If you use an object to perform multiple operations, it is efficient to use these classes.
Because they read the authentication and other information of the file system object at construction time, no matter how many methods are called, they do not need to read the information again.
Only the action files are listed above, and the Action folder requires DirectoryInfo or directory
C # Action Flow object
Pending update
Manipulate files/folders with C # (delete, copy, move)