0

ドライブ文字(Cドライブ)でVb.Net 2010のファイルを検索しようとしています。ファイルパスを見つけたら、実行可能ファイルを実行します。これは私がファイルを見つけるために使用しようとしているコードです:

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

これにより、コードがごみ箱(またはアクセスできない他のファイル)を検索しようとしたときにUnauthorizedAccessExceptionがスローされ、インターネットを検索したところ、Try ... Catch ...EndTryを使用するように提案されましたがループを使用しておらず、コードをループとして機能するように変更する方法がわからないため、これは機能しません。ディレクトリを検索する前に、GetAccessControlメソッドを使用してアクセス許可をテストすることが提案されている場所を見てきましたが、現在のコードでどのように使用するかわかりませんでした。

UnauthorizedAccessExceptionが原因で、Convert.ToString(...)をテストできませんでした。このコードまたは残りのコードに問題がある場合は、お知らせください。

私はVB.Netにかなり慣れていないので、説明を簡潔にしてください。

ありがとうございました。

4

2 に答える 2

2

私は最初にこの質問をしましたが、今ではすべてのディレクトリでファイルを検索するコードがあります。同様の問題を抱えている人が私のものを使用できるように、実際のコードを投稿する義務があると感じています。作成したアカウントにログインできないため、別のアカウントを使用しています。このコードを実行するには、4つのリストボックスが必要です。

    '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

このコードが誰かに役立つことを願っています。それは私にとってはうまくいきましたが、バグがあるかもしれません。CarmeloLaMonicaにご協力いただきありがとうございます。

于 2011-09-01T17:56:05.683 に答える
0

このスレッドを見てみてください

http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/255be857-9d1e-4c80-9ae2-5c8b48697943/

よろしく。

于 2011-08-19T15:44:01.697 に答える