Advertisement:

Skystone Software

http://www.SkystoneSoftware.com

Scott Waletzko's Blog
Launching the File Properties Dialog
Published: 4/29/2008
XMl / RSS

Rather than rolling your own file properties dialog, there might be times where it is useful to ask Windows to display the standard properties dialog for a system file from your application. Doing so requires a little dip into Interop, but really isn't all that bad. Here's an example of how to create a method that displays the file properties dialog for a file given the full path to the file:

VB:
Private Const SW_SHOW = 5 
Private Const SEE_MASK_INVOKEIDLIST = &HC 

Public Structure 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 
    Dim nShow As Integer 
    Dim hInstApp As IntPtr 
    Dim lpIDList As IntPtr 
    Public lpClass As String 
    Public hkeyClass As IntPtr 
    Public dwHotKey As Integer 
    Public hIcon As IntPtr 
    Public hProcess As IntPtr 
End Structure 

<System.Runtime.InteropServices.DllImport("Shell32")> _ 
Public Shared Function ShellExecuteEx(ByRef lpExecInfo As SHELLEXECUTEINFO) As Boolean 
End Function 

Public Sub DisplayFileProperties(ByVal Filepath As String) 

    Dim shInfo As New SHELLEXECUTEINFO 

    With shInfo 
        .cbSize = System.Runtime.InteropServices.Marshal.SizeOf(shInfo) 
        .lpFile = Filepath 
        .nShow = SW_SHOW 
        .fMask = SEE_MASK_INVOKEIDLIST 
        .lpVerb = "properties" 
    End With 

    ShellExecuteEx(shInfo) 

End Sub 
	
C#:
private const int SW_SHOW = 5;
private const char SEE_MASK_INVOKEIDLIST = 0XC;

public struct SHELLEXECUTEINFO
{
	public int cbSize;
	public int fMask;
	public IntPtr hwnd;
	public string lpVerb;
	public string lpFile;
	public string lpParameters;
	public string lpDirectory;
	public int nShow;
	public IntPtr hInstApp;
	public IntPtr lpIDList;
	public string lpClass;
	public IntPtr hkeyClass;
	public int dwHotKey;
	public IntPtr hIcon;
	public IntPtr hProcess;
}

[System.Runtime.InteropServices.DllImport("Shell32")]
public extern static bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo);

public void DisplayFileProperties(string Filepath)
{

	SHELLEXECUTEINFO shInfo = new SHELLEXECUTEINFO();

	shInfo.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(shInfo);
	shInfo.lpFile = Filepath;
	shInfo.nShow = SW_SHOW;
	shInfo.fMask = SEE_MASK_INVOKEIDLIST;
	shInfo.lpVerb = "properties";

	ShellExecuteEx(ref shInfo);

}

	



Questions or Comments? .

VB to C# and C# to VB translation provided by Instant C# and Instant VB.