using System.Runtime.InteropServices; using System; public class ChinarFileDlog { public int structSize = 0; public IntPtr dlgOwner = IntPtr.Zero; public IntPtr instance = IntPtr.Zero; public string filter = null; public string customFilter = null; public int maxCustFilter = 0; public int filterIndex = 0; public string file = null; public int maxFile = 0; public string fileTitle = null; public int maxFileTitle = 0; public string initialDir = null; public string title = null; public int flags = 0; public short fileOffset = 0; public short fileExtension = 0; public string defExt = null; public IntPtr custData = IntPtr.Zero; public IntPtr hook = IntPtr.Zero; public string templateName = null; public IntPtr reservedPtr = IntPtr.Zero; public int reservedInt = 0; public int flagsEx = 0; } public class OpenFileDialog { [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)] public static extern bool GetOpenFileName([In, Out] ChinarFileDlog ofd); } public class FolderSelectDialog { private const uint BIF_RETURNONLYFSDIRS = 0x0001; [DllImport("shell32.dll")] private static extern IntPtr SHBrowseForFolder(ref BROWSEINFO lpbi); [DllImport("shell32.dll", CharSet = CharSet.Unicode)] private static extern bool SHGetPathFromIDList(IntPtr pidl, IntPtr pszPath); [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] private struct BROWSEINFO { public IntPtr hwndOwner; public IntPtr pidlRoot; public IntPtr pszDisplayName; public string lpszTitle; public uint ulFlags; public IntPtr lpfn; public IntPtr lParam; public int iImage; } public static string OpenFolderDialog(string title) { string selectedPath = string.Empty; var bi = new BROWSEINFO(); bi.hwndOwner = IntPtr.Zero; bi.ulFlags = BIF_RETURNONLYFSDIRS; bi.lpszTitle = title; IntPtr pidl = SHBrowseForFolder(ref bi); if (pidl != IntPtr.Zero) { var path = Marshal.AllocHGlobal(260 * sizeof(char)); if (SHGetPathFromIDList(pidl, path)) { selectedPath = Marshal.PtrToStringUni(path); } Marshal.FreeHGlobal(path); Marshal.FreeCoTaskMem(pidl); } return selectedPath; } }