I have a listview in C# with four columns and add an item with a specified directory (i.e. c or other drive). I have done it like this and it seems to work:
public Form1() { InitializeComponent(); listView1.View = View.Details; listView1.GridLines = true; listView1.FullRowSelect = true; // for adding column in listview listView1.Columns.Add("Index", 60); listView1.Columns.Add("File Name", 300); listView1.Columns.Add("Size", 100); listView1.Columns.Add("Readonly", 70); int i = 1; // Get all text files of specified directory to add a listview string[] filePaths = Directory.GetFiles(@"D:\\Anupam Mishra", "*.txt", SearchOption.AllDirectories); foreach (var item in filePaths) { long len = newFileInfo(item.ToString()).Length; String name = newFileInfo(item.ToString()).Name; // for adding item in listview ListViewItem _item = newListViewItem(); _item.Text = i.ToString(); _item.SubItems.Add(name); _item.SubItems.Add(BytesToString(len)); _item.SubItems.Add(filePaths.IsReadOnly.ToString()); // adding item in listview listView1.Items.Add(_item); i++; } }
for checking the file size we create a new function BytesToString(double) as below:
It returns total size and unit of file size.
staticString BytesToString(long byteCount) { string[] _unit = { "Byte", "KB", "MB", "GB", "TB", "PB", "EB" }; if (byteCount == 0) return"0" + _unit [0]; long bytes = Math.Abs(byteCount); int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024))); double num = Math.Round(bytes / Math.Pow(1024, place), 1); return (Math.Sign(byteCount) * num).ToString() + " "+_unit [place]; }
It runs where user click on the button and display current selected row data. Code as follow:
Anupam Mishra
18-Jan-2016I have a listview in C# with four columns and add an item with a specified directory (i.e. c or other drive). I have done it like this and it seems to work:
for checking the file size we create a new function BytesToString(double) as below:
It returns total size and unit of file size.
It runs where user click on the button and display current selected row data. Code as follow:
Output: