C # File operations

Source: Internet
Author: User

File operations

File class, FileInfo class. Using System.IO Namespace
(i) Create

Method One:

1         private string path = @ "F:\Text\aaa.txt"; 2         private void Createfile_click (object sender, EventArgs e) 3         {4< C6/>filestream fs = file.create (path); 5             fs. Close ();//return FileStream must free stream 6         }

Method Two:

1         private string path = @ "F:\Text\aaa.txt"; 2         private void Createfile_click (object sender, EventArgs e) 3         {            4             FileInfo CreateFile = new FileInfo (path); 5             FileStream stream = Createfile.create (); 6             stream. Close ();//return FileStream type must free stream 7         }

(ii) deletion

Method One:

1         private string path = @ "F:\Text\aaa.txt"; 2         private void Deletefile_click (object sender, EventArgs e) 3         {4< C5/>file.delete (path); 5         }

Method Two:

1         private string path = @ "F:\Text\aaa.txt"; 2         private void Deletefile_click (object sender, EventArgs e) 3         { C11/>4             FileInfo deletefile = new FileInfo (path); 5             Deletefile.delete (); 6         }

(c) whether there is

Method One:

1         private string path = @ "F:\Text\aaa.txt"; 2         private void Existfile_click (object sender, EventArgs e) 3         {4
   bool ex = file.exists (path);             5              if (ex) 6              {7                  MessageBox.Show ("presence"), 8              } 9              else10              {one                  MessageBox.Show ("nonexistent"); 12              }13         }

Method Two:

1         private string path = @ "F:\Text\aaa.txt"; 2         private void Existfile_click (object sender, EventArgs e) 3         {
   4              FileInfo existfile = new FileInfo (path), 5              bool ex = existfile.exists, 6              if (ex) 7              {8                  MessageBox. Show ("presence"); 9              }10              else11              {                  MessageBox.Show ("not Present");              }14         }

(iv) Copying---cannot cross-drive characters

Method One:

1         private string path = @ "F:\Text\aaa.txt"; 2         private void Copyfile_click (object sender, EventArgs e) 3         {4             file.copy (path, @ "F:\Text\bbb.txt");//Copy the contents of the content also and copy 5         }

Method Two:

1  private void Copyfile_click (object sender, EventArgs e) 2         {          3             FileInfo CopyFile = new FileInfo (path); 4             Copyfile.copyto (@ "F:\Text\bbb.txt");//Copy the contents of the content also and copy 5         }

(v) Move (renamed)---cannot cross the drive letter

Note: Move () or MoveTo (): 1. Path with different names (equivalent to-cut),

2. Path different names (equivalent to-cut + rename),

3. The path same name is different (equivalent to-rename).

And move all the objects within the folder at the same time.

Method One:

1         private string path = @ "F:\Text\aaa.txt"; 2         private void Movefile_click (object sender, EventArgs e) 3         {4< C10/>file.move (Path, @ "F:\Test\bbb.txt");            5         }

Method Two:

1         private string path = @ "F:\Text\aaa.txt"; 2         private void Movefile_click (object sender, EventArgs e) 3         {           4             FileInfo MoveFIle = new FileInfo (path), 5             Movefile.moveto (@ "F:\Test\ccc.txt"); 6         }

(vi) Obtaining the file name

Note: Only one method of the FileInfo () class

1. Get the file name only (seemingly useless-the content of show () below is Aaa.txt)

1         private string path = @ "F:\Text\aaa.txt"; 2         private void Filename_click (object sender, EventArgs e) 3         {4             FileInfo FileName = new FileInfo (path); 5             String name = Filename.name;6             MessageBox.Show (name); 7         }

2. Or take the full name of the file (including the path)

1         private string path = @ "F:\Text\aaa.txt"; 2         private void Filename_click (object sender, EventArgs e) 3         {            4             FileInfo FileName = new FileInfo (path); 5             String name = Filename.fullname;6             MessageBox.Show (name); 7         }

(vii) Access to extensions

Note: Only one method of the FileInfo () class

1         private string path = @ "F:\Text\aaa.txt"; 2         private void Extensionname_click (object sender, EventArgs e) 3< C14/>{4             FileInfo extensionname = new FileInfo (path); 5             string exname = Extensionname.extension;6             MessageBox.Show (Exname); 7         }

(eight) Get auxiliary properties

Note: Only one method of the file () class

1         private string path = @ "F:\Text\aaa.txt"; 2         private void Getattribute_click (object sender, EventArgs e) 3         { 4             fileattributes Attributes = file.getattributes (path), 5             DateTime createtime = file.getcreationtime (path), 6             DateTime lastaccesstime = file.getlastaccesstime (path); 7             DateTime getlastwritetime =  file.getlastwritetime (path), 8             MessageBox.Show (attributes.tostring () + "\ n" + Createtime.tostring () + "\ n" +lastaccesstime.tostring () + "\ n" +getlastwritetime.tostring ()); 9         }

(ix) Modifying auxiliary properties

Note: Only one method of the file () class

        private string Path = @ "F:\Text\aaa.txt";        private void Button1_Click (object sender, EventArgs e)        {            fileattributes Attributes = new FileAttributes ();            Attributes = Fileattributes.hidden;            File.setattributes (path, Attributes);            File.setcreationtime (path, datetime.now);            File.setlastaccesstime (path, datetime.now);            File.setlastwritetime (path, DateTime.Now);        }

(10) Open Save

File read/write

Note: The class name is written first after opening new class. Close ()

FileStream class: More general.

Open it

1         private string path = @ "F:\Text\aaa.txt"; 2         private void Open_stream_click (object sender, EventArgs e) 3         {4             FileStream stream = new FileStream (path, filemode.open);//read data is stored in binary code Form 5             byte[] nr = new Byte[stream. Length]; 6             Stream. Read (NR,0,NR. Length); 7             Stream. Close (); 8             //Put the contents of byte[] into a string in the text box. 9             String s = System.Text.Encoding.Default.GetString (NR);             TextBox1.Text = s;11         }

Save

1         private string path = @ "F:\Text\aaa.txt"; 2         private void Save_stream_click (object sender, EventArgs e) 3         {4             //Turn the string of text boxes into binary arrays 5             byte[] nr = System.Text.Encoding.Default.GetBytes (TextBox1.Text); 6             //Send to file 7             FileStream stream = new FileStream (path, filemode.openorcreate); 8             Stream. Write (NR, 0, nr. Length); 9             Stream. Close ();         

StreamReader class:

Open it

1         private string path = @ "F:\Text\aaa.txt"; 2         private void Open_reader_click (object sender, EventArgs e) 3         {4             StreamReader reader = new StreamReader (path, encoding.default); 5             string s = Reader. ReadToEnd (); 6             reader. Close (); 7             TextBox1.Text = ""; 8             TextBox1.Text = s;9         }

Or

1         private string path = @ "F:\Text\aaa.txt"; 2         private void Open_writer_click (object sender, EventArgs e) 3
   
    {4             FileStream stream = new FileStream (path, FileMode.Open); 5             StreamReader reader = new StreamReader (Stream, E Ncoding. Default); 6             string s = Reader. ReadToEnd (); 7             Reader. Close (); 8             Stream. Close (); 9             TextBox1.Text = s;10         }
   

StreamWriter class:

1         private string path = @ "F:\Text\aaa.txt"; 2         private void Save_writer_click (object sender, EventArgs e) 3         {4             StreamWriter writer = new StreamWriter (path, false, Encoding.default); 5             writer. WriteLine (TextBox1.Text); 6             writer. Close (); 7         }

Or

1        private string path = @ "F:\Text\aaa.txt"; 2        private void Save_writer_click (object sender, EventArgs e) 3        {4            FileStream stream = new FileStream (path, filemode.openorcreate); 5            StreamWriter writer = new StreamWriter (stream); 6            writer. WriteLine (TextBox1.Text); 7            writer. Close (); 8            Stream. Close (); 9        }10     

C # 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.