Results 1 to 7 of 7

Thread: [RESOLVED] [2005] Is Directory

  1. #1

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Resolved [RESOLVED] [2005] Is Directory

    Dear All,
    Is there any method avl to check wheather a entry is File or Directory

    Dana
    Please mark you thread resolved using the Thread Tools as shown

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Is Directory

    An entry in what exactly?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: [2005] Is Directory

    Dear JMC
    I am trying to populate the treeview with files and folders.For that if the file entry is Directory then I want to list the files also

    Dana
    Please mark you thread resolved using the Thread Tools as shown

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Is Directory

    You get folders by calling Directory.GetDirectories and you get files by calling Directory.GetFiles. If you're saying (without actually saying) that you're calling Directory.GetFileSystemEntries then there is no way to tell one from the other because all you get back are Strings. You'd have to call File.Exists or Directory.Exists on each one to tell them apart, which is unnecessary.

    You could also call DirectoryInfo.GetFileSystemInfos, then you could test the type of each one for either FileInfo or DirectoryInfo. That's also less efficient though, if all you're using is the name of each one. Just make the two separate calls and then there's no need to distinguish.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: [2005] Is Directory

    Dear JMC,
    I trying with this one.But not working for subFolders and Files
    vb Code:
    1. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    2.         Dim Folders As TreeNode
    3.         Dim FilePath As String
    4.         Dim FolderPath As String
    5.         Dim FilesinFolder As String
    6.         For Each FolderPath In Directory.GetDirectories("C:\Inetpub", "*")
    7.             Folders = TreeView1.Nodes.Add(Path.GetFileName(FolderPath))
    8.             For Each FilePath In Directory.GetFiles(FolderPath)
    9.                 FilesinFolder = Path.GetFileName(FilePath).ToString
    10.                 Folders.Nodes.Add(FilesinFolder)
    11.                 'If New DirectoryInfo(FilePath).Exists = True Then
    12.                 '    For Each SubFolder In Directory.GetFileSystemEntries(FilePath)
    13.                 '        FolderNode.Nodes.Add(Path.GetFileName(SubFolder))
    14.                 '    Next
    15.                 ' End If
    16.             Next
    17.  
    18.         Next
    19.     End Sub
    Please mark you thread resolved using the Thread Tools as shown

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Is Directory

    That's only going to go one level deep. A tree is a recursive structure so to traverse a tree the most logical way is with recursion:
    vb Code:
    1. Private Sub PopulateFolderTree(ByVal rootFolderPath As String, _
    2.                                ByVal nodes As TreeNodeCollection)
    3.     Try
    4.         'Add a node for each subfolder.
    5.         For Each subfolderPath As String In IO.Directory.GetDirectories(rootFolderPath)
    6.             'Add the node and recursively popluate it with children.
    7.             Me.PopulateFolderTree(subfolderPath, _
    8.                                   nodes.Add(IO.Path.GetFileName(subfolderPath)).Nodes)
    9.         Next subfolderPath
    10.     Catch ex As Exception
    11.         'You'll end up here if the folder is inaccessible.  Just log and continue.
    12.         Debug.WriteLine(ex.ToString())
    13.     End Try
    14.  
    15.     'Add a node for each file.
    16.     For Each filePath As String In IO.Directory.GetFiles(rootFolderPath)
    17.         nodes.Add(IO.Path.GetFileName(filePath))
    18.     Next filePath
    19. End Sub
    Note that TreeNodeCollection.Add returns a reference to the TreeNode added so this:
    vb Code:
    1. 'Add the node and recursively popluate it with children.
    2. Me.PopulateFolderTree(subfolderPath, _
    3.                       nodes.Add(IO.Path.GetFileName(subfolderPath)).Nodes
    is equivalent to this:
    vb Code:
    1. 'Add the node...
    2. Dim node As TreeNode = nodes.Add(IO.Path.GetFileName(subfolderPath))
    3.  
    4. '...and recursively popluate it with children.
    5. Me.PopulateFolderTree(subfolderPath, node.Nodes)
    If you wanted to populate a tree with the entire (accessible) contents of your C drive you'd do this:
    vb Code:
    1. Me.PopulateFolderTree("C:\", Me.TreeView1.Nodes.Add("C:").Nodes)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: [2005] Is Directory

    Thanks JMC. Good Example with code.

    Dana
    Please mark you thread resolved using the Thread Tools as shown

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width