. NET Fundamentals Review (vii)

Source: Internet
Author: User

A Recursively loading a directory tree

Examples of programs:

1  Public Partial classForm1:form2     {3         stringPath =@"D:\ League of Legends";4 5          PublicForm1 ()6         {7 InitializeComponent ();8         }9 Ten         Private voidLoadtree (stringPath, TreeNode node =NULL) One         { A             string[] dirs =directory.getdirectories (path); -             foreach(stringDirinchdirs) -             { theTreeNode Node1 =NewTreeNode (Path.getfilename (dir)); -                 if(node = =NULL) -                 { - TvData.Nodes.Add (node1); +                 } -                 Else +                 { A node. Nodes.Add (Node1); at                 } -                 if(Directory.getdirectories (dir). Length >0) -                 { - Loadtree (dir,node1); -                 } -             } in         } -  to         Private voidForm1_Load (Objectsender, EventArgs e) +         { - loadtree (path); the         } *}

Two File read/write stream operation

Examples of programs:

1 Private voidButton1_Click (Objectsender, EventArgs e)2         {3             //reading data from a file stream4FileStream stream =NewFileStream (@"E:\WebSite\web.config", FileMode.Open);5             byte[] buffer =New byte[1024x768*1024x768];6Stream. Read (Buffer,0, buffer. Length);7             stringContent =Encoding.Default.GetString (buffer);8Txtcontent.text =content;9 Stream. Dispose ();Ten         } One  A         Private voidButton2_Click (Objectsender, EventArgs e) -         { -             //writing data through a file stream theSaveFileDialog SFD =NewSaveFileDialog (); -DialogResult res =SFD. ShowDialog (); -             if(res = =DialogResult.OK) -             { +FileStream stream =NewFileStream (SFD). Filename,filemode.create); -                 stringContent =Txtcontent.text; +                 byte[] buffer =Encoding.UTF8.GetBytes (content); AStream. Write (Buffer,0, buffer. Length); at Stream. Dispose (); -             } -             //FileStream stream = new FileStream (@ "C:\Users\ teng \desktop\"); -}

  

Three Large file copy

Examples of programs:

1 Private voidButton3_Click (Objectsender, EventArgs e)2         {3             //preparing a file stream for reading data4FileStream StreamReader =NewFileStream (@"e:\ Instructional Video \jquery\ 2nd day \1_shangwu1-style Operation 1.avi", FileMode.Open);5             //preparing a file stream for writing data6FileStream StreamWriter =NewFileStream (@"C:\Users\ Teng \desktop\2.avi", FileMode.Create);7             //prepare a byte array to prepare to save the read data8             byte[] data =New byte[1024x768*1024x768*2];9             //Read and write operationsTen             intLength =0; One              Do A             { -Length = StreamReader.Read (data,0, data. Length); -StreamWriter.Write (data,0, length); the} while(Length >=data. Length); -             //Close File Stream - streamreader.dispose (); - streamwriter.dispose (); +MessageBox.Show ("Operation Successful! "); -         } +Small application: You can modify the value when reading the file data into a byte array, which can achieve the effect of encryption to some extent.

Four. The nature of Using

1. The Dispose () method of the object is called by the using managed object, out of {}.

2. If the object wants to be managed by using, the object's class must implement the Idisposeable interface.

3. The nature of the using is actually Try-catch, the code in the using is generated in a try and the Disopose method of the calling object is written in the finally, so the Dispose method is called anyway.

Five Serialization and deserialization

code example:

1 namespaceserialization and deserialization2 {3 [Serializable]4     class Person5     {6          Public stringName {Get;Set; }7          Public intAge {Get;Set; }8         //Public dog Dog;9     }Ten  One [Serializable] A     classDog -     { } - } the  - Static voidMain (string[] args) -         { -             //the state of the object needs to be persisted. +             //serialization: Persist the state of an object to a device (disk) -             //An object of the class to be marked as serializable can be serialized +             //serialization in binary mode A  at             //Serialization of -             //Person p = new Person () {age = 12,name = "Rose"}; -             //BinaryFormatter bf = new BinaryFormatter (); -             //using (FileStream fs = new FileStream ("Se.bin", FileMode.Create)) -             //{ -             //BF. Serialize (fs,p); in             //} -  to             //deserialization +BinaryFormatter BF =NewBinaryFormatter (); -             using(FileStream fs =NewFileStream ("Se.bin", FileMode.Open)) the             { *                 Objectobj =BF. Deserialize (FS); $Person p = obj asPerson ;Panax NotoginsengConsole.WriteLine (p.name+":"+p.age); -             } the  + Console.readkey (); A}

. NET Fundamentals Review (vii)

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.