当前位置:网站首页>FileInfo and directoryinfo classes

FileInfo and directoryinfo classes

2020-11-10 17:23:20 Qing and time

adopt FileInfo and DirectoryInfo Class to read file and folder properties View file properties , create a file , Moving files , Rename file Determine if the path exists , Create directory

1、 The following classes are used to browse the file system and perform operations , Like moving , Copy and delete files .

System.MarshalByRefObject This is .NET Class is the base object class for remote operations , It allows you to group data between application domains .

FileSystemInfo This is the base class for any file system object

FileInfo and File These classes represent files on the file system

DirectoryInfo and Directory Represents the folder on the file system

Path Contains some static methods for handling pathnames

DriveInfo Its properties and methods provide information about the specified drive

2、 Representing files and folders .NET class

We have two classes for folders and two classes for files Directory( Folder ) and  File( file ) Class contains only static methods , Can't be instantiated . If you only perform one operation on a folder or file , Using these classes is very effective , Save to instantiate .NET Class overhead .

DirectoryInfo Classes and FileInfo Class implementation and Directory and File The same public approach , They have some common properties and constructors , None of these classes are static . You need to instantiate these classes , Then associate each instance with a specific folder or file . If you use the same object to perform multiple operations , It's better to use these classes , This is because at construction time they will read the authentication and other information of the appropriate file system object , No matter how many methods are called per object , No need to read this information again .

FileInfo and DirectoryInfo class

about FileInfo and DirectoryInfo Many methods in the class can also be used File and Directory Many of the methods in . 

            //1, Complete a copy of a file 
            FileInfo myFile = new FileInfo(@"c:\pxx\xx\xxx\xxx.txt");
            myFile.CopyTo(@"d:\xx\xx.txt");// Copy files 
                                           // Corresponding File Processing mode 
            File.Copy(@"c:\xxx\xx\xx\xx.txt", @"d:\xx\xx\xx.txt");

            //2, Determine if a folder exists 
            DirectoryInfo myFolder = new DirectoryInfo(@"c:\program files");
            myFolder.Exists

about FileInfo, perhaps DirectoryInfo When we construct , If you pass a non-existent file or folder path , There will be no exceptions at this time , Only when you use this file or folder will there be a problem . FileInfo and DirectoryInfo The objects of can pass through Exists Property to determine whether the file or folder exists .

 

 FileInfo and DirectoryInfo List of properties

 

attribute effect
CreationTime Time when the file or folder was created
DirectoryName( be used for FileInfo) Contains the full path to the folder
Parent( be used for DirectoryInfo) Specify the parent directory of the subdirectory
Exists Does the file or folder exist
Extension Extension of the file , For folders , It returns blank
FullName The full pathname of a file or folder
LastAccessTime The last time a file or folder was accessed
LastWriteTime The last time a file or folder was modified
Name The name of the file or folder
Root( Only used for DirectoryInfo The root part of the path
Length( Only used for FileInfo) Returns the size of the file ( In bytes )

 

FiInfo and DirectoryInfo List of methods

Method effect
Create() Create a folder or empty file with the given name , about FileInfo, This method returns a flow object , To facilitate writing to the file .
Delete() Delete files or folders . For a folder, there is a recursive Delete Options
MoveTo() Move or rename files or folders
CopyTo() ( Used only for FileInfo) Copy file , Folder has no copy method , If you want to copy the entire tree , You need to copy each file and folder separately
GetDirectories() ( Only applicable to DirectoryInfo) return DirectoryInfo An array of objects , This array represents all the folders contained in the folder
GetFiles() ( Only applicable to DirectoryInfo) return FileInfo An array of objects , This array represents all the files in the folder
GetFileSystemInfos() ( Only applicable to DirectoryInfo) return FileInfo and DirectoryInfo object , It represents all the objects contained in a folder as a FileSystemInfo Reference array

版权声明
本文为[Qing and time]所创,转载请带上原文链接,感谢

随机推荐