// This class was found online.
// Original author is probably: Swizzy
// https://github.com/ttgxdinger/Random/blob/master/CPUKey%20Checker/CPUKey%20Checker/FolderSelectDialog.cs
using System;
using System.Windows.Forms;
namespace WPinternals
{
///
/// Wraps System.Windows.Forms.OpenFileDialog to make it present
/// a vista-style dialog.
///
public class FolderSelectDialog
{
// Wrapped dialog
private readonly OpenFileDialog ofd = null;
///
/// Default constructor
///
public FolderSelectDialog()
{
ofd = new OpenFileDialog
{
Filter = "Folders|\n",
AddExtension = false,
CheckFileExists = false,
DereferenceLinks = true,
Multiselect = false
};
}
#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 = string.IsNullOrEmpty(value) ? Environment.CurrentDirectory : value; }
}
///
/// Gets/Sets the title to show in the dialog
///
public string Title
{
get { return ofd.Title; }
set { ofd.Title = value ?? "Select a folder"; }
}
///
/// 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)
{
var fbd = new FolderBrowserDialog
{
Description = this.Title,
SelectedPath = this.InitialDirectory,
ShowNewFolderButton = false
};
if (fbd.ShowDialog(new WindowWrapper(hWndOwner)) != DialogResult.OK)
{
return false;
}
ofd.FileName = fbd.SelectedPath;
return true;
}
#endregion
}
///
/// Creates IWin32Window around an IntPtr
///
public class WindowWrapper : IWin32Window
{
///
/// Constructor
///
/// Handle to wrap
public WindowWrapper(IntPtr handle)
{
Handle = handle;
}
///
/// Original ptr
///
public IntPtr Handle { get; }
}
}