dotnet thoughts 

a dotnet developer's technical blog

File Properties Dialog box using VB.Net

While working with my WPF project, I come to a requirement, where I need to display the File Properties dialog box, which available on right click on a File > selecting Properties. I got one solution from Google groups, but I forgot the link. :( I am posting the code.

Imports System
Imports System.Runtime.InteropServices
Namespace Win32
    <StructLayout(LayoutKind.Sequential)gt; _
 Public Class SHELLEXECUTEINFO
        Public cbSize As Integer
        Public fMask As Integer
        Public hwnd As IntPtr
        Public lpVerb As String
        Public lpFile As String
        Public lpParameters As String
        Public lpDirectory As String
        Public nShow As Integer
        Public hInstApp As Integer
        Public lpIDList As Integer
        Public lpClass As String
        Public hkeyClass As Integer
        Public dwHotKey As Integer
        Public hIcon As Integer
        Public hProcess As Integer
    End Class
    Public Class Shell
        Public Const SEE_MASK_INVOKEIDLIST As Integer = 12

        <DllImport("shell32.dll")gt; _
        Public Shared Function ShellExecuteEx(<[In](), Out()> _
      ByVal execInfo As SHELLEXECUTEINFO) As Boolean
        End Function
    End Class
End Namespace

And you can call the function like this

        Dim fileInfo As New Win32.SHELLEXECUTEINFO
        fileInfo.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(fileInfo)
        fileInfo.lpVerb = "properties"
        fileInfo.fMask = Win32.Shell.SEE_MASK_INVOKEIDLIST
        fileInfo.nShow = 1
        fileInfo.lpFile = filename		'File name to display properties.
        Win32.Shell.ShellExecuteEx(fileInfo)

There is one issue I am facing right now with the code, the Dialog is not displayed as Modal window. I have to do R&D on it, and will update later.

No Responses to “File Properties Dialog box using VB.Net”

  1. I get an “Attempted to read or write protected memory…” exception when I call ShellExecuteEx(). Any idea how to get around that?

    Comment by Justin — July 7, 2009 @ 9:30 pm

  2. Normally we are getting this error when the signature of the API is different.

    Comment by anuraj — July 8, 2009 @ 9:30 am

RSS feed for comments on this post. TrackBack URL

Leave a Response

*