Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friends,

i have a treeview control in which i am list all drive.....on selecting a drive i have to show all of its directories and files in a listview.....i am using following code..

C#
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
       {
           //string[] omit = { ".doc", ".mp3", ".cs" };
           TreeNode newSelected = e.Node;
           listView1.Items.Clear();
           DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
           ListViewItem.ListViewSubItem[] subItems;
           ListViewItem item = null;

           foreach (DirectoryInfo dir in nodeDirInfo.GetDirectories())
           {
               item = new ListViewItem(dir.Name, 0);
               subItems = new ListViewItem.ListViewSubItem[]
                   {new ListViewItem.ListViewSubItem(item, "Directory"),
                    new ListViewItem.ListViewSubItem(item,
                       dir.LastAccessTime.ToShortDateString())};
               item.SubItems.AddRange(subItems);
               //      checkedListBox1.Items.Add(item);
               listView1.Items.Add(item);
           }



when it comes to DirectoryInfo nodeDirInfo=(DirectoryInfo)newSelected.Tag...line...it loads null value....
because it select C:\\ form e.node property....
how to solve this error....so it can select C:\..??
Posted
Comments
toATwork 14-May-13 2:29am    
Are you sure that you have assigned a DirectoryInfo object to the Tag of the "C:\" node? Otherwise the tag would not be null.
abhishekagrwl25 14-May-13 2:34am    
i am using this code to populate the treeview

System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();

foreach (System.IO.DriveInfo drive in drives)
{
if (drive.DriveType == System.IO.DriveType.CDRom)
{
continue;
}
else
{
TreeNode mn = new TreeNode(drive.Name);

treeView1.Nodes.Add(mn);
string str = mn.FullPath;
DirectoryInfo info1 = new DirectoryInfo(@str);
GetDirectories(info1.GetDirectories(), mn);
}
}
abhishekagrwl25 14-May-13 2:33am    
e.node property returns the value "C:\\" not "C:\"...this is the exact problem...what to do so it can select "C:\" only..??
toATwork 14-May-13 2:45am    
No, I do not think that this is your problem. If I see your code above, you never assign the Tag of the new node!

For populating the drives, assign the DirectoryInfo to the Tag property of the new TreeNode:

C#
foreach (System.IO.DriveInfo drive in drives)
{
 if (drive.DriveType == System.IO.DriveType.CDRom)
 {
  continue;
 }
 else
 {
  TreeNode mn = new TreeNode(drive.Name);
  treeView1.Nodes.Add(mn);
  string str = mn.FullPath;
  DirectoryInfo info1 = new DirectoryInfo(@str);  // I guess the '@' has to (can) be removed
  mn.Tag = info1;  // this has to be added!
  GetDirectories(info1.GetDirectories(), mn);
 }
}
 
Share this answer
 
v2
Comments
toATwork 14-May-13 3:07am    
Why "just" a vote of "3" for a correct solution?
abhishekagrwl25 14-May-13 5:00am    
now how to save the path of checked item of listview in a string array on button click...how to do it..??
toATwork 14-May-13 5:11am    
I would rather use a list of strings. When the button is clicked, iterate recursive over all nodes. If it is checked and then add the path of the DirectoryInfo to your list.
abhishekagrwl25 14-May-13 5:19am    
how to check for that item is checked...while listviewitem doesnt have item.checked property..?\
toATwork 14-May-13 5:24am    
So you are looking for the selected items? Then use ListView.SelectedItems.
Or have you set the ListView.CheckBoxes property to true.
You are not providing sufficient information.
You should rather use System.IO.Directory.GetDirectories:
http://msdn.microsoft.com/en-us/library/c1sez4sc.aspx[^],
http://msdn.microsoft.com/en-us/library/6ff71z1w.aspx[^],
http://msdn.microsoft.com/en-us/library/ms143314.aspx[^].

Also, you should better be aware of one unpleasant Microsoft problem. Please see my past answer: Directory.Get.Files search pattern problem[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900