OpenFileDialog throws "Attempted to read or write protected memory"

1.4k views Asked by At

I face this error when I'm trying to browse files using OpenFileDialog1 in vb.net

Attempted to read or write protected memory. This is often an indication that other memory is corrupt

I'm using this code:

    Try
        OpenFileDialog1.Filter = "(*.jpg)|*.jpg"
        OpenFileDialog1.InitialDirectory = Nothing
        OpenFileDialog1.FileName = ""
        OpenFileDialog1.ShowDialog()
        If OpenFileDialog1.FileName > "" Then
            TextBox8.Text = OpenFileDialog1.FileName
            PictureBox1.Image = Image.FromFile(TextBox8.Text)
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
        Exit Sub
    End Try  

Everything works fine on my machine, but this error apears when run the app on the client machine!
Any ideas please?

3

There are 3 answers

1
Solution On

I had same issue, Just clean all solution project then rebuild.

0
Ayden On

Finally i just find it out! I was using ms Access 2010 with my projrct. Solution: is to convert to use 2003!!!! I don't know what is that related!!!!

0
Mazen el Senih On

In my experience with this exception, there are highly two reasons for this issue:

  1. This is the highly cause for your problem: always check your build platform targeting to match the assemblies you are calling, so it's a mixed platform issue as I think.

  2. Always put the disposable components in the designer, or declare it as a private variable so it get's disposed with the form destructor called.

You can set the build option to set your target platform in the project settings dialog.