Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Ok once again I have made some improvments to the code but am stuck when trying to display the current folder/subfolder and file name to my lblmessage.text box. The root and folder that I wish to scan show in the text box when i select it in the treeview but how can I itterate to show what files are currently being scanned?

here is my code as it stands:
VB
Imports System.IO
Public Class form1
    Private Shared Sub FillOneLevel(ByVal root As TreeNode, ByVal path As String)
        Dim di As New DirectoryInfo(path)
        Try
             For Each subFolder As DirectoryInfo In di.GetDirectories()
                Dim node As New TreeNode(subFolder.Name) With {.Tag = subFolder.FullName}
                root.Nodes.Add(node)
            Next subFolder
        Catch ex As Exception
            ' do something
        End Try
    End Sub

    Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim drives() As String = Environment.GetLogicalDrives()
        For Each dr As String In drives
            Dim node As New TreeNode(dr) With {.Tag = dr}
            FillOneLevel(node, dr)
            tv1.Nodes.Add(node)
        Next dr
        tv1.Nodes(0).Expand()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        LookAtDir(lblMessage.Text)
    End Sub
    Private Sub LookAtDir(ByVal path As String)
        Try
            For Each dir As String In Directory.GetDirectories(path)
                LookAtDir(dir)
            Next
            '...Do something with the directory.
        Catch ex As UnauthorizedAccessException
        End Try
    End Sub

    Private Sub tv1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tv1.AfterSelect
        lblMessage.Text = tv1.SelectedNode.Tag
    End Sub
End Class

This code successfuly gathers folders recrusivly but once I click start Nothing new is displayed in the textbox..... This is when id like to see the current dir and folder to the next and so on that is being scanned.... does anyone know what i mean?


thank you in advance and have a nice day!! :)
Posted
Updated 2-Feb-11 20:49pm
v3
Comments
JF2015 3-Feb-11 2:36am    
Edited to add code formatting.

This requires very accurate well-planned technique. Here I can only describe the schema.

1) You don't need to gather file system objects recursively. You need to do one level of directory depth at a time for each tree node representing a directory. In addition, for each sub-directory at a given level you should find out if it is currently empty or not.

2) At a given level of directory structure and tree structure, add a node per file system object. If a current node represent non-empty directory, for this node add exactly one child, representing fake object.

3) You should handle events TreeView.BeforeExpand and TreeView.AfterCollapse.

4) When you're about to expand a tree node representing a non-empty directory (all other node should not have any children), you should remove a fake child and repeat procedure from item (2) for this tree node and a level of directory structure. After expanding is done, the user will see newly obtained directory structure.

5) After you collapse a tree node, clean all its children and add a fake child node again.

Items (4-5) shows virtual behavior of the tree: you only have as many data items as number of currently expanded nodes. Remember, during your operation another process can add/remove files, so there is no sense to keep all data in the tree. Moreover, you may not have enough memory to keep all available files in your tree.

Very advanced feature would be using System.IO.FileSystemWatcher class to track all the changes in directory structure (in you view) which are done by all other processes and reflect relevant changes on you tree view.

Good luck,
—SA
 
Share this answer
 
Comments
Dale 2012 3-Feb-11 3:26am    
I am so lost now that I dont know where to start....... I have been at this for two or three solid weeks and need to move on but cannot because I need this treeview to simply scan all folders/ subfolders and files when the user selects a parent or child node and hits start. I do not wish to add folders or roots to the treeview as this is done. I need to scan the hard drive by using the treeview as a means of selecting a scan directory. I also need the lblmessage to display the current root\folder\file being scanned in real time as you have stated. I am exhausted and frustrated to make this work will you give a completed solution or example by adding to my all ready existing code please? I never thought it would take this much to simply scan my computer with treeview...... please anyone help :(
Sergey Alexandrovich Kryukov 3-Feb-11 22:56pm    
Well, do you think anybody else will volunteer some 3 days (perhaps not weeks, but not hours, it's more than that) as a gift to not very successful student of programming? I could not and don't want it, sorry, I have my own problems.

I already spend good amount of my time; and I think I offered you a good program of action, you can slightly modify it according to your requirements. Instead of asking some sensible question you ask for help again. Did you try to find available solution for a similar problem? You did not even do a good job explaining what exactly you need methodically using correct terminology.

You're not asking about any specific techniques or professional "secret", you just say you cannot complete the task -- you just need the job done, get rid of it. I feel you have any interest in the work itself. But you see, such attitude is the best way to fail.

If you're exhausted, etc., it means you don't have proper skill do judge your own capability yet, so you messed up your own project. Don't you think it's right time to address to your teacher/advisor/supervisor and honestly admit your failure. From what I see, you need a brake and, perhaps a bit easier tasks to get prepared to more difficult.

Sorry, I just trying to tell how I see things.
--SA
See my answer to your previous posting of this question. How do I use a treeview to scan root directories folders and subfolders?[^]

If you want to see what directory / file is being handled in real time, then you have two choices:
1) Move the processing to a background thread.
2) Liberally scatter calls to Application.DoEvents through your code.

Otherwise, it is so busy scanning, then the UI does not get updated.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Feb-11 3:13am    
This is something to think a bit. Only I would say DoEvent is quite bad. Would you agree?
--SA
OriginalGriff 3-Feb-11 3:29am    
Oh yes! I described it as "doing the nasty" and "suitable for testing only" in the previous answer!
Dale 2012 3-Feb-11 3:39am    
How can i move processes to background threads?
Kschuler 3-Feb-11 12:07pm    
Research the BackgroundWorker:
http://www.google.com/#hl=en&sugexp=ldymls&xhr=t&q=vb.net+backgroundworker&cp=9&pf=p&sclient=psy&aq=0&aqi=&aql=&oq=vb.net+ba&pbx=1&fp=a02c97597e71f11

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