Managing the types of files and folders
1 How to manipulate files and folders
2 How to implement the monitoring function of files and folders
Managing the types of files and folders
1 How to manipulate files and folders
. The FileInfo and Dictionaryinfo two types are available in the net built-in classes, respectively, to manipulate files and folders. Different from file and dictionary types. The main function of FileInfo and Dictionaryinfo is to manipulate the structure of files and folders in the file system, and to perform functions such as creating, copying, reading information, moving, judging existence and deleting.
Example:
classUsedirectoryinfo {Static voidMain (string[] args) {processdirectory (); Console.read (); } //function of the action folder Static voidprocessdirectory () {DirectoryInfo di=NULL; Try { //Create a folderDI =NewDirectoryInfo ("e:\\test"); Di. Create (); //to add subfolders to a folderDi. Createsubdirectory ("Subdic"); //add a file to a subfolderFileInfo info =NewFileInfo ("E:\\test\\subdic\\text.txt"); using(FileStream fs =info. Create ()) {}//Read folder informationreaddirectoryinfo (DI); //move a folderDi. MoveTo ("E:\\test2"); } finally { if(Di! =NULL)
{ //Delete a folderDi. Delete (true);//Delete sub-files and subfolders together, be careful!!!
}
} } //the ability to read folder state information Static voidreaddirectoryinfo (DirectoryInfo di) {Console.WriteLine ("the folder name is:"+di. Name); Console.WriteLine ("The parent folder is:"+di. Parent.name); Console.WriteLine ("folder created in:"+di. CreationTime); Console.WriteLine ("folder Last modified:"+di. LastWriteTime); Console.WriteLine ("folder last access time:"+di. LastAccessTime); Console.WriteLine ("the folder full name is:"+di. FullName); Console.WriteLine ("files in the folder:"); fileinfo[] FIS=di. GetFiles (); foreach(FileInfo fiinchFIS) Readfileinfo (FI); Console.WriteLine ("Subfolders in the folder:"); directoryinfo[] Dis=di. GetDirectories (); foreach(DirectoryInfo SDIinchdis) Readdirectoryinfo (SDI); } //the ability to read file status information Static voidreadfileinfo (FileInfo fi) {Console.WriteLine ("the file name is:"+fi. Name); Console.WriteLine ("files in:"+fi. DirectoryName); Console.WriteLine ("length of File:"+fi. Length); Console.WriteLine ("file created in:"+fi. CreationTime); Console.WriteLine ("file Last modified:"+fi. LastWriteTime); Console.WriteLine ("Last file access time:"+fi. LastAccessTime); Console.WriteLine ("the full file name is:"+fi. FullName); Console.WriteLine ("The file name extension is:"+fi. Extension); } }
Output:
The folder name is: Test
The parent folder is: E:\
Folder created on: 2015/8/16 23:58:21
Folder Last modified: 2015/9/10 17:17:09
Folder last access time: 2015/9/10 17:17:09
Folder full name is: E:\Test
Files in the folder:
File name is: Person.txt
Files in: E:\Test
Length of File: 1092
File created on: 2015/8/16 23:59:30
File Last modified: 2015/8/17 2:02:07
File last access time: 2015/8/16 23:59:30
The full file name is: E:\Test\person.txt
The file extension is:. txt
Subfolders in the folder:
Folder name is: Subdic
The parent folder is: Test
Folder created on: 2015/9/10 17:17:09
Folder Last modified: 2015/9/10 17:17:09
Folder last access time: 2015/9/10 17:17:09
Folder full name is: E:\Test\subdic
Files in the folder:
File name is: Text.txt
Files in: E:\Test\subdic
Length of File: 0
File created on: 2015/9/10 17:17:09
File Last modified: 2015/9/10 17:17:09
File last access time: 2015/9/10 17:17:09
The full file name is: E:\Test\subdic\text.txt
The file extension is:. txt
Subfolders in the folder:
FileInfo and Dictionaryinfo operations involve resource contention and do not guarantee the existence of a folder or file, and generally all operations need to be contained within a try, catch, finally block.
2 How to implement the monitoring function of files and folders
. NET provides the FileSystemWatcher type two implementation of file system monitoring. By controlling the monitoring directory, the control type and the callback method, it is easy to implement the monitoring function, but you need to be careful to filesystemwatcher the cache overflow situation.
FileSystemWatcher maintains an internal buffer to accept file system modification notifications, but when the file system changes too much in a short period of time, the buffer overflows, causing some changes to be lost. The size of the internal cache can be modified through internalbuffersize, but this has a significant impact on performance, and it is recommended to avoid loss of file modification events by speeding up processing and reducing the scope of monitoring.
Example:
classUSEFS_FSW {//Exit program Command Private ConstString Exit ="Exit"; //the Monitored folder Private ConstString Folder ="e:\\test"; PrivateFileSystemWatcher _FSW; PublicUSEFS_FSW () {_FSW=NewFileSystemWatcher (); //set up a detection folder if(directory.exists (folder)) Directory.delete (folder,true); Directory.CreateDirectory (Folder); _FSW. Path=Folder; //This selects the last time to access the file, the last time to write the file,//trigger event when file name, folder name changes_FSW. NotifyFilter =notifyfilters.lastaccess|Notifyfilters.lastwrite|Notifyfilters.filename|Notifyfilters.directoryname; //choose to detect all zip files here_FSW. Filter ="*.zip"; //add a processing method for these events:_FSW. Changed + =NewFileSystemEventHandler (OnChanged); _FSW. Created+=NewFileSystemEventHandler (OnChanged); _FSW. Deleted+=NewFileSystemEventHandler (OnChanged); _FSW. Renamed+=NewRenamedeventhandler (onrenamed); //All events will be triggered at the beginning of this._FSW. EnableRaisingEvents =true; } //handle change times, changes can include create, modify, and delete Private voidOnChanged (Objectsource, FileSystemEventArgs E) {Console.WriteLine ("File:"+E.fullpath+" "+e.ChangeType); } //Handling Renaming Events Private Static voidOnrenamed (Objectsource, RenamedEventArgs E) {Console.WriteLine ("file: {0} renamed to: {1}", E.oldfullpath, E.fullpath); } Static voidMain (string[] args) { Try{USEFS_FSW ws=NewUSEFS_FSW (); while(Console.ReadLine ()! =Exit); } finally { //Clean up test data if(directory.exists (folder)) Directory.delete (folder,true); } } }
Create T.zip in folder, rename to T1.zip, modify content, delete, output as follows:
Files: E:\Test\t.zip Created
File: E:\Test\t.zip renamed as: E:\Test\t1.zip
Files: E:\Test\t1.zip Changed
Files: E:\Test\t1.zip Deleted
Reprint please specify the source:
Jesselzj
Source: http://jesselzj.cnblogs.com
. NET Basics (14) Managing the types of files and folders