(+84) 236.3827111 ex. 402

Viết chương trình lấy thông tin từ các ổ đĩa và hiển thị trên LISTVIEW


Tạo một lệnh trên form (c#) và code đoạn lệnh sau:

privatevoid button1_Click(object sender, EventArgs e)

{

//Khai bao su dung doi tuong DirectoryInfo

DirectoryInfo dir = new DirectoryInfo(@"C:\Windows\");

//Khai số cột

this.listView1.Columns.Add("No", 45,

HorizontalAlignment.Center);

this.listView1.Columns.Add("Name", 120,

HorizontalAlignment.Left);

this.listView1.Columns.Add("Size", 50,

HorizontalAlignment.Right);

this.listView1.Columns.Add("Type", 120,

HorizontalAlignment.Right);

int i = 0;

//Cho phép chọn hàng

listView1.FullRowSelect = true;

//Khai báo thuộc tính CheckBoxer

listView1.CheckBoxes = true;

//Chọn chế độ trình bày

listView1.View = View.Details;

//Khai báo đối tượng ListView

ListViewItem item1;

//Duyệt qua từng tập tin

foreach (FileInfo f in dir.GetFiles("*.*"))

{

i++;

item1 = new ListViewItem(i.ToString());

//Trình bày thuộc tính tên tập tin

item1.SubItems.Add(f.Name);

//Trình bày thuộc tính kích thước tập tin

item1.SubItems.Add(

f.Length.ToString());

//Trình bày thuộc tính của tập tin

item1.SubItems.Add(

f.Attributes.ToString());

//thêm đối tượng item1 vào đối tượng listview1

listView1.Items.Add(item1);

}

}