File and folder operations
C/s:winform can manipulate client files
b/S: Browser service brower Server
Namespaces:using System. IO;
1. File class: Files
Create: file.create (path); Create file, return FileStream
FileStream fs = file.create (path), after which it needs to be closed or not open,fs.close ();
Delete: file.delete (path); No return value
To copy a file: file.copy (source file, target file);
Cut Files: file.move (source file path, target path);
determine if the file exists: file.exists (path); Returns a Boolean that indicates that an existing
file encryption: File.encrypt (); The file name becomes green or the current user can open the copy to another user that cannot be opened
file decryption: File.decrypt ();
file.getcreationtime (path); Gets the creation time , returning the datetime type setcreationtime(Path, datetime type); Modify creation Time
file.getlastaccesstime (path); Last access Time , returns the datetime type setlastaccesstime(Path, datetime type); Modify Access Time
file.getlastwritetime (path); Last modified time , return datetime type setlastwritetime(Path, datetime type); Modify Modification Time
2. Directory classes, directories (folders)
Directory. CreateDirectory (path); Create a directory
Directory. Delete (path); Delete directory
Directory. Exists (path); whether the directory exists
Three-time get and set same file class
Directory. GetDirectories (path); gets a subdirectory that returns a string array
Directory. GetFiles (path); Get child Files! Name! , returns a string array , string[] s = Directory. GetFiles (path);
Directory. Getdirectoryroot (path); Get root directory
Directory. GetParent (path); get top level directory
3. FileInfo class
Is the instance method, need to create object new to use, above is the static method of file
To create a file: FileInfo f = new FileInfo (path); FileStream s = f.create (); s.close ();
To delete a file: FileInfo f = new FileInfo (path); f.delete ();
To copy a file: FileInfo f = new FileInfo (path) , F.copyto (target path, whether overwrite (true is overwrite));
To move a file: FileInfo f = new FileInfo (path); F.moveto (target path);
whether the file exists: FileInfo f = new FileInfo (path); bool B = f.exists; Boolean, is a property
Get file name: FileInfo f = new FileInfo (path);string s = f.fullname; property, file name with path
Get creation Time: DateTime d = f.creationtime; All three times are the same, all are attributes
Set creation time: F.creationtime = DateTime.Now.AddDays (+); All three of them are the same.
get file Size: F.length
4. DirectoryInfo class
To Create a directory: DirectoryInfo d = new DirectoryInfo (path); d.create ();
To Delete a directory: D.delete ();
To move a directory: D.moveto (target path);
whether the directory exists: bool B = d.exists;
Get directory full name: D.fullname;
get the Sub file! Object Information! : fileinfo[] f = d.getfiles (); Returns an array of File objects , in more detail,d.getfiles ("*.exe") only gets EXE files
Get subdirectories: directoryinfo[] dr = D.getdirectories ();
Example: (with recursion)
1. Get all the files under the folder and output
//get all the files under the folder and output Private voidGetallfiles (stringpath) { //Create folder Information ObjectDirectoryInfo DF =NewDirectoryInfo (path); //get all files under this folderfileinfo[] Fsz =DF. GetFiles (); //Output File Information foreach(FileInfo datainchFsz) {richTextBox1.Text+ = data. FullName +"|***|"; } //get all subfolders under this folderdirectoryinfo[] Dsz =DF. GetDirectories (); //Traverse foreach(DirectoryInfo DatadinchDSZ) {getallfiles (Datad). FullName); } }
2. To a folder, get the number of all files in the folder
Private voidButton1_Click (Objectsender, EventArgs e) {Label1. Text= FileCount (@"E:\test"). ToString (); } Private intFcount =0; Private intFileCount (stringpath) { //Create folder Information ObjectDirectoryInfo DWJJ =NewDirectoryInfo (path); //the number of files under the current folderFcount + =DWJJ. GetFiles (). Length; //take all folders in the current directory foreach(DirectoryInfo DinchDWJJ. GetDirectories ()) {filecount (d.fullname); } returnFcount; }
3. To a folder, get the number of folders under the file
Private intDCount =0; Private intDircount (stringpath) { //Create a folder information ObjectDirectoryInfo d =NewDirectoryInfo (path); //take all folders under this directorydirectoryinfo[] df =d.getdirectories (); //cumulative number of foldersDCount + =DF. Length; //Traverse all Folders foreach(DirectoryInfo WinchDF) {Dircount (w.fullname); } returnDCount; } Private voidButton2_Click (Objectsender, EventArgs e) {Label2. Text= Dircount (@"E:\test"). ToString (); }
4. To a folder, get the folder size
Private voidButton3_Click (Objectsender, EventArgs e) {Label3. Text= Dirsize (@"E:\test"). ToString (); } Private LongSize =0; Private LongDirsize (stringpath) { //Create a directory information ObjectDirectoryInfo d =NewDirectoryInfo (path); //take all files in the current directory foreach(FileInfo datainchD.getfiles ()) {Size+=data. Length; } //take all folders in the current directory foreach(DirectoryInfo datainchd.getdirectories ()) {dirsize (data. FullName); } returnsize; }
5. To a folder, delete the folder
Private voidButton4_Click (Objectsender, EventArgs e) {Deletedir (@"E:\test"); } Private voidDeletedir (stringpath) { //Creating directory information ObjectsDirectoryInfo d =NewDirectoryInfo (path); //take all the files in that directory, delete foreach(FileInfo datainchD.getfiles ()) {data. Delete (); } //Delete Subfolders foreach(DirectoryInfo datainchd.getdirectories ()) {Deletedir (data. FullName); } //Delete an empty folderD.delete (); }
WinForm file Operations