.NET基礎回顧(七)

來源:互聯網
上載者:User

標籤:des   style   blog   color   os   ar   for   sp   strong   

一.遞迴載入分類樹

程式樣本:

 1 public partial class Form1 : Form 2     { 3         string path = @"D:\英雄聯盟"; 4  5         public Form1() 6         { 7             InitializeComponent(); 8         } 9 10         private void LoadTree(string path, TreeNode node = null)11         {12             string[] dirs = Directory.GetDirectories(path);13             foreach (string dir in dirs)14             {15                 TreeNode node1 = new TreeNode(Path.GetFileName(dir));16                 if (node == null)17                 {18                     tvData.Nodes.Add(node1);19                 }20                 else21                 {22                     node.Nodes.Add(node1);23                 }24                 if (Directory.GetDirectories(dir).Length > 0)25                 {26                     LoadTree(dir,node1);27                 }28             }29         }30 31         private void Form1_Load(object sender, EventArgs e)32         {33             LoadTree(path);34         }35     }

二.檔案讀寫流操作

程式樣本:

 1 private void button1_Click(object sender, EventArgs e) 2         { 3             //通過檔案流讀取資料 4             FileStream stream = new FileStream(@"E:\WebSite\web.config",FileMode.Open); 5             byte[] buffer = new byte[1024*1024]; 6             stream.Read(buffer,0,buffer.Length); 7             string content = Encoding.Default.GetString(buffer); 8             txtContent.Text = content; 9             stream.Dispose();10         }11 12         private void button2_Click(object sender, EventArgs e)13         {14             //通過檔案流寫入資料15             SaveFileDialog sfd = new SaveFileDialog();16             DialogResult res = sfd.ShowDialog();17             if (res == DialogResult.OK)18             {19                 FileStream stream = new FileStream(sfd.FileName,FileMode.Create);20                 string content = txtContent.Text;21                 byte[] buffer = Encoding.UTF8.GetBytes(content);22                 stream.Write(buffer,0,buffer.Length);23                 stream.Dispose();24             }25             //FileStream stream = new FileStream(@"C:\Users\騰\Desktop\");26         }

  

三.大檔案拷貝

程式樣本:

 1 private void button3_Click(object sender, EventArgs e) 2         { 3             //準備用於讀資料的檔案流 4             FileStream streamReader = new FileStream(@"E:\教學視頻\jQuery\第2天\1_shangwu1-樣式操作1.avi", FileMode.Open); 5             //準備用於寫資料的檔案流 6             FileStream streamWriter = new FileStream(@"C:\Users\騰\Desktop\2.avi", FileMode.Create); 7             //準備一個位元組數組用於準備儲存讀出來的資料 8             byte[] data = new byte[1024 * 1024 * 2]; 9             //讀寫操作10             int length = 0;11             do12             {13                 length = streamReader.Read(data, 0, data.Length);14                 streamWriter.Write(data, 0, length);15             } while (length >= data.Length);16             //關閉檔案流17             streamReader.Dispose();18             streamWriter.Dispose();19             MessageBox.Show("操作成功!");20         }21 小應用:可以在讀取檔案資料到byte數組中時修改數值,可以在一定程度上達到加密的效果。

四.Using的本質

1. 被Using管理的對象,出了{}就會被調用該對象的Dispose()方法。

2. 如果對象想要被Using管理,這個對象的類必須要實現IDisposeable介面。

3. using的本質其實就是try-catch,將using中的代碼產生在try中,將調用對象的Disopose方法寫在finally中,所以Dispose方法無論如何都會被調用。

五.序列化和還原序列化

程式碼範例:

 1 namespace 序列化和還原序列化 2 { 3     [Serializable] 4     class Person 5     { 6         public string Name { get; set; } 7         public int Age { get; set; } 8         //public Dog dog; 9     }10 11     [Serializable]12     class Dog13     { }14 }15 16 static void Main(string[] args)17         {18             //需要將對象的狀態儲存起來 持久化19             //序列化:將對象的狀態儲存持久化到某一裝置上(磁碟)20             //要將類標記為Serializable 這個類的對象才可以被序列化21             //以二進位方式序列化22 23             //序列化24             //Person p = new Person() { Age = 12,Name = "Rose"};25             //BinaryFormatter bf = new BinaryFormatter();26             //using (FileStream fs = new FileStream("se.bin", FileMode.Create))27             //{28             //    bf.Serialize(fs,p);29             //}30 31             //還原序列化32             BinaryFormatter bf = new BinaryFormatter();33             using (FileStream fs = new FileStream("se.bin", FileMode.Open))34             {35                 object obj = bf.Deserialize(fs);36                 Person p = obj as Person;37                 Console.WriteLine(p.Name+":"+p.Age);38             }39 40             Console.ReadKey();41         }

 

.NET基礎回顧(七)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.