Jmouse.JFiles
空间名称:Jmouse
类:JFiles
成员:JSearchOptions, JPatternOptions
方法:SetDirMethod, SetFileMethod, SetErrorMethod (+2 次重载), Search (+4 次重载)
此类是针对于 Directory 类的 GetFiles 和 GetDirectories 方法的一种完善。
当我们在使用 Directory 类的 GetFiles 和 GetDirectories 方法时,如果使用了 SearchOption.AllDirectories 来搜索子目录,则当 path 参数为卷时会出现“ 在 System.UnauthorizedAccessException 中第一次偶然出现的“mscorlib.dll”类型的异常”的 UnauthorizedAccessException 异常,这是因为当程序试图访问卷下的 System Volume Information 及其它无权访问的文件夹时发生了权限错误,而使用 Jmouse.JFiles 在对文件进行搜索时避开了该问题,并且程序员可以使用 SetErrorMethod 方法对异常进行自定义处理。
在 Jmouse.JFiles 中的成员:
在 Jmouse.JFiles 中的方法:
*关于方法的参数和重写规则可以通过 查看 Jmouse.JFiles 的源码来获取
*对于 Search 参数中的所有参数,除 JSearchOptions 和 JPatternOptions 外均与原 Directory 类中的 GetFiles 和 GetDirectories 方法的参数要求或格式相同。
示例(C#):
此文章适用于 Windows Vista, Windows XP, Windows 2000, Windows NT, Windows ME, Windows 98, Windows 95
空间名称:Jmouse
类:JFiles
成员:JSearchOptions, JPatternOptions
方法:SetDirMethod, SetFileMethod, SetErrorMethod (+2 次重载), Search (+4 次重载)
此类是针对于 Directory 类的 GetFiles 和 GetDirectories 方法的一种完善。
当我们在使用 Directory 类的 GetFiles 和 GetDirectories 方法时,如果使用了 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);
}
}
}
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