Jump to content

So I am making a program with Visual Basic that searches you C drive for a specific file, but when I start the search the program crashes. Here is the code:

 

    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

 

Any help is welcomed.

Link to comment
https://linustechtips.com/topic/41599-vbnet-crashing-issues/
Share on other sites

Link to post
Share on other sites

Are you considering symfilelinks that can cause a loop?  (yes windows does have them)

 

 

Also, you are calling a specific inferred class, do you have the system class UnauthorisedAccessException is in?

 

I'm not sure what it is as I'm on a phone and can't look it up, just making sure.

01110100 01101000 01100101 00100000 01110001 01110101 01101001 01100101 01110100 01100101 01110010 00100000 01111001 01101111 01110101 00100000 01100010 01100101 01100011 01101111 01101101 01100101 00101100 00100000 01110100 01101000 01100101 00100000 01101101 01101111 01110010 01100101 00100000 01111001 01101111 01110101 00100000 01100001 01110010 01100101 00100000 01100001 01100010 01101100 01100101 00100000 01110100 01101111 00100000 01101000 01100101 01100001 01110010

 

Link to comment
https://linustechtips.com/topic/41599-vbnet-crashing-issues/#findComment-540204
Share on other sites

Link to post
Share on other sites

Are you considering symfilelinks that can cause a loop?  (yes windows does have them)

 

 

Also, you are calling a specific inferred class, do you have the system class UnauthorisedAccessException is in?

 

I'm not sure what it is as I'm on a phone and can't look it up, just making sure.

It's not a problem with symfilelinks with the loop, and I have the class for UnauthorisedAccessException.

 

If I narrow the search field down to a few files for testing it works but when I search the whole drive it crashed.

Link to comment
https://linustechtips.com/topic/41599-vbnet-crashing-issues/#findComment-540276
Share on other sites

Link to post
Share on other sites

If the file you are searching for cannot be found then your code will run in an infinite loop.

 

The Do Loop is not neccesary, DirectoryInfo.GetFiles using the option of AllDirectories will search every folder on the C: drive for the file, ordinarily if the file can't be found then it will return a zero length array and "a" will be set to 1 and the loop will exit.

 

However if it hits an area on the disk that causes an UnauthorizedAccessException then "files" will be left as Nothing which will mean you get stuck in an infinite loop.

Current Rig (Ongoing Build)


Spec:- 4770K | GTX 780 | 32Gb 2133Mhz Vengeance Pro | CaseLabs TH10 | 2 x 840 Pro RAID 0 | 3 x 3Tb WD Red RAID 5 | Maximus VI Formula | LSI MegaRAID 9271


Cooling (Ongoing Build) :- EK CSQ Clean | EK FC Titan | 3 x BlackIce SR-1 480mm | NB eLoop Fans | Aquaero 5 XT | Dual D5 | Aqualis XT Res

Link to comment
https://linustechtips.com/topic/41599-vbnet-crashing-issues/#findComment-543911
Share on other sites

Link to post
Share on other sites

If the file you are searching for cannot be found then your code will run in an infinite loop.

The Do Loop is not neccesary, DirectoryInfo.GetFiles using the option of AllDirectories will search every folder on the C: drive for the file, ordinarily if the file can't be found then it will return a zero length array and "a" will be set to 1 and the loop will exit.

However if it hits an area on the disk that causes an UnauthorizedAccessException then "files" will be left as Nothing which will mean you get stuck in an infinite loop.

Ok, I can take out the loop, but when I run the program it finds the first UnauthorizedAccessException in the first folder it searches and then stops. So I can't check beyond %Recycle Bin%. Is there any way I could get around this?

Link to comment
https://linustechtips.com/topic/41599-vbnet-crashing-issues/#findComment-548165
Share on other sites

Link to post
Share on other sites

Ok, I can take out the loop, but when I run the program it finds the first UnauthorizedAccessException in the first folder it searches and then stops. So I can't check beyond %Recycle Bin%. Is there any way I could get around this?

 

 Try this.
Public Sub FindFile(ByVal Pattern As String, StartFolder As String)        Dim Results As New Generic.List(Of IO.FileInfo)        Dim StartFolderDirInfo As New IO.DirectoryInfo(StartFolder)        SearchDirectory(Pattern, StartFolderDirInfo, Results)        'Results will contain all matched files    End Sub    Public Sub SearchDirectory(ByVal Pattern As String, SearchFolder As IO.DirectoryInfo, MatchedFilesCollection As Generic.List(Of IO.FileInfo))        Dim Files() As IO.FileInfo = Nothing        Try            Files = SearchFolder.GetFiles(Pattern)        Catch ex As Exception            'Assume a read error of some kind, probably permissions related        End Try        If Files IsNot Nothing AndAlso Not Files.Length = 0 Then            For Each File As IO.FileInfo In Files                MatchedFilesCollection.Add(File)            Next        End If        Dim Directories() As IO.DirectoryInfo = SearchFolder.GetDirectories()        For Each Directory As IO.DirectoryInfo In Directories            SearchDirectory(Pattern, Directory, MatchedFilesCollection)        Next    End Sub

Current Rig (Ongoing Build)


Spec:- 4770K | GTX 780 | 32Gb 2133Mhz Vengeance Pro | CaseLabs TH10 | 2 x 840 Pro RAID 0 | 3 x 3Tb WD Red RAID 5 | Maximus VI Formula | LSI MegaRAID 9271


Cooling (Ongoing Build) :- EK CSQ Clean | EK FC Titan | 3 x BlackIce SR-1 480mm | NB eLoop Fans | Aquaero 5 XT | Dual D5 | Aqualis XT Res

Link to comment
https://linustechtips.com/topic/41599-vbnet-crashing-issues/#findComment-554294
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×