|
-
Mar 8th, 2007, 01:54 AM
#1
[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
-
Mar 8th, 2007, 02:01 AM
#2
Re: [2005] Is Directory
An entry in what exactly?
-
Mar 8th, 2007, 02:04 AM
#3
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
-
Mar 8th, 2007, 02:12 AM
#4
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.
-
Mar 8th, 2007, 02:32 AM
#5
Re: [2005] Is Directory
Dear JMC,
I trying with this one.But not working for subFolders and Files
vb Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim Folders As TreeNode
Dim FilePath As String
Dim FolderPath As String
Dim FilesinFolder As String
For Each FolderPath In Directory.GetDirectories("C:\Inetpub", "*")
Folders = TreeView1.Nodes.Add(Path.GetFileName(FolderPath))
For Each FilePath In Directory.GetFiles(FolderPath)
FilesinFolder = Path.GetFileName(FilePath).ToString
Folders.Nodes.Add(FilesinFolder)
'If New DirectoryInfo(FilePath).Exists = True Then
' For Each SubFolder In Directory.GetFileSystemEntries(FilePath)
' FolderNode.Nodes.Add(Path.GetFileName(SubFolder))
' Next
' End If
Next
Next
End Sub
Please mark you thread resolved using the Thread Tools as shown
-
Mar 8th, 2007, 05:15 PM
#6
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:
Private Sub PopulateFolderTree(ByVal rootFolderPath As String, _
ByVal nodes As TreeNodeCollection)
Try
'Add a node for each subfolder.
For Each subfolderPath As String In IO.Directory.GetDirectories(rootFolderPath)
'Add the node and recursively popluate it with children.
Me.PopulateFolderTree(subfolderPath, _
nodes.Add(IO.Path.GetFileName(subfolderPath)).Nodes)
Next subfolderPath
Catch ex As Exception
'You'll end up here if the folder is inaccessible. Just log and continue.
Debug.WriteLine(ex.ToString())
End Try
'Add a node for each file.
For Each filePath As String In IO.Directory.GetFiles(rootFolderPath)
nodes.Add(IO.Path.GetFileName(filePath))
Next filePath
End Sub
Note that TreeNodeCollection.Add returns a reference to the TreeNode added so this:
vb Code:
'Add the node and recursively popluate it with children.
Me.PopulateFolderTree(subfolderPath, _
nodes.Add(IO.Path.GetFileName(subfolderPath)).Nodes
is equivalent to this:
vb Code:
'Add the node...
Dim node As TreeNode = nodes.Add(IO.Path.GetFileName(subfolderPath))
'...and recursively popluate it with children.
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:
Me.PopulateFolderTree("C:\", Me.TreeView1.Nodes.Add("C:").Nodes)
-
Mar 8th, 2007, 11:04 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|