File Operations for primary school students and operations for primary school students

Source: Internet
Author: User

File Operations for primary school students and operations for primary school students

How to read and write files

  

// Create a file stream FilesStream myfs = new FilesStream (path, FileMode. create); // Create the writer StreamWriter mySw = new StreamWriter (myfs); // write the entered content to the file mySw. write ("I am a good person"); // close the writer mySw. close (); // Close the file stream myfs. close ();

 

1. How to keep data in memory persistent?

Resolution: Save the database (also file: XXX. MDF)

File (text file, xml file, XX. bin)

2. FileMode values

  FileMode. Create:Check whether the file exists in the corresponding directory. If the file is overwritten, no error is reported.

CreateNew:Create a file. If the file exists, an exception occurs, prompting that the file already exists.

  FileMode. Open ():Open an existing file in the specified path to prepare for reading the file.

  FileMode. OpenOrCreate ():If you have one, read it and write it if you do not have one.

  FileMode. Append:Append new content to the end of the original file

3. encoding for reading files

Resolution: StreamReader sr = new StreamReader (fs, Encoding. GetEncoding ("UTF-8 "));

4. IO schematic

Exercise,Requirement Description:

Allows you to use a small resource manager to display file information.

Use the TreeView control to display the folder structure

Use the ListView control to display the list of objects in the selected folder

1 // <summary> 2 // bind data to the root node 3 of the TreeView control /// </summary> 4 private void LoadTreeView () 5 {6 7 DirectoryInfo dir = new DirectoryInfo (@ "E: \"); 8 // return the subdirectory 9 DirectoryInfo [] dirs = dir in the current directory. getDirectories (); 10 foreach (DirectoryInfo item in dirs) 11 {12 TreeNode tn = new TreeNode (); 13 tn. text = item. name; 14 tn. tag = item. fullName; 15 this. treeView1.Nodes. add (tn); 16} 17 18} 19 private void Form1_Load (o Bject sender, EventArgs e) 20 {21 LoadTreeView (); 22 23} 24 // <summary> 25 // bind the subdirectory 26 // </summary> 27 // <param name = "node"> </param> 28 private void BindInfo (TreeNode node) 29 {30 DirectoryInfo dir = new DirectoryInfo (node. tag. toString (); 31 DirectoryInfo [] dirs = dir. getDirectories (); 32 foreach (DirectoryInfo item in dirs) 33 {34 TreeNode tn = new TreeNode (); 35 tn. text = item. name; 36 tn. tag = ite M. fullName; 37 node. nodes. add (tn); 38} 39 // get the file list under the directory. dir is the directory object 40 FileInfo [] fileInfo = dir. getFiles (); 41 // defines the information of a generic set of stored files 42 List <MyFile> files = new List <MyFile> (); 43 // traverse the file list 44 foreach (FileInfo item in fileInfo) 45 {46 MyFile file = new MyFile (); 47 file. fileName = item. name; 48 float value = item. length; 49 file. fileLength = float. parse (Math. round (value/1024, 2 ). toString (); 50 file. fileType = item. ex Tension; 51 file. FilePath = item. FullName; 52 files. Add (file); 53} 54 if (this. listView1.Items! = Null) 55 {56 this. listView1.Items. clear (); 57} 58 foreach (MyFile item in files) 59 {60 ListViewItem lvItem = new ListViewItem (item. fileName); 61 lvItem. subItems. add (item. fileLength. toString (); 62 lvItem. subItems. add (item. fileType); 63 lvItem. subItems. add (item. filePath); 64 this. listView1.Items. add (lvItem); 65} 66} 67 // click bind file and Folder Information 68 private void treeviewappsafterselect (object sender, TreeViewEventArgs e) 69 {70 // obtain the node 71 TreeNode node in the TreeView control = this. treeView1.SelectedNode; 72 BindInfo (node); 73 74}

Copy and delete files

1 private void copy ToolStripMenuItem_Click (object sender, EventArgs e) 2 {3 // select the target folder 4 DialogResult result = folderBrowserDialog1.ShowDialog (); 5 if (result = DialogResult. OK) 6 {7 // source file path strength 8 string sourcePath = this. listView1.SelectedItems [0]. subItems [3]. text; 9 // path strength of the target file 10 string desPath = folderBrowserDialog1.SelectedPath; 11 desPath + = "\" + this. listView1.SelectedItems [0]. subItems [0]. text; 12 // Copy File 13 File. Copy (sourcePath, desPath); 14 MessageBox. Show ("Copied successfully !!! "); 15} 16} 17 18 private void Delete ToolStripMenuItem_Click (object sender, EventArgs e) 19 {20 string sourcePath = this. listView1.SelectedItems [0]. subItems [3]. text; 21 File. delete (sourcePath); 22 MessageBox. show ("deleted successfully !! "); 23 TreeNode node = this. treeView1.SelectedNode; 24 BindInfo (node); 25}


Summary:

1. Five Steps for reading and writing a file: create a file stream, create a reader, read and write a file, close the reader, and close the file stream.

2. The file stream class is FileStream. When creating a file stream, you must specify the path strength of the Operation file, the file opening form, and the file access method.

3. StreamWriter is a writer and StreamReader is a reader.

4. The File class is used to operate files, such as copying, moving, and deleting files. The Directory class is used to operate folders. They are static classes.

5. static classes only contain static members. Non-static classes can contain static members. Static classes cannot contain instance members. Non-static classes can contain instance members. Static classes use class names to access members, non-static class uses her instance to access members.

6. The FileInfo class has similar functions as the File class. You can also perform basic operations on files. The difference is that the File class cannot instantiate objects. If you want to reuse a file object multiple times, you can use the FileInfo class because security check is not always required.

7. The DirectoryInfo class has similar functions in the Directory class and can perform basic operations on folders. If you want to reuse a directory object multiple times, consider using the instance method of the DirectoryInfo class.

 

Related Article

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.