|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionI needed a program that could help me clean up my old pc. Therefore I wanted a program that could show the size of each folder. My first thought was that .NET 2.0 (C#) was the perfect for that job - and it was! Using the codeAs I said the code for this is really short. Especially with the .NET 2.0 version of GetDirectories which has and option for searching all subdirectories, this was a very easy job. The code that does the work: private DataTable m_dirs = new DataTable(); private Dictionary<STRING double=""> </STRING> m_dirsSize = new Dictionary<STRING double=""> </STRING>(); private bool LoopFolder(DirectoryInfo root) { if (this.backgroundWorker1.CancellationPending) return false; foreach (DirectoryInfo di in root.GetDirectories("*", SearchOption.AllDirectories)) { string fullname = di.FullName; SetStatusBar(fullname); double size = GetFilesSize(di); DataRow dr = m_dirs.NewRow(); dr["Name"] = fullname; dr["Size"] = size; m_dirs.Rows.Add(dr); m_dirsSize.Add(fullname, size); // sum up the fullsize on all parents DirectoryInfo parent = di.Parent; while (parent != null && !this.backgroundWorker1.CancellationPending) { if (m_dirsSize.ContainsKey(parent.FullName)) { m_dirsSize[parent.FullName] += size; parent = parent.Parent; } else break; } } return true; } The program loops all directories and adds them to a DataTable. When finish the DataTable can then be bound to the gridview. The program uses a generic Dictionary to sum up the size of each parent. This could also have been sum up in the DataTable but it is must faster using a generic Dictionary. When the loop is finish the program runs through every DataRow in the DataTable to round the size and to insert the accumulated size in the DataTable: foreach (DataRow dr in m_dirs.Rows) { dr["Size"] = Math.Round((double)dr["Size"], 2); string fullname = (string)dr["Name"]; if (m_dirsSize.ContainsKey(fullname)) dr["Fullsize"] = Math.Round(m_dirsSize[fullname], 2); } One last thing that we usually also wants it to see the size of each file in a folder. if (grdDirectories.SelectedRows.Count == 1) { string directory = grdDirectories.SelectedRows[0].Cells[0].Value.ToString(); toolStripStatusLabel1.Text = directory; DataTable tblFiles = new DataTable(); tblFiles.Columns.Add("Name", typeof(string)); tblFiles.Columns.Add("Size", typeof(double)); tblFiles.Columns.Add("LastAccessTime", typeof(DateTime)); tblFiles.Columns.Add("LastWriteTime", typeof(string)); tblFiles.Columns.Add("CreationTime", typeof(string)); tblFiles.Columns.Add("ReadOnly", typeof(bool)).ReadOnly = true; tblFiles.Columns.Add("Ext", typeof(string)); DirectoryInfo di = new DirectoryInfo(directory); foreach (FileInfo fi in di.GetFiles()) { DataRow dr = tblFiles.NewRow(); dr["Name"] = fi.Name; dr["LastAccessTime"] = fi.LastAccessTime; dr["Size"] = Math.Round(fi.Length / 1024.0 / 1024.0, 2); // size in MB dr["LastWriteTime"] = fi.LastWriteTime; dr["ReadOnly"] = fi.IsReadOnly; dr["CreationTime"] = fi.CreationTime; dr["Ext"] = fi.Extension; tblFiles.Rows.Add(dr); } tblFiles.DefaultView.Sort = "Size desc"; grdFiles.DataSource = tblFiles; } You can actually bind the gridview directly to the FileInfo[] from the GetFiles(). But to control which columns is showed I use a DataTable. Using a DataTable also makes it possible to sort gridview. Points of InterestOne thing that would be nice to add is the opportunity to delete a folder or file.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||