Using GetOpenFileName and CommDlgExtendedError


Imports System.Runtime.InteropServices
Imports System.Text

Public Class Form1

    *lt;StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)>
    Public Structure OPENFILENAME
        Public lStructSize As Integer
        Public hwndOwner As IntPtr
        Public hInstance As IntPtr
        Public lpstrFilter As String
        Public lpstrCustomFilter As String
        Public nMaxCustFilter As Integer
        Public nFilterIndex As Integer
        Public lpstrFile As String
        Public nMaxFile As Integer
        Public lpstrFileTitle As String
        Public nMaxFileTitle As Integer
        Public lpstrInitialDir As String
        Public lpstrTitle As String
        Public Flags As Integer
        Public nFileOffset As Short
        Public nFileExtension As Short
        Public lpstrDefExt As String
        Public lCustData As IntPtr
        Public lpfnHook As IntPtr
        Public lpTemplateName As String
        Public pvReserved As IntPtr
        Public dwReserved As Integer
        Public FlagsEx As Integer
    End Structure

    <DllImport("comdlg32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Public Shared Function GetOpenFileName(ByRef ofn As OPENFILENAME) As Boolean
    End Function

    <DllImport("comdlg32.dll")>
    Public Shared Function CommDlgExtendedError() As Integer
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' File buffer must be pre-filled with null characters
        Dim fileBuffer As String = New String(Chr(0), 256)
        Dim titleBuffer As String = New String(Chr(0), 64)

        Dim ofn As New OPENFILENAME()
        ofn.lStructSize = Marshal.SizeOf(ofn)
        ofn.hwndOwner = Me.Handle
        ofn.lpstrFilter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        ofn.lpstrFile = fileBuffer
        ofn.nMaxFile = fileBuffer.Length
        ofn.lpstrFileTitle = titleBuffer
        ofn.nMaxFileTitle = titleBuffer.Length
        ofn.lpstrTitle = "Select a File"
        ofn.lpstrInitialDir = "C:\"
        ofn.lpstrDefExt = "txt"
        ofn.Flags = &H80000 Or &H4 Or &H8 ' OFN_EXPLORER Or OFN_FILEMUSTEXIST Or OFN_PATHMUSTEXIST

        If GetOpenFileName(ofn) Then
            ' Extract string up to first null
            Dim selectedPath As String
            If ofn.lpstrFile.IndexOf(Chr(0)) > -1 Then
                selectedPath = ofn.lpstrFile.Substring(0, ofn.lpstrFile.IndexOf(Chr(0)))
            Else
                selectedPath = ofn.lpstrFile
            End If
            MessageBox.Show("Selected file: " & selectedPath)
        Else
            Dim err = CommDlgExtendedError()
            If err = 0 Then
                MessageBox.Show("User cancelled the dialog.")
            Else
                MessageBox.Show("Dialog failed. Error code: 0x" & err.ToString("X"))
            End If
        End If
    End Sub

End Class

Download 'Using GetOpenFileName and CommDlgExtendedError':

📥 Download using-GetOpenFileName-and-CommDlgExtendedError.vb