.net提供的文件夹浏览控件源码下载
public partial class FolderBrowser : UserControl { #region 共有方法和属性说明 [Browsable(true), Description("设置或获取根节点路径"), Category("外观")] public string RootPath; [Browsable(true), Description("设置或获取当前选中的文件夹的路径"), Category("外观")] public string SelectedPath; [Browsable(true), Description("设置或获取根路径的名称(即根节点的文本)"), Category("外观")] public string RootText; [Browsable(true), Description("设置或获取是否显示隐藏文件夹"), Category("行为")] public bool ShowHiddenDir; #endregion #region 若要实现FolderBrowser功能,重写以下虚函数(重写时不需调用基类函数) //返回指定目录的所有子目录,并通过Dirs返回(用于左侧目录树) //重写说明: //(1)必须重写该方法 //(2)当获取子目录和文件失败时,必须抛出异常GetSubDirsException //参数: //(1)path[in]:要获取目录信息的目录路径; //(2)filter[in]:筛选字符串 //(3)Dirs[out]:用于返回所有子目录 virtual protected void GetSubDirectory(string path, out LDirectoryInfo[] di); /// <summary> ///检索计算机上的所有逻辑驱动器的驱动器名称 ///返回值:返回LDriveInfo数组,表示计算机上的逻辑驱动器。 ///重写说明: ///(1)当根目录为我的电脑时,必须重写该方法 ///(2)当获取逻辑驱动器失败时,必须抛出异常GetDrivesException /// </summary> virtual protected void GetAllDrive(out LDriveInfo[] di); #endregion }下面,我们通过继承该类设计一个本地文件夹浏览控件:
public partial class LocalFolderBrowser : FolderBrowser { public LocalFolderBrowser() { InitializeComponent(); } protected override void GetAllDrive(out LDriveInfo[] ldis) { try { DriveInfo[] dis = DriveInfo.GetDrives(); ldis = new LDriveInfo[dis.Length]; int i = 0; foreach (DriveInfo di in dis) { ldis[i] = new LDriveInfo(di); i++; } } catch (Exception ioe) { throw new GetDriveException(ioe.Message); } } protected override void GetSubDirectory(string path, out LDirectoryInfo[] Dirs) { try { DirectoryInfo[] dis = null; DirectoryInfo pdi = new DirectoryInfo(path); dis = pdi.GetDirectories(); Dirs = new LDirectoryInfo[dis.Length]; int i = 0; foreach (DirectoryInfo di in dis) Dirs[i++] = new LDirectoryInfo(di); } catch (Exception Error) { Dirs = null; throw new GetDirsAndFilesException(Error.Message); } } }以上就是文件夹浏览控件的设计思路和代码。