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.

Dani AI

Generated

Short summary and practical fixes that apply to what described and to ’s comment about hooking: the extra dialog is almost always generated by the code/path that actually fails (often the file-open call) — not by your own MessageBox. The correct approach is to prevent or catch the failure before the external runtime shows its dialog. Hooking another process’s windows is possible but fragile; it is a last resort.

Make the file operation resilient and close any helper UI when an error is caught. Validate the path, ensure the file exists, open it with an appropriate sharing mode (so you don’t get “in use” failures if another app has the file open), and always dispose handles quickly. Wrap the read in a Try/Catch and, in the Catch, close the list dialog on the UI thread before showing your own message. Example pattern (VB.NET):

Try
    Dim path As String = "C:\path\to\file.log"
    If Not File.Exists(path) Then
        MessageBox.Show("File not found.")
        Return
    End If

    Using fs As New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
        Using sr As New StreamReader(fs)
            While Not sr.EndOfStream
                Dim line = sr.ReadLine()
                ' process line
            End While
        End Using
    End Using

Catch ex As IOException
    If helperForm IsNot Nothing AndAlso helperForm.Visible Then
        helperForm.Invoke(Sub() helperForm.Close())
    End If
    MessageBox.Show("Error reading file: " & ex.Message)
End Try

If the offending dialog is originating from the other program (VB6 runtime or similar), the reliable options are: stop/adjust that program, read a copy of the file, or implement a retry-with-delay pattern while opening with FileShare. Avoid cross-process message hooking unless you have to — it is error-prone and platform-specific. Use tools like Process Monitor to see which process actually holds the file handle (ProcMon), and refer to the .NET docs on FileShare and invalid filename checks for validation helpers (FileShare, Path.GetInvalidFileNameChars).

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