如何使用树形视图扫描根目录文件夹和子文件夹? [英] How do I use a treeview to scan root directories folders and subfolders?

查看:99
本文介绍了如何使用树形视图扫描根目录文件夹和子文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何列出计算机上的所有硬盘驱动器,但不知道一旦用户选择了父节点(如"C:\"),如何扫描所选目录的文件夹和子文件夹.有人可以提供解决方案或示例吗?

非常感谢您的时间和专业知识! [注意:我已决定重新发布我的问题,并尝试用更好的措词来表达.]

I am aware of how to list all hard drives on my computer but am unaware of how to scan the selected directories folders and subfolders once the user has selected a parent node such as ("C:\"). Can someone please give a solution or example?

Thank you very much for your time and expertise! [Note: I have decided to repost my question and try to word it better.]

推荐答案

一句话:递归.
如果您还没有遇到一个简单的想法,最好用以下方法解释一下:

递归:请参阅递归"

Google,并仔细阅读其内容.

基本思想是:
In a word: Recursion.
If you haven''t met it before it is a simple idea, that is best explained by:

Recursion: see "Recursion"

Google for it, and read up on what it says.

The basic idea is:
private void button1_Click(object sender, EventArgs e)
    {
    LookAtDir(@"F:\Temp");
    }

private void LookAtDir(string path)
    {
    foreach (string dir in Directory.GetDirectories(path))
        {
        LookAtDir(dir);
        }
    ...Do something with the directory.
    }





Private Sub button1_Click(sender As Object, e As EventArgs)
    LookAtDir("F:\Temp")
End Sub
Private Sub LookAtDir(path As String)
    For Each dir As String In Directory.GetDirectories(path)
        LookAtDir(dir)
    Next
    '...Do something with the directory.
End Sub



[edit]抱歉-我忘了你是VB-OriginalGriff [/edit]



好吧,我已经将其添加到树状视图中,并且看起来一切都很好.我不得不在您的私有子目录LookAtDir中添加try catch语句,但是现在我不确定它在做什么.您能告诉我如何进行遍历吗?文件夹和子文件夹以显示在文本框中扫描的当前文件夹?这样我就可以在操作中看到它了……......."

这可能会变得很复杂:请记住,它将在处理过程中减慢LOTS的速度以显示每个文件夹和子文件夹,并且除非您将处理代码移至后台线程或执行令人讨厌的操作,否则您不会看到屏幕上发生的一切.

要将其移至后台线程,请搜索此站点上的文章,我知道有一些非常好的文章:我认为Luc Pattyn,Pete O''Hanlon和DavyM69将是一个不错的起点,但是它我看了他们已经有一段时间了.

若要进行讨厌的操作,请在每次更新TextBox后调用Application.DoEvents方法.令人讨厌,会降低速度,但可以进行测试.

或者,为了测试输出到控制台的当前路径...
好,再次为我的无知表示歉意,但是您会以写作的方式告诉我做讨厌的事吗?大声笑.........我是新手,您在这里使用过的一些术语我也很喜欢它,如果您能解释指向目录的代码部分....LookAtDir("F:\ Temp").如何使用户能够单击任何根目录或文件夹/子文件夹,以及使它可以进行扫描吗?.....我知道这些不是容易回答的问题,并且知道我可能没有问正确的问题,但是我认为您知道id喜欢与此搭配的地方.....简而言之我想要一个可以显示所有硬盘驱动器以及文件夹/子文件夹的树状视图,然后用户单击所需的根目录,例如C:\或D:\ ect,然后单击开始.....文本框将显示当前目录.文件夹被扫描,直到eof ....".

这样做是一种讨厌的方式,而不是"...对目录执行某些操作".以上:



[edit]Sorry - I forgot you were VB - OriginalGriff[/edit]



"Ok I have added this to my treeview and all seems well. I had to add a try catch statement to your private sub LookAtDir but now I am unsure it is doing anything at all. Can you tell me how to itterate through the folders and subfolders to show the current folder scanned in a textbox? so that i can see this in action.........."

This can get complex: Bear in mind that it will slow things down LOTS to show each of your folders and sub-folders as you process it, and unless you move the processing code to a background thread or do something nasty you won''t see anything happening on screen.

To move it to a background thread, search the articles on this site, I know there are a couple of very good ones: I think Luc Pattyn, Pete O''Hanlon, and DavyM69 will be a good place to start, but it''s been a while since I looked at them.

To do the nasty, call the Application.DoEvents method after every update to your TextBox. Nasty, and will slow things down a lot, but it''ll be ok for testing.

Alternatively, for testing output the current path to the console...
"Ok once again sorry for my ignorance but will you show me in writing what you mean by doing the nasty? lol......... I am new to console and some of the terms you have used here. also I would love it if you could explain the part of your code that points to the dir.... LookAtDir("F:\Temp"). How can I enable the user to click any root dir or folder/subfolder and make it available to scan?.... I know that theese are not easy questions and know that I may not be asking the correct questions but I think you get the idea where id like to go with this..... In short I want a treeview that can show all hard drives and there folders/subfolders. The user then clicks the desired root such as C:\ or D:\ ect and clicks start........ The textbox then shows the current folder being scanned untill eof...."

Doing it the nasty way, instead of the "...Do something with the directory." bit above:

textBox1.Text += dir
Application.DoEvents()



虽然这很讨厌,并且仅用于测试.线程是现实世界中更好的解决方案.

控制台:如果创建控制台应用程序,则可以执行Console.WriteLine打印到屏幕",是吗?它也可以在WinForms应用程序中工作-输出进入调试器的输出"窗格...

例程LookAtDir所需的只是目录的文本路径.因此,如果将其传递给"F:\"("F"驱动器的根目录),它将对其进行扫描并为"F"根驱动器中的每个文件夹使用新路径进行调用.因此,如果您具有以下结构:



It is nasty though, and should only be for testing. Threading is a much better solution for the real world.

Console: If you create a console app, you can do Console.WriteLine to print to the "screen", yes? It works in WinForms apps as well - the output goes to the "output" pane of the debugger...

All the routine LookAtDir needs is the text path to the directory. So if you pass it "F:\" (the root of the ''F'' drive), it will scan it and call itself with a new path for each folder within the root drive of ''F''. So if you have the following structure:

F:\
   Temp
      Folder1
   Perm
      Folder2
      Folder3

,然后将使用以下命令调用LookAtDir:

then LookAtDir will be called with:

LookAtDir("F:\")
LookAtDir("F:\Temp")
LookAtDir("F:\Temp\Folder1")
LookAtDir("F:\Perm")
LookAtDir("F:\Perm\Folder2")
LookAtDir("F:\Perm\Folder3")

因此,您要做的就是将路径传递到要扫描的顶级文件夹,它将处理以下所有文件夹.如果您呼叫

So all you have to do is pass the path to the top level folder you want to scan, and it will handle all the ones below. If you called

LookAtDir("F:\Perm")

,那么它只会处理

then it would just process

LookAtDir("F:\Perm")
LookAtDir("F:\Perm\Folder2")
LookAtDir("F:\Perm\Folder3")

并忽略所有其他内容.


看看System.IO.Directory.GetFiles和System.IO.Directory.GetDirectories方法.进行递归查询所有子文件夹.但是请考虑一下性能差-如果您有一个非常深的嵌套或大文件系统.当我用树形视图创建一个"Explorer"时,我只是从用户选择中为另外两个层"创建了节点-一个节点就足够了,但是这样用户可以查看是否还有更多子文件夹(树形视图随后显示了很小的子文件夹). +号)
Have a look at System.IO.Directory.GetFiles and System.IO.Directory.GetDirectories Methods. Make a recursive function to query all child folders. But think about bad performance - If you have a very deep nested or big filesystem. When I created an "Explorer" with a treeview I just created the nodes for another two "levels" down from the user selection - one would be enough but so the user sees if there are any more child folders (the treeview then shows the little + sign)


这篇关于如何使用树形视图扫描根目录文件夹和子文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