You can use SHBrowseForFolder
with
BIF_BROWSEINCLUDEFILES
A test =>
{
var hWnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
HRESULT hr = HRESULT.E_FAIL;
IntPtr pidlRet = IntPtr.Zero;
BROWSEINFO bi = new BROWSEINFO();
bi.hwndOwner = hWnd;
bi.lpszTitle = "Title";
bi.ulFlags = BIF_SHAREABLE | BIF_USENEWUI | BIF_BROWSEINCLUDEFILES;
IntPtr pidlRoot = IntPtr.Zero;
//Guid FOLDERID_PublicDocuments = new Guid("ED4824AF-DCE4-45A8-81E2-FC7965083634");
Guid FOLDERID_ComputerFolder = new Guid("0AC0837C-BBF8-452A-850D-79D08E667CA7");
hr = SHGetKnownFolderIDList(ref FOLDERID_ComputerFolder, 0, IntPtr.Zero, out pidlRoot);
bi.pidlRoot = pidlRoot;
pidlRet = SHBrowseForFolder(bi);
if (pidlRet != IntPtr.Zero)
{
IntPtr pszSelectedPath = Marshal.AllocHGlobal(((260 + 1) * Marshal.SystemDefaultCharSize));
bool bRetPath = SHGetPathFromIDList(pidlRet, pszSelectedPath);
if (bRetPath)
{
string sSelectedPath = Marshal.PtrToStringUni(pszSelectedPath);
}
Marshal.FreeHGlobal(pszSelectedPath);
}
}
Declarations :
public enum HRESULT : int
{
S_OK = 0,
S_FALSE = 1,
E_NOINTERFACE = unchecked((int)0x80004002),
E_NOTIMPL = unchecked((int)0x80004001),
E_FAIL = unchecked((int)0x80004005),
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public class BROWSEINFO
{
public IntPtr hwndOwner;
public IntPtr pidlRoot;
public IntPtr pszDisplayName;
public string lpszTitle;
public int ulFlags;
public BrowseCallbackProc lpfn;
public IntPtr lParam;
public int iImage;
}
public delegate int BrowseCallbackProc(IntPtr hwnd, int msg, IntPtr lParam, IntPtr lpData);
[DllImport("Shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr SHBrowseForFolder([In] BROWSEINFO lpbi);
// Browsing for directory.
public const int BIF_RETURNONLYFSDIRS = 0x00000001; // For finding a folder to start document searching
public const int BIF_DONTGOBELOWDOMAIN = 0x00000002; // For starting the Find Computer
public const int BIF_STATUSTEXT = 0x00000004; // Top of the dialog has 2 lines of text for BROWSEINFO.lpszTitle and one line if
// this flag is set. Passing the message BFFM_SETSTATUSTEXTA to the hwnd can set the
// rest of the text. This is not used with BIF_USENEWUI and BROWSEINFO.lpszTitle gets
// all three lines of text.
public const int BIF_RETURNFSANCESTORS = 0x00000008;
public const int BIF_EDITBOX = 0x00000010; // Add an editbox to the dialog
public const int BIF_VALIDATE = 0x00000020; // insist on valid result (or CANCEL)
public const int BIF_NEWDIALOGSTYLE = 0x00000040; // Use the new dialog layout with the ability to resize
// Caller needs to call OleInitialize() before using this API
public const int BIF_USENEWUI = (BIF_NEWDIALOGSTYLE | BIF_EDITBOX);
public const int BIF_BROWSEINCLUDEURLS = 0x00000080; // Allow URLs to be displayed or entered. (Requires BIF_USENEWUI)
public const int BIF_UAHINT = 0x00000100; // Add a UA hint to the dialog, in place of the edit box. May not be combined with BIF_EDITBOX
public const int BIF_NONEWFOLDERBUTTON = 0x00000200; // Do not add the "New Folder" button to the dialog. Only applicable with BIF_NEWDIALOGSTYLE.
public const int BIF_NOTRANSLATETARGETS = 0x00000400; // don't traverse target as shortcut
public const int BIF_BROWSEFORCOMPUTER = 0x00001000; // Browsing for Computers.
public const int BIF_BROWSEFORPRINTER = 0x00002000; // Browsing for Printers
public const int BIF_BROWSEINCLUDEFILES = 0x00004000; // Browsing for Everything
public const int BIF_SHAREABLE = 0x00008000; // sharable resources displayed (remote shares, requires BIF_USENEWUI)
public const int BIF_BROWSEFILEJUNCTIONS = 0x00010000; // allow folder junctions like zip files and libraries to be browsed
[DllImport("Shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool SHGetPathFromIDList(IntPtr pidl, IntPtr pszPath);
[DllImport("Shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern HRESULT SHILCreateFromPath([MarshalAs(UnmanagedType.LPWStr)] string pszPath, ref IntPtr ppIdl, ref uint rgflnOut);
[DllImport("Shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern HRESULT SHGetKnownFolderIDList(ref Guid rfid, uint dwFlags, IntPtr hToken, out IntPtr ppidl);
public enum KNOWN_FOLDER_FLAG
{
KF_FLAG_DEFAULT,
KF_FLAG_FORCE_APP_DATA_REDIRECTION,
KF_FLAG_RETURN_FILTER_REDIRECTION_TARGET,
KF_FLAG_FORCE_PACKAGE_REDIRECTION,
KF_FLAG_NO_PACKAGE_REDIRECTION,
KF_FLAG_FORCE_APPCONTAINER_REDIRECTION,
KF_FLAG_NO_APPCONTAINER_REDIRECTION,
KF_FLAG_CREATE,
KF_FLAG_DONT_VERIFY,
KF_FLAG_DONT_UNEXPAND,
KF_FLAG_NO_ALIAS,
KF_FLAG_INIT,
KF_FLAG_DEFAULT_PATH,
KF_FLAG_NOT_PARENT_RELATIVE,
KF_FLAG_SIMPLE_IDLIST,
KF_FLAG_ALIAS_ONLY
}