Does anyone have a WORKING example that displays a directory tree structure in a TreeView for VB .NET? - vb.net

I have looked everywhere and cannot find a version that works. The ones I found are all either outdated or have errors.
I have something that is working for the most part, but I'm having some trouble with restricted-access folders.
The code I'm using is as follows:
Imports System.IO
Public Class frmMain
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each drive In DriveInfo.GetDrives
Dim i As Integer = TreeView1.Nodes.Count
TreeView1.Nodes.Add(drive.ToString)
If drive.IsReady Then
PopulateTree(drive.ToString, TreeView1.Nodes(i))
End If
Next
End Sub
Private Sub PopulateTree(ByVal sDir As String, ByVal node As TreeNode)
Dim directory As New DirectoryInfo(sDir)
Try
For Each d As DirectoryInfo In directory.GetDirectories
Dim t As New TreeNode(d.Name)
PopulateTree(d.FullName, t)
node.Nodes.Add(t)
Next
Catch excpt As UnauthorizedAccessException
Debug.WriteLine(excpt.Message)
End Try
End Sub
End Class
For testing purposes I replaced this section...
If drive.IsReady Then
PopulateTree(drive.ToString, TreeView1.Nodes(i))
End If
...with this...
If drive.toString = "L:\"
PopulateTree(drive.ToString, TreeView1.Nodes(i))
End If
...and it worked fine for that drive. The L:\ is a removable USB drive by the way.
However, with the original code I get debug errors on some folders because they are access-restricted. Is there any way to ignore those particular folders and show the rest?

Yes, you need to tighten the scope of your try catch block. You are catching the error too far away from where it occurs. Try this:
Private Sub PopulateTree(ByVal sDir As String, ByVal node As TreeNode)
Dim directory As New DirectoryInfo(sDir)
For Each d As DirectoryInfo In directory.GetDirectories
Dim t As New TreeNode(d.Name)
Try
PopulateTree(d.FullName, t)
node.Nodes.Add(t)
Catch excpt As UnauthorizedAccessException
Debug.WriteLine(excpt.Message)
EndTry
Next
End Sub

Related

Display all drives, files, and subfolders in treeview

I'm looking for the simplest way to display all drives, files, and subfolders in a treeview. If someone's got a snippet of code to do this that they don't mind sharing I would really appreciate it.
The closest I've gotten was this code I tried using, but it gave me a "IOException was unhandled" error saying "The device is not ready." error at runtime (after about 5-10 sec) on the line below
Dim folders() As String = IO.Directory.GetDirectories(dir)
underneath is the rest of the code
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim drives As System.Collections.ObjectModel.ReadOnlyCollection(Of IO.DriveInfo) = My.Computer.FileSystem.Drives
Dim rootDir As String = String.Empty
For i As Integer = 0 To drives.Count - 1
rootDir = drives(i).Name
TreeView1.Nodes.Add(rootDir)
PopulateTreeView(rootDir, TreeView1.Nodes(i))
Next
End Sub
Private Sub PopulateTreeView(ByVal dir As String, ByVal parentNode As TreeNode)
Dim folder As String = String.Empty
Try
Dim folders() As String = IO.Directory.GetDirectories(dir)
If folders.Length <> 0 Then
Dim childNode As TreeNode = Nothing
For Each folder In folders
childNode = New TreeNode(folder)
parentNode.Nodes.Add(childNode)
PopulateTreeView(folder, childNode)
Next
End If
Catch ex As UnauthorizedAccessException
parentNode.Nodes.Add(folder & ": Access Denied")
End Try
End Sub
Seems like you're off to a good start. The IOException you receive is most likely caused by your procedure trying to list contents on an empty disc drive, which is obviously impossible.
The fix is simple:
For i As Integer = 0 To drives.Count - 1
If Not drives(i).IsReady Then
Continue For
End If
rootDir = drives(i).Name
TreeView1.Nodes.Add(rootDir)
PopulateTreeView(rootDir, TreeView1.Nodes(i))
Next
Besides that, I recommend not loading folder contents until a node is clicked. Limit the recursive call to 1 level (current directory + content of all its subdirectories). That way, you get the best performance while still being able to determine whether a subdirectory should have the treeview expand button.

Searching a drive with vb.net

