I found this code on some website, thanks for that.
// Methods
private void AddDirectories(TreeNode tnSubNode)
{
// This method is used to get directories (from disks, or from other directories)
//Clear the TreeView each time the method is called.
treeView1.BeginUpdate();
iDirectories = 0;
try
{
DirectoryInfo diRoot;
// If drive, get directories from drives
if (tnSubNode.SelectedImageIndex < 11)
{
diRoot = new DirectoryInfo(tnSubNode.FullPath + “\\”);
}
// Else, get directories from directories
else
{
diRoot = new DirectoryInfo(tnSubNode.FullPath);
}
DirectoryInfo[] dirs = diRoot.GetDirectories();
// Must clear this first, else the directories will get duplicated in treeview
tnSubNode.Nodes.Clear();
// Add the sub directories to the treeView1
foreach (DirectoryInfo dir in dirs)
{
iDirectories++;
TreeNode subNode = new TreeNode(dir.Name);
subNode.ImageIndex = 11;
subNode.SelectedImageIndex = 12;
tnSubNode.Nodes.Add(subNode);
}
}
// Throw Exception when accessing directory: C:\System Volume Information // do nothing
catch { ; }
treeView1.EndUpdate();
}
private void AddFiles(string strPath)
{
listView1.BeginUpdate();
listView1.Items.Clear();
iFiles = 0;
try
{
DirectoryInfo di = new DirectoryInfo(strPath + “\\”);
FileInfo[] theFiles = di.GetFiles();
foreach (FileInfo theFile in theFiles)
{
iFiles++;
ListViewItem lvItem = new ListViewItem(theFile.Name);
lvItem.SubItems.Add(theFile.Length.ToString());
lvItem.SubItems.Add(theFile.LastWriteTime.ToShortDateString());
lvItem.SubItems.Add(theFile.LastWriteTime.ToShortTimeString());
listView1.Items.Add(lvItem);
}
}
catch (Exception Exc) { MessageBox.Show(form.statusStrip1.Text = Exc.ToString(); }
listView1.EndUpdate();
}
