zoukankan      html  css  js  c++  java
  • 文件夹浏览控件(.NET)

        .net提供的文件夹浏览控件FolderBrowserDialog只能浏览本地文件夹,本文中,将设计一个文件夹浏览控件,通过继承该控件并重写某些虚函数,就可以扩展出不同功能的文件夹浏览控件(如浏览本地文件夹,浏览网络上其他机器的文件夹)。

    设计思路:

        比较不同功能的文件夹浏览控件(如浏览本地文件夹,浏览网络上其他机器的文件夹)。可以发现,它们只是获取目录信息的方式不同(例如本地浏览控件可以调用相关的函数获取,而浏览网络上其他机器的文件夹则需要通过网络连接来获取),而其他功能如显示目录树,选择某个文件夹时触发事件等都是一样的。为了避免编写不同功能的文件夹浏览控件时重复编写实现这些功能的代码,我们可以设计一个文件夹浏览控件的基类,该基类提供了文件夹浏览控件必须的功能如显示目录树,选择某个文件夹时触发事件,而获取目录信息的方法被设计成虚函数,这样一来,当我们需要某种功能的文件夹浏览控件,只需继承该类,并重写获取目录信息的虚函数,就可以扩展出我们需要的文件夹浏览控件。

    文件夹浏览控件基类代码(由于代码较长,只给出方法定义和说明):

    源码下载

    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);
            }
        }
    }
    以上就是文件夹浏览控件的设计思路和代码。
  • 相关阅读:
    csp-s89考试记录
    急需巩固
    近日反思
    csp-s 81 瓶颈
    近日总结
    csp-s 76总结
    PhoneGap + JQueryMobile + HTML5
    PHP大文件上传配置
    scp命令报错(IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!)
    mysql权限设置
  • 原文地址:https://www.cnblogs.com/lucc/p/1045447.html
Copyright © 2011-2022 走看看