C # forms WinForm file operations

Source: Internet
Author: User

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); Return Boolean, True indicates 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 property

Set creation time:f.creationtime = DateTime.Now.AddDays (100); 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 ();

Delete directory: d.delete ();

Mobile 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, the contents of which are more detailed, D.getfiles ("*.exe") only get EXE files

Get subdirectories: directoryinfo[] dr = D.getdirectories ();


Example: (with recursion)

1. Get all the files under the folder and output

        Get all files under folder and output        private void Getallfiles (string path)         {            //Build Folder information Object            DirectoryInfo df = new DirectoryInfo (path);            Get all files under this folder            fileinfo[] Fsz = df. GetFiles ();            Output file Information            foreach (FileInfo data in Fsz)            {                richTextBox1.Text + = data. FullName + "|***|";             }            Get all subfolders under the folder            directoryinfo[] Dsz = df. GetDirectories ();            Traverse            foreach (DirectoryInfo Datad in Dsz)             {                getallfiles (Datad. FullName);             }        }

2. To a folder, get the number of all files in the folder

        private void Button1_Click (object sender, EventArgs e)        {            Label1. Text = FileCount (@ "E:\test"). ToString ();        }        private int fcount = 0;        private int FileCount (string path)        {            //Create folder information Object            DirectoryInfo dwjj = new DirectoryInfo (path);            Take the number of files under the current folder            Fcount + = Dwjj. GetFiles (). Length;            Take all folders in the current directory            , foreach (DirectoryInfo d in DWJJ. GetDirectories ())            {                filecount (d.fullname);            }            return fcount;                  }

3. To a folder, get the number of folders under the file

        private int dcount = 0;        private int Dircount (string path)        {            //Create a Folder information object            DirectoryInfo d = new DirectoryInfo (path);            Take all folders under the directory            directoryinfo[] df = d.getdirectories ();            Cumulative number of folders            DCount + = df. Length;            Traverse all Folders            foreach (DirectoryInfo w in df)            {                dircount (w.fullname);            }            return dcount;        }        private void Button2_Click (object sender, EventArgs e)        {            Label2. Text = Dircount (@ "E:\test"). ToString ();        }

4. To a folder, get the folder size

        private void Button3_Click (object sender, EventArgs e)        {            label3. Text = Dirsize (@ "E:\test"). ToString ();        }        Private long size = 0;        Private long Dirsize (string path)        {            //Create a directory information object            DirectoryInfo d = new DirectoryInfo (path);            Take all files in the current directory,            foreach (FileInfo data in D.getfiles ())            {                size + = data. Length;            }            Take all folders in the current directory,            foreach (DirectoryInfo data in D.getdirectories ())            {                dirsize (data). FullName);            }            return size;        }


5. To a folder, delete the folder

        private void Button4_Click (object sender, EventArgs e)        {            Deletedir (@ "E:\test");        }        private void Deletedir (string path)        {            //Build directory information Object            DirectoryInfo d = new DirectoryInfo (path);            Remove all files from the directory and delete the            foreach (FileInfo data in D.getfiles ())            {                data. Delete ();            }            Delete the subfolder            foreach (DirectoryInfo data in D.getdirectories ())            {                deletedir (data. FullName);            }            Delete empty folder            D.delete ();        }

C # forms WinForm file operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.