I am making a program that searches your drive for an executable.
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim di As New DirectoryInfo("C:\")
Dim files() As FileInfo
Dim a As Integer
Do While a = 0
Try
files = di.GetFiles("FileName.exe", SearchOption.AllDirectories)
Catch ex As UnauthorizedAccessException
End Try
If Not files Is Nothing Then
a = 1
End If
Loop
txt_Location.Text = files(0).FullName
End Sub
As soon as it hits the first UnauthorizedAccessException, it gets stuck in an infinite loop. How do I skip over the files that produce the exception?
EDIT:
This is what I have now:
Public Sub DirSearch(ByVal sDir As String)
Dim dir As String
Try
For Each dir In Directory.GetDirectories(sDir)
For Each file In Directory.GetFiles(dir, "Filename.exe")
ComboBox1.Items.Add(file)
Next
DirSearch(dir)
Next
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
DirSearch("C:\")
End Sub
You need recursion here which handles each folder.
EDIT: As requested by the OP, a little example:
Public Sub DirSearch(ByVal sDir As String)
Try
For Each dir as String In Directory.GetDirectories(sDir)
For Each file In Directory.GetFiles(dir, "yourfilename.exe")
lstFilesFound.Items.Add(file)
Next
DirSearch(dir)
Next
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
End Sub
Also take a look at the following answers:
Looping through all directory's on the hard drive (VB.NET)
How to handle UnauthorizedAccessException when attempting to add files from location without permissions (C#)
Also note, if you have enough access rights, you could simplify your code to this:
Dim di as New DirectoryInfo()
Dim files = di.GetFiles("iexplore.exe", SearchOption.AllDirectories) 'already returns all files at once
And at last but not least:
Avoid having infinite loops. Like in your example, they can lead to broken code just because some circumstances aren't like you've expected them to be. Imagine your code has run on your PC and you deployed this software to a customer - horrible scenario. ;-)

How to correctly enumerate files in selected path?

Visual Studio 2008 (vb.net)
I made simple anivirus but when I make Full scan by this code:
FolderBrowserDialog1.SelectedPath = ("C:\")
'first scan:************************************
Try
For Each strDir As String In
System.IO.Directory.GetDirectories(FolderBrowserDialog1.SelectedPath)
For Each strFile As String In System.IO.Directory.GetFiles(strDir)
ListBox1.Items.Add(strFile)
Next
Next
'Start the timer:
Catch ex As Exception
End Try
Timer1.Start()`
Just scan the first 6 files ...
I think the problem from Windows Folder permissions (Windows - Program Files ...etc)
So how to fix it?
Put a Console.WriteLine(ex) in your catch block so you can see any exceptions that are thrown. You'll probably see your problem then. Most likely permissions.
You could try the following:
For Each strFile As String In System.IO.Directory.GetFiles(strDir, "*", IO.SearchOption.AllDirectories)
Edit:
You could try the last solution found in this thread:
http://www.vbforums.com/showthread.php?t=624969
I tried this myself and it was super slow, but worked fine.
Public Class Form1
Private Sub foo(ByVal aDir As String)
Try
Dim di As New IO.DirectoryInfo(aDir)
Dim aryFiles() As IO.FileInfo = di.GetFiles("*.*")
Dim aryDirs() As IO.DirectoryInfo = di.GetDirectories()
For Each fi As IO.FileInfo In aryFiles
rslts.Add(fi.FullName)
Next
For Each d As IO.DirectoryInfo In aryDirs
foo(d.FullName)
Next
Catch ex As Exception
'Stop 'the catch should be more specific
End Try
End Sub
Dim rslts As List(Of String)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
rslts = New List(Of String)
foo("C:\")
ListBox1.Items.Clear()
ListBox1.Items.AddRange(rslts.ToArray)
End Sub
End Class
It looks like your solution essentially loops through the first folder it can find and stops there. This solution is a bit different as it will recursively go through all the files and folders based on the start location.

VB 2010 mkdir read only

Hi i cant seem to get mkdir to create a folder which isnt read only, this is causing alot of problems in my code because i am unable to write files to the directory i have created. thanks for any help. this is my code below:
Else
MessageBox.Show("Please set a Root Path for your ****")
RootFBD.ShowDialog()
TextBox1.Text = RootFBD.SelectedPath
My.Computer.FileSystem.CreateDirectory("C:\****-Tools\config\root.txt")
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f2 As New FileIOPermission(FileIOPermissionAccess.Read, TextBox1.Text)
f2.AddPathList(FileIOPermissionAccess.Write Or FileIOPermissionAccess.Read, TextBox1.Text)
Dim rootSave As System.IO.StreamWriter
rootSave = My.Computer.FileSystem.OpenTextFileWriter("C:\****-Tools\config\root.txt", True)
rootSave.WriteLine(TextBox1.Text)
Me.Hide()
MainTool.Show()
End Sub
End Class
Thanks again josh
You're misunderstanding the problem; this isn't a permission issue.
Rather, you're leaving the file open, which prevents other processes from writing to ir.
You just need to Close() your StreamWriter.
Or, you can just call File.AppendText, which will avoid the issue.
You are creating the directory with the file name. Try this:
My.Computer.FileSystem.CreateDirectory("C:\****-Tools\config")

Pressing a button in visual basic

I am new to Visual Basic.NET and I am just playing around with it. I have a book that tells me how to read from a file but not how to write to the file with a button click. All I have is a button and a textbox named fullNameBox. When I click the button it gives me an unhandled exception error. Here is my code:
Public Class Form1
Sub outputFile()
Dim oWrite As System.IO.StreamWriter
oWrite = System.IO.File.CreateText("C:\sample.txt")
oWrite.WriteLine(fullNameBox.Text)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
outputFile()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Have you tried stepping through your application to see where the error is? With a quick glance, it looks like you might need to use System.IO.File on the fourth line (oWrite = IO.File...) instead of just IO, but I haven't tried to run it.
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SaveFileDialog1.FileName = ""
SaveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
SaveFileDialog1.ShowDialog()
If SaveFileDialog1.FileName.Trim.Length <> 0 Then
Dim fs As New FileStream(SaveFileDialog1.FileName.Trim, FileMode.Create)
Dim sr As New StreamWriter(fs)
sr.Write(TextBox1.Text)
fs.Flush()
sr.Close()
fs.Close()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
OpenFileDialog1.ShowDialog()
If OpenFileDialog1.FileName.Trim.Length <> 0 Then
Dim fs As New FileStream(OpenFileDialog1.FileName.Trim, FileMode.Open)
Dim sw As New StreamReader(fs)
TextBox1.Text = sw.ReadToEnd
fs.Flush()
sw.Close()
fs.Close()
End If
End Sub
End Class
this is a complete functional program if you want, you just need to drag drop a textbox, openfiledialog, and a savefiledialog.
feel free to play around with the code.
enjoy
by the way, the problem in your code is that you "must" close filestream when your done using it, doing so will release any resource such as sockets and file handles.
The .net framework is a very powerful framework. In the same way (however) it has easy and convenient methods for simple tasks. Most individuals tend to complicate things in order to display knowledge. But less code = less processing = faster and more efficient application (sometimes) so the large above method may not be suitable. Along with that, the above mentioned method would be better off written as a sub or if returning something then a function.
My.Computer.FileSystem.WriteAllText("File As String", "TextAsString", Append as Boolean)
A general Example would be
My.Computer.FileSystem.WriteAllText("C:\text.text", "this is what I would like to add", False)
this is what I would like to add
can be changed to the current text of a field as well.
so a more specific example would be
My.Computer.FileSystem.WriteAllText("C:\text.text", fullNameBox.text, True)
If you would like to understand the append part of the code
By setting append = true you are allowing your application to write the text at the end of file, leaving the rest of the text already in the file intact.
By setting append = false you will be removing and replacing all the text in the existing file with the new text
If you don't feel like writing that part of the code (though it is small) you could create a sub to handle it, however that method would be slightly different, just for etiquette. functionality would remain similar. (Using StreamWriter)
Private Sub WriteText()
Dim objWriter As New System.IO.StreamWriter("file.txt", append as boolean)
objWriter.WriteLine(textboxname.Text)
objWriter.Close()
End Sub
The Specific Example would be
Private Sub WriteText()
Dim objWriter As New System.IO.StreamWriter("file.txt", False)
objWriter.WriteLine(fullnamebox.Text)
objWriter.Close()
End Sub
then under the button_click event call:
writetext()
You can take this a step further as well. If you would like to create a more advabced Sub to handle any textbox and file.
Lets say you plan on having multiple separate files and multiple fields for each file (though there is a MUCH cleaner more elegant method) you could create a function. {i'll explain the concept behind the function as thoroughly as possible for this example}
below is a more advanced sub demonstration for your above request
Private Sub WriteText(Filename As String, app As Boolean, text As String)
Dim objWriter As New System.IO.StreamWriter(Filename, app)
objWriter.WriteLine(text)
objWriter.Close()
End Sub
What this does is allows us to (on the same form - if you need it global we can discuss that another time, it's not much more complex at all) call the function and input the information as needed.
Sub Use -> General Sample
WriteText(Filename As String, app As Boolean)
Sub Use -> Specific Sample
WriteText("C:\text.txt, False, fullnamebox.text)
But the best part about this method is you can change that to be anything as you need it.
Let's say you have Two Buttons* and **Two Boxes you can have the button_event for the first button trigger the above code and the second button trigger a different code.
Example
WriteText("C:\text2.txt, False, halfnamebox.text)
The best part about creating your own functions and subs are Control I won't get into it, because it will be off topic, but you could check to be sure the textbox has text first before writing the file. This will protect the files integrity.
Hope this helps!
Richard Sites.