ShellExecuteEX and ShellExecuteInfo RevisitedWho says history doesn't repeat itself? I am working on a project that needs to display and access file properties. A quick search through the .NET 2.0 Framework didn't yield anything that popped out. I did however, remember our wonderful friend - the SHELL32 API. Previously, I had posted on using ShellExecuteEX with Delphi (I am a huge Delphi fan; I may not be posting much about it recently, but I did not forget it). Here is that same sample in using C#.
public const uint SW_SHOW =0x5;
public const uint SEE_MASK_INVOKEIDLIST = 0xC;
public enum verbs
...{
properties,
open,
edit,
explore,
print
}
[StructLayout(LayoutKind.Sequential)]
public struct SHELLEXECUTEINFO
...{
public int cbSize;
public uint fMask;
public IntPtr hwnd;
public String lpVerb;
public String lpFile;
public String lpParameters;
public String lpDirectory;
public uint nShow;
public int hInstApp;
public int lpIDList;
public String lpClass;
public int hkeyClass;
public uint dwHotKey;
public int hIcon;
public int hProcess;
}
[DllImport("shell32.dll")]
static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo);
public void MyShellExecuteInfo(string filename, verbs verb)
...{
SHELLEXECUTEINFO info = new SHELLEXECUTEINFO();
info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info);
switch (verb)
...{
case verbs.properties:
info.lpVerb = "properties";
break;
case verbs.print:
info.lpVerb = "print";
break;
case verbs.open:
info.lpVerb = "open";
break;
case verbs.explore:
info.lpVerb = "explore";
break;
case verbs.edit:
info.lpVerb = "edit";
break;
}
info.lpFile = filename;
info.nShow = SW_SHOW;
info.fMask = SEE_MASK_INVOKEIDLIST;
ShellExecuteEx(ref info);
}