I've run into a couple of errors within my program which reads a text file. When a user clicks a button to read the textfile and an exception is thrown, i have it displayed as a string in a message box, but an additional message box related to runtime error 52 'bad filename or number' has shown up and continues to pop up, even after clicking ok. I am currently working on the error itself in the textfile, but until I can work around all of the variables with reading this file, I was wondering if there is anything I can do to either prevent the runtime error box from popping up or figure out why it's popping up so many times. The really weird thing is sometimes after clicking ok on the initial message box displaying the error string, it won't pop up and other times it does, repeatedly.

Recommended Answers

All 6 Replies

Can u show us ur code so that we can find out where u are going wrong....

I already know where I'm going wrong with reading the textfile and from what I can see right now I think I know why the runtime error box keeps popping up. The textfile being read is basically a 'junk file' from another program which I parse data from to place into my winform. Right now the program with the junk file is not running so when I get the exception thrown, no runtime error box pops up. I am just wondering if there is a way to disable the runtime error messagebox without disabling the original messagebox to string.

Reading and writing from/to a text file is a VERY simple procces;

You need to define a stream reader/writer

Dim sr As New StreamReader("C:/Users/MyUser/Desktop/file.txt")

Do While sr.Peek <> -1
    Dim curLine as String = sr.ReadLine
    'curLine now holds an entire line from the text file, from crlf to crlf'
Loop

For writing to a text file you do the same thing, just opposite.

Dim sw As New StreamWriter("C:/Users/MyUser/Desktop/file.txt")

Dim S() As String = {"Line 1","Line 2","Line 3","Line 4"}

For i = 0 to S.count - 1
    sw.WriteLine(s(i)) 
    'This will loop through the array and print every element on it's own line.'
Next

Now with that being said, we can't see your code to find any errors. But as log as you have the basics down, you should be able to determine the root of the problem.

it is also a possibility that the file is being used by the other program.

Is the runtime error a "file in use" or "file is being used by another program" or something of that nature?

Well, I actually think it's a 'file in use' error because the log/junk file is a part of another program, but the messagebox 'bad filename or number' is what pops up. When the user clicks the button, a listbox pops up with all of the information the user needs to populate the form and I'm thinking I need to figure out where in my code, I need to tell the listbox to just close if an error pops up.

You might want to look into some Windows message hooking. The only way to directly catch an error would be if the other application was part of yours. Here is an example in C#. Here is a code translator that will make the code easier for you to read if you have no C# experience.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.