CÁC GIẢI PHÁP LẬP TRÌNH C# - Trang 198

198
Chương 6: Windows Form

using System.IO;

public class TagPropertyExample : System.Windows.Forms.Form (

// (Bỏ qua phần mã designer.)

private void TagPropertyExample_Load(object sender,

System.EventArgs e) {

// Lấy tất cả các file trong thư mục gốc ổ đĩa C.

DirectoryInfo directory = new DirectoryInfo("C:\\");

FileInfo[] files = directory.GetFiles();

// Hiển thị tất cả các file trong ListView.

foreach (FileInfo file in files) {

ListViewItem item = listView.Items.Add(file.Name);

item.ImageIndex = 0;

item.Tag = file;

}

}

private void listView_ItemActivate(object sender,

System.EventArgs e) {

// Lấy kích thước file.

ListViewItem item = ((ListView)sender).SelectedItems[0];

FileInfo file = (FileInfo)item.Tag;

string info = file.FullName + " is " + file.Length + " bytes.";

// Hiển thị kích thước file.

MessageBox.Show(info, "File Information");

}

}