.NET Framework Developer Center >
Windows Forms Forums
>
Windows Forms General
>
C#: How to add text (ex creation time) to my folders in my treeview.
C#: How to add text (ex creation time) to my folders in my treeview.
- Hello
I am making an application where you in a form have a treeview of folders. If you click on one of the folders in the treeview you get directed to a new form with a new treeview containing the files in the chosen folder. Please forgive my inexperienced programming. We all have to start somewhere ;)
This is code from my first form.
DirectoryInfo di = new DirectoryInfo("c:/ProjektTest/");
DirectoryInfo[] folders = di.GetDirectories();
ArrayList folderArray = new ArrayList();
foreach (DirectoryInfo d in folders)
{
TreeNode aFolder = new TreeNode(d.ToString());
aFolder.Tag = folderArray;
folderArray.Add(new ListViewItem(aFolder.Text));
treeView1.Nodes.Add(aFolder);
}
Now my folders are listed. Next I need to list my files (*.smp) in the treeview on form 2 (myFiles).
folderPath = new DirectoryInfo("c:/ProjektTest/" + treeView1.SelectedNode.FullPath.ToString());
FileInfo[] rgFile = folderPath.GetFiles("*.smp");
foreach (FileInfo f in rgFile)
{
folderAndNodePath = new DirectoryInfo(folderPath.ToString());
ArrayList fileArray = new ArrayList();
aFile = new TreeNode(f.ToString());
aFile.Tag = fileArray;
fileArray.Add(new ListViewItem(aFile.Text));
myFiles.treeView2.Nodes.Add(aFile);
}
If I change the text in my treeview - add creation time or something like that - the application will crash since the given path is now wrong and it is used in a method call to read the contant of the files. The added text will also be added to the path - hence the error.
I've tried setting aFile.Name and afterwards using this when setting the path. But if I put this in my foreach statement the name will always be the last file in my folder.
I am not sure how to work around this. Can you help me out?
Thank you!- Moved byeryangMSFTThursday, November 05, 2009 7:45 AMwrong forum (From:.NET Base Class Library)
Answers
- It seems I handled my problem. I've saved all file names in an ArrayList. After this I can alter the text in my nodes. When i then need a path for the chosen file I simply get the file name in my ArrayList.
FileNames[treeView1.SelectedNode.Index].ToString()
I dont know if there is a better way to do it, but this worls - for now :P
Thank you for your time.- Marked As Answer byMBN84 Thursday, November 05, 2009 2:04 PM
All Replies
- It seems I handled my problem. I've saved all file names in an ArrayList. After this I can alter the text in my nodes. When i then need a path for the chosen file I simply get the file name in my ArrayList.
FileNames[treeView1.SelectedNode.Index].ToString()
I dont know if there is a better way to do it, but this worls - for now :P
Thank you for your time.- Marked As Answer byMBN84 Thursday, November 05, 2009 2:04 PM


