【问题标题】:Finding a file in Vb.net and UnauthorizedAccessException problems在 Vb.net 中查找文件和 UnauthorizedAccessException 问题
【发布时间】:2011-08-19 15:30:23
【问题描述】:

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

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

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

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

我对 VB.Net 还很陌生,所以请尽量让您的解释保持简单。

谢谢。

【问题讨论】:

  • 该代码将始终对 c:\system 卷信息一探究竟。存储还原点的目录。即使拥有管理员访问权限也不能让您查看它。您必须一次迭代一个目录,Directory.GetDirectories()。
  • 我该怎么做呢?我会用 Directory.GetDirectories() 做什么?

标签: vb.net vb.net-2010


【解决方案1】:

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

    '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 的帮助。

【讨论】:

  • 嗨,Pulsar27,不客气。最好的问候
【解决方案2】:

【讨论】:

  • 我已经修改了链接中的代码,它大部分都可以工作。我现在收到文档和设置的 UnauthorizedAccessException 错误(我使用的是 Windows 7)我知道我可以添加另一个 ElseIf 语句来排除该位置,但我想让程序测试该位置,然后如果访问被拒绝跳过,否则搜索目录。该程序将在 Windows 7 和 Windows XP 上使用,我不想跳过我可能能够在其他版本的 Windows 中访问的目录。
  • 您好,如果您打算使用您可以想到的相同类型的代码来识别并安装它,从而确定是否搜索XP没有问题的目录,而windows7可以给你。问候
  • 我想出了如何调整代码以编程方式搜索并跳过它无法访问的文件。太糟糕了,它不允许我从 C 盘搜索 Program Files 文件夹。
  • 由于我无法编辑上面的评论:我从 Program Files 文件夹开始搜索以解决此问题。我认为这暂时可行。我可能会尝试在我无权访问但可能包含我正在搜索的文件的目录中开始搜索。
  • Windows XP 的后续产品版本已包含访问权限,其中之一和 Windows 7 中的文件夹 ProgramFiles,在 Windows XP 上不存在,您仍然可以摸索他们做什么并从管理员权限开始运行您的应用程序。问候
猜你喜欢
  • 2010-11-02
  • 2021-04-18
  • 1970-01-01
  • 2012-06-28
  • 2014-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-10
相关资源
最近更新 更多