I am trying to search a drive letter (C drive) for a file in Vb.Net 2010. After I find the file path I want to run the executable. This is the code I am trying to use to find the file:
path = Convert.ToString(IO.Directory.GetFiles("C:\", "wswc.exe", System.IO.SearchOption.AllDirectories))
This throws an UnauthorizedAccessException when my code tries to search a recycle bin (or some other file that I don't have access to) and I have searched the开发者_Python百科 internet and people have suggested to use Try...Catch...End Try but this will not work for me since I am not using a loop and I don't know how to change my code to function as a loop. I have seen where is was suggested to use the GetAccessControl method to test for permissions before searching the directory but I was not sure how use use it with my current code.
I have not been able to test the Convert.ToString(...) because of the UnauthorizedAccessException so if there is something wrong with this or any of the rest of the code please let me know.
I am fairly new to VB.Net so try to keep your explanation simple.
Thank you.
I originally asked this question and I have code that now searches all of the directories for a file. I feel obligated to post the actual code so that someone with a similar problem can use mine. I'm using a different account because I can't seem to log in to the one I created. You need four listboxes to run this code.
'Run the file if found
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim path As String
'Search for a specified file
Start_Search(ListBox1)
For k = 0 To ListBox2.Items.Count - 1
Try
ListBox2.SelectedIndex = k
path = ListBox2.SelectedItem.ToString
System.Diagnostics.Process.Start(path)
Catch ex As Exception
End Try
Next
Quit()
End Sub
'Set the root of your search
Private Sub Start_Search(ByVal listbox1 As ListBox)
Dim strroot As String
strroot = "C:\"
listbox1.Items.Add(strroot)
Search(listbox1, ListBox2)
End Sub
'Search all directories and sub-directories in the search root(s)
Private Sub Search(ByVal listbox1 As ListBox, ByVal listbox2 As ListBox)
Dim listbox4 As New ListBox
'Get all sub-directories of all items in your search root(s) (listbox1),
'clear listbox1, copy all sub-directories into listbox1
For j = 0 To listbox1.Items.Count - 1
listbox1.SelectedIndex = j
Try
For Each strfolder As String In My.Computer.FileSystem.GetDirectories(listbox1.SelectedItem.ToString)
listbox4.Items.Add(strfolder)
Dim junk = listbox4.Items.Count - 1
Next
Catch ex As Exception
End Try
Next
listbox1.Items.Clear()
listbox1 = listbox4
'every directory that throws an UnauthorizedAccessException is
'placed into listbox3. Then there is a recursive call on listbox3
'
For i = 0 To listbox1.Items.Count - 1
Try
listbox1.SelectedIndex = i
'You can place the file you are looking for in this line
listbox2.Items.AddRange(System.IO.Directory.GetFiles(listbox1.SelectedItem.ToString & "\", "File to Find.exe", System.IO.SearchOption.AllDirectories))
Catch ex As UnauthorizedAccessException
ListBox3.Items.Add(listbox1.SelectedItem.ToString)
Catch ex1 As Exception
End Try
Next
If listbox2.Items.Count > 0 Then
Return
ElseIf ListBox3.Items.Count >= 0 Then
Search(ListBox3, listbox2)
End If
Return
End Sub
I hope this code can be of use to someone. It worked for me but there may be bugs in it. Thank you for your help Carmelo La Monica.
try to see this thread
http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/255be857-9d1e-4c80-9ae2-5c8b48697943/
Regards.
精彩评论