VB.net Crashing Issues(Answered)

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(sedner 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.

Your code works. I dont think its crashed, It's probably busy as hell scanning the entire drive. and since its singletreaded everything seems to 'hang' until it is completely done.

This is a raw search in .net code. not an indexed one like you are used to. You might get comfortable while waiting. Or you should specify more for on the directory info.

OR you could make some output, so you can keep track.

OOOOR if you really are an avid .net programmer, you could multitread the thing (Might pose challenging)

I did some more testing with it and it is hitting an infinite loop when it gets to a file that it can't access, because when it loops it start over again instead of starting where the error was thrown. Do you(or anyone else) know a way around this?

Try.... Catch! 

And in the catch you could do like... Nothing (as in: just carry on plz)

Or you could even log where he flukes (catch e as exception, e.message)

Just google this wonderfull way of catching exceptions, youll get the hang of it :)

I am already using a try...catch statement, but when it hits the error and goes to run again it starts at the beginning again and so it hits the same error in the same location. So I am not making any progress and it repeat the same thing in an infinite loop.

Oh.. I'm sorry. I'm not at my coding pc atm so I didn't test it. I'm kind of suprised aswell it retries a statement that just gave an exception. Actually thinking about it, it should break off & goto the next statement... Weird..

Intresting. Let me see if I can up with something..

Oh boy, There is no optimized way to do this (that I cant think of) I have some concepts in my head that require you to make huge sized arrays (folders & files) that keep track of where your function would have 'left of'. I cant test this but I would imagine It wont win a noble prise of an optimized search...

Cant you just test the script in administrator mode? (By compiling the binary and admin'ing it)

 

Btw - does it fail at open files, or system files?

I made it admin access only but I still get stopped at "C:\Documents and Settings".

You mean "run as admin" right?

All system files, that are either hidden or in the "Windows" folder.

Yes

Okay, do it in a "for each" style. Getfiles seems to be made in that fashion. That way it knows where it left off. You need to for each every dir, and then capsulate each file.

Use this for help:


http://msdn.microsoft.com/en-us/library/dd997370.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

You can see here how they do the same catch, only here, in a for each structure, it probably knows where it left off.

Good luck, i dont have visual studio here

Actually, forget about getfiles, you can query for the name yourself with fi.FullName.

Enumerate each subfolder of a given folder, and each subfolder its files and other folders. I'm sure there is a nice recursive way for folders into folders, just google it when the ms pages get too general about it, look for simple examples.

By this method you are programming the search yourself and have more control about errors and (apperantly) bugs

Its alot more work, but that seems to be necesairy at this point

Again im sorry for not being more specific with examples but Im not at a coding machine :/

Sub DirSearch(ByVal sDir As String)

Dim d As String

Dim f As StringTry

 For Each d In Directory.GetDirectories(<the dir>)

  For Each f In Directory.GetFiles(d, txtFile.Text)

   if f.equals(<file youre looking for>) then

    put f somewhere 

   end if

  Next

  DirSearch(d) '<== sweet recursiveness.

 Next

Catch excpt As System.Exception

Debug.WriteLine(excpt.Message)

End Try

end sub

---

If the file throws any exception, Im hoping he will just finish the nested loop with the other files.

That, or, .Net has som serious issues. At that point I would have had it with .net.

Sub DirSearch1(ByVal sDir As String)

Dim d As String

Dim f As String
Try

For Each d In Directory.GetDirectories("C:\")

For Each f In Directory.GetFiles(d, "FileName.exe")

If f.Equals("FileName.exe") Then

txt_Location.Text = f

End If

Next

DirSearch1(d) '<== sweet recursiveness.

Next

Catch excpt As System.Exception

Debug.WriteLine(excpt.Message)

End Try

End Sub

 

Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click


DirSearch1("C:\")

End Sub

 

This gives me an overflow error.

Yep It does.

In better news, I'm at the coding machine atm.

http://pastebin.com/iRzw5BBX

It seems to work on smaller folders, and it doesnt seem to crash on my C:\

However the C: on this machine is HUGE. I cannot hang around to let it finish.

It works, thank you so much, I have been working on this for a week and looking around on forums for about 5 days, and now it finally works.

Is there anyway I could make to code a little smaller if it will never have to handle more then one .exe at the same time and there will never be 2 outputs?

Cool, Glad I could make a diffrence.

And I'm afraid not, this particular design of coding uses a stack of strings inside a function, which (well it seems that way to me, I got overflows with other numerous methods) look to be extremely resource-friendly considering you can't make it crash even with scanning an entire harddisk.

I would really recommend keeping the current code, as it will still be flexible. And even with knowing how much outputs you would expect, I don't think you can change anything.