C# 选择目录的包装类。

 
  1. using System;
  2.         using System.Text;
  3.         using System.Runtime.InteropServices;
  4.         public class OpenFolderDialog
  5.         {
  6.             /*
  7.             private struct SHITEMID
  8.             {
  9.              ushort cb;
  10.              byte adID; // BYTE abID[1];
  11.             }
  12.             private struct ITEMIDLIST
  13.             {
  14.              SHITEMID mkid;
  15.             }
  16.             */
  17.             [StructLayout(LayoutKind.Sequential)]
  18.             private struct BROWSEINFO
  19.             {
  20.                 public IntPtr hwndOwner; // HWND hwndOwner
  21.                 public IntPtr pidlRoot; // const ITEMIDLIST* pidlRoot
  22.                 [MarshalAs(UnmanagedType.LPTStr)]
  23.                 public string pszDisplayName;
  24.                 [MarshalAs(UnmanagedType.LPTStr)]
  25.                 public string lpszTitle;
  26.                 public int ulFlags;
  27.                 [MarshalAs(UnmanagedType.FunctionPtr)]
  28.                 public BrowseCallBackProc lpfn;
  29.                 [MarshalAs(UnmanagedType.LPTStr)]
  30.                 public string lParam;
  31.                 public int iImage;
  32.             }
  33.             [DllImport("User32.Dll")]
  34.             [return: MarshalAs(UnmanagedType.Bool)]
  35.             private extern static bool SendMessage(IntPtr hWnd, uint Msg, uint wParam, [MarshalAs(UnmanagedType.LPTStr)] string lParam);
  36.             [DllImport("Shell32.dll", CharSet = CharSet.Auto)]
  37.             private extern static IntPtr SHBrowseForFolder(ref BROWSEINFO lpbi);
  38.             [DllImport("Shell32.dll", CharSet = CharSet.Auto)]
  39.             [return: MarshalAs(UnmanagedType.Bool)]
  40.             private extern static bool SHGetPathFromIDList(IntPtr pidl, StringBuilder pszPath);
  41.             private delegate int BrowseCallBackProc(IntPtr hwnd, uint msg, IntPtr lParam, [MarshalAs(UnmanagedType.LPTStr)] string lpData);
  42.             private static int BrowseCtrlCallback(IntPtr hwnd, uint uMsg, IntPtr lParam, [MarshalAs(UnmanagedType.LPTStr)] string lpData)
  43.             {
  44.                 if (uMsg == 1) // BFFM_INITIALIZED
  45.                 {
  46.                     string szPath = lpData;
  47.                     if (szPath != null && szPath != "")
  48.                     {
  49.                         if (szPath[szPath.Length - 1] != '//')
  50.                             szPath += '//';
  51.                         SendMessage(hwnd, 0x400 + 103, 1, szPath); // BFFM_SETSELECTION
  52.                     }
  53.                 }
  54.                 return 0;
  55.             }
  56.             // hwndOwner:父窗体句柄
  57.             // Title:标题;可以为null,也可以为""
  58.             // lpszInitPath:初始目录;可以为null,也可以为"";为null或""时指向“我的电脑”
  59.             // return:如果按下了“确定”按钮则为用户选择的目录,否则返回null
  60.             public static string ShowDialog(IntPtr hwndOwner, string Title, string lpszInitPath)
  61.             {
  62.                 BROWSEINFO BrInfo;
  63.                 BrInfo.hwndOwner = hwndOwner;
  64.                 BrInfo.pidlRoot = IntPtr.Zero;
  65.                 BrInfo.pszDisplayName = null;
  66.                 BrInfo.lpszTitle = Title;
  67.                 BrInfo.ulFlags = 0x0001; // BIF_RETURNONLYFSDIRS
  68.                 BrInfo.lpfn = new BrowseCallBackProc(BrowseCtrlCallback);
  69.                 BrInfo.lParam = lpszInitPath;
  70.                 BrInfo.iImage = 0;
  71.                 IntPtr pidlDestination = SHBrowseForFolder(ref BrInfo);
  72.                 string folderName = null;
  73.                 if (pidlDestination != IntPtr.Zero)
  74.                 {
  75.                     StringBuilder tmp = new StringBuilder(260); // MAX_PATH
  76.                     SHGetPathFromIDList(pidlDestination, tmp);
  77.                     folderName = tmp.ToString();
  78.                 }
  79.                 return folderName;
  80.             }
  81.         }
  82.     
  83. ///调用方法
  84. this.strItemFolder = OpenFolderDialog.ShowDialog(this.Handle, "选择目录", @"C:/");
  85.             if (strItemFolder != null)
  86.             {
  87.                 ///set select save path.
  88.                 textBox_Path.Text = this.strItemFolder;
  89.                 ///获得目录 AND 创建目录
  90.             }
  91.             else
  92.             {
  93.                 MessageBox.Show("靠,你按了“取消”按钮,啥也没选");
  94.             }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值