using System;
using System.Windows.Forms;
// ------------------------------------------------------------------
// Wraps System.Windows.Forms.OpenFileDialog to make it present
// a vista-style dialog.
// ------------------------------------------------------------------
namespace FolderSelect
{
///
/// Wraps System.Windows.Forms.OpenFileDialog to make it present
/// a vista-style dialog.
///
public class FolderSelectDialog : IDisposable
{
// Wrapped dialog
System.Windows.Forms.OpenFileDialog ofd = null;
///
/// Default constructor
///
public FolderSelectDialog()
{
ofd = new System.Windows.Forms.OpenFileDialog();
ofd.Filter = "Folders|\n";
ofd.AddExtension = false;
ofd.CheckFileExists = false;
ofd.DereferenceLinks = true;
ofd.Multiselect = false;
}
~FolderSelectDialog()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
ofd.Dispose();
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#region Properties
///
/// Gets/Sets the initial folder to be selected. A null value selects the current directory.
///
public string InitialDirectory
{
get { return ofd.InitialDirectory; }
set { ofd.InitialDirectory = value == null || value.Length == 0 ? Environment.CurrentDirectory : value; }
}
///
/// Gets/Sets the title to show in the dialog
///
public string Title
{
get { return ofd.Title; }
set { ofd.Title = value == null ? "Select a folder" : value; }
}
///
/// Gets the selected folder
///
public string FileName
{
get { return ofd.FileName; }
}
#endregion
#region Methods
///
/// Shows the dialog
///
/// True if the user presses OK else false
public bool ShowDialog()
{
return ShowDialog(IntPtr.Zero);
}
///
/// Shows the dialog
///
/// Handle of the control to be parent
/// True if the user presses OK else false
public bool ShowDialog(IntPtr hWndOwner)
{
bool flag = false;
if (Environment.OSVersion.Version.Major >= 6)
{
var r = new Reflector("System.Windows.Forms");
uint num = 0;
Type typeIFileDialog = r.GetType("FileDialogNative.IFileDialog");
object dialog = r.Call(ofd, "CreateVistaDialog");
r.Call(ofd, "OnBeforeVistaDialog", dialog);
uint options = (uint)r.CallAs(typeof(System.Windows.Forms.FileDialog), ofd, "GetOptions");
options |= (uint)r.GetEnum("FileDialogNative.FOS", "FOS_PICKFOLDERS");
r.CallAs(typeIFileDialog, dialog, "SetOptions", options);
object pfde = r.New("FileDialog.VistaDialogEvents", ofd);
object[] parameters = new object[] { pfde, num };
r.CallAs2(typeIFileDialog, dialog, "Advise", parameters);
num = (uint)parameters[1];
try
{
int num2 = (int)r.CallAs(typeIFileDialog, dialog, "Show", hWndOwner);
flag = 0 == num2;
}
finally
{
r.CallAs(typeIFileDialog, dialog, "Unadvise", num);
GC.KeepAlive(pfde);
}
}
else
{
var fbd = new FolderBrowserDialog();
fbd.Description = this.Title;
fbd.SelectedPath = this.InitialDirectory;
fbd.ShowNewFolderButton = false;
if (fbd.ShowDialog(new WindowWrapper(hWndOwner)) != DialogResult.OK) return false;
ofd.FileName = fbd.SelectedPath;
flag = true;
}
return flag;
}
#endregion
}
///
/// Creates IWin32Window around an IntPtr
///
public class WindowWrapper : System.Windows.Forms.IWin32Window
{
///
/// Constructor
///
/// Handle to wrap
public WindowWrapper(IntPtr handle)
{
_hwnd = handle;
}
///
/// Original ptr
///
public IntPtr Handle
{
get { return _hwnd; }
}
private IntPtr _hwnd;
}
}