在Vb.net中查找文件和UnauthorizedAccessException问题

时间:2011-08-19 15:30:24

标签: vb.net vb.net-2010

我正在尝试在Vb.Net 2010中搜索驱动器号(C驱动器)中的文件。找到文件路径后,我想运行可执行文件。这是我试图用来查找文件的代码:

path = Convert.ToString(IO.Directory.GetFiles("C:\", "wswc.exe", System.IO.SearchOption.AllDirectories))

当我的代码试图搜索回收站(或我无权访问的其他文件)时,会抛出UnauthorizedAccessException,并且我搜索了互联网,并且有人建议使用Try ... Catch ...结束尝试,但这对我不起作用,因为我没有使用循环,我不知道如何更改我的代码作为循环。我已经看到建议在搜索目录之前使用GetAccessControl方法测试权限的地方,但我不确定如何使用它与我当前的代码一起使用。

由于UnauthorizedAccessException,我无法测试Convert.ToString(...)所以如果这个或任何其他代码有问题,请告诉我。

我是VB.Net的新手,所以尽量让你的解释变得简单。

谢谢。

2 个答案:

答案 0 :(得分:2)

我最初问过这个问题,我现在有代码搜索所有目录中的文件。我觉得有义务发布实际的代码,以便有类似问题的人可以使用我的。我正在使用其他帐户,因为我似乎无法登录到我创建的帐户。您需要四个列表框才能运行此代码。

    '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

我希望这段代码对某人有用。它对我有用,但可能存在漏洞。感谢您的帮助Carmelo La Monica。

答案 1 :(得分:0)