Jmouse.JFiles 类

Jmouse.JFiles
空间名称:
Jmouse
类:JFiles
成员:JSearchOptions, JPatternOptions
方法:SetDirMethod, SetFileMethod, SetErrorMethod (+2 次重载), Search (+4 次重载)

此类是针对于 Directory 类的 GetFilesGetDirectories 方法的一种完善。
当我们在使用 Directory 类的  GetFilesGetDirectories 方法时,如果使用了 SearchOption.AllDirectories 来搜索子目录,则当 path 参数为卷时会出现“ 在 System.UnauthorizedAccessException 中第一次偶然出现的“mscorlib.dll”类型的异常”的 UnauthorizedAccessException 异常,这是因为当程序试图访问卷下的 System Volume Information 及其它无权访问的文件夹时发生了权限错误,而使用 Jmouse.JFiles 在对文件进行搜索时避开了该问题,并且程序员可以使用 SetErrorMethod 方法对异常进行自定义处理。

在 Jmouse.JFiles 中的成员:
JSearchOptions设置搜索范围
JPatternOptions当使用 searchPattern 查找特定文件时,决定 searchPattern 的适用范围





在 Jmouse.JFiles 中的方法:
SetDirMethod需要重写,此方法可以得到并处理所有文件夹的绝对路径
SetFileMethod需要重写,此方法可以得到并处理所有文件的绝对路径
SetErrorMethod 需要重写,此方法可以得到并处理在搜索过程中发生的异常 (+2 次重载)
Search 此方法用来搜索文件及文件夹,并返回出错文件及文件夹的个数 (+4 次重载)








*关于方法的参数和重写规则可以通过 查看 Jmouse.JFiles 的源码来获取
*对于 Search 参数中的所有参数,除 JSearchOptions 和 JPatternOptions 外均与原 Directory 类中的 GetFiles 和 GetDirectories 方法的参数要求或格式相同。

示例(C#):
using  System;
using
 System.Collections.Generic;
using
 System.Text;
using
 System.IO;
using Jmouse; //引用 Jmouse 空间


namespace  MyConsole
{
    
class
 MyClass
    
{
        
static void Main(string
[] args)
        
{
            newJFiles clsJFiles 
= new
 newJFiles();
            
//搜索并显示所有文件及文件夹

            Console.WriteLine(" 搜索完毕,共有 {0} 个文件及文件夹在搜索中发生了错误。",
                clsJFiles.Search(
"C://"
).ToString());
            
//搜索并显示根目录下的所有文件及文件夹

            Console.WriteLine(" 搜索完毕,共有 {0} 个文件及文件夹在搜索中发生了错误。",
                clsJFiles.Search(
"C://"
,
                JFiles.JSearchOptions.OnlyTopDirectory).ToString());
            
//搜索并显示所有以 "J" 开头的文件及文件夹

            Console.WriteLine(" 搜索完毕,共有 {0} 个文件及文件夹在搜索中发生了错误。",
                clsJFiles.Search(
"C://"
,
                
"J*"
,
                JFiles.JPatternOptions.Both).ToString());
            
//搜索并显示所有根目录下以 "J" 开头的文件

            Console.WriteLine(" 搜索完毕,共有 {0} 个文件及文件夹在搜索中发生了错误。",
                clsJFiles.Search(
"C://"
,
                
"J*"
,
                JFiles.JPatternOptions.OnlyFiles,
                JFiles.JSearchOptions.OnlyTopDirectory).ToString());
            
//构建一个错误,".."是非法字符

            Console.WriteLine(" 搜索完毕,共有 {0} 个文件及文件夹在搜索中发生了错误。",
                clsJFiles.Search(
"C://"
,
                
".."
,
                JFiles.JPatternOptions.Both).ToString());
        }

    }


    
public class newJFiles : JFiles
    
{
        
/*newJFiles 派生自 Jmouse.JFiles,并重写所有方法来列出搜索到的
        文件、文件夹以及搜索中出现的错误
*/


        
protected override void SetDirMethod(string[] path)
        
{
            
if (path.Length > 0
)
            
{
                
for (int j = 0; j < path.Length; j++
)
                
{
                    Console.WriteLine(path[j]);
                }

            }

        }


        
protected override void SetFileMethod(string[] path)
        
{
            
if (path.Length > 0
)
            
{
                
for (int j = 0; j < path.Length; j++
)
                
{
                    Console.WriteLine(path[j]);
                }

            }

        }


        
protected override void SetErrorMethod(string path, 
            Exception e)
        
{
            Console.WriteLine(
" 错误:{0}"
, e.Message);
        }


        
protected override void SetErrorMethod(string path,
            
string
 searchPattern, Exception e)
        
{
            Console.WriteLine(
" 错误:{0}"
, e.Message);
            Console.WriteLine(
"这可能是由于您使用的 {0} 字符中包含了非法字符。"

                , searchPattern);
        }

    }

}

此文章适用于 Windows Vista, Windows XP, Windows 2000, Windows NT, Windows ME, Windows 98, Windows 95
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值