如何在列表视图中查看驱动器的所有目录 [英] how to view all directories of drive ina listview
本文介绍了如何在列表视图中查看驱动器的所有目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好朋友,
i有一个treeview控件,我列出所有驱动器.....在选择驱动器时我必须显示其所有目录和列表视图中的文件.....我正在使用以下代码..
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..
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);
}
当谈到 DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag ... line ...它加载空值....
因为它选择C:\\形式e.node属性... 。
如何解决这个错误....所以它可以选择C:\ .. ??
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:\..??
推荐答案
用于填充驱动器,将DirectoryInfo分配给新TreeNode的Tag属性:
For populating the drives, assign the DirectoryInfo to the Tag property of the new TreeNode:
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);
}
}
你应该使用System.IO.Directory.GetDirectories
:
http://msdn.microsoft.com /en-us/library/c1sez4sc.aspx [ ^ ],
http://msdn.microsoft .com / zh-CN / library / 6ff71z1w.aspx [ ^ ],
http:// msdn .microsoft.com / zh-CN / library / ms143314.aspx [ ^ ]。
此外,您应该更好地了解一个令人不快的Microsoft问题。请参阅我以前的回答: Directory.Get.Files搜索模式问题 [ ^ ]。
-SA
You should rather useSystem.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
这篇关于如何在列表视图中查看驱动器的所有目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文