- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
伙计们,我在我的应用程序中有一个功能,可以使用 GetFiles 在特定目录中搜索特定文件。方法
System.IO.Directory.GetFiles(string path, string searchPattern, System.IO.SearchOption)
它工作正常,直到我选择要搜索的驱动器目录(D:\
或 C:\
等),因为它也在访问回收站,然后限制
Access to the path 'D:\$RECYCLE.BIN\S-1-5-21-106145493-3722843178-2978326776-1010' is denied.
它还需要能够搜索子文件夹 (SearchOption.AllDirectories
)。
如何跳过要搜索的地方?因为可能还有任何其他文件夹也被拒绝访问。
我将 SKIP 大写,因为如果我使用 try catch 并捕获到一个异常,那么整个搜索也会失败。
谢谢。请澄清您需要的任何内容。
最佳答案
已编辑以更加清晰。
当递归扫描目录树时,比如使用递归方法,以目录开始作为参数,你可以得到目录的属性。然后检查它是否是系统目录而不是像“C:\”这样的根目录 - 在这种情况下,您想要跳过该目录,因为它可能是,例如,回收站。
这是执行此操作的一些代码,还捕获了一些在我摆弄目录扫描时发生的常见异常。
void scan_dir(string path)
{
// Exclude some directories according to their attributes
string[] files = null;
string skipReason = null;
var dirInfo = new DirectoryInfo( path );
var isroot = dirInfo.Root.FullName.Equals( dirInfo.FullName );
if ( // as root dirs (e.g. "C:\") apparently have the system + hidden flags set, we must check whether it's a root dir, if it is, we do NOT skip it even though those attributes are present
(dirInfo.Attributes.HasFlag( FileAttributes.System ) && !isroot) // We must not access such folders/files, or this crashes with UnauthorizedAccessException on folders like $RECYCLE.BIN
)
{ skipReason = "system file/folder, no access";
}
if ( null == skipReason )
{ try
{ files = Directory.GetFiles( path );
}
catch (UnauthorizedAccessException ex)
{ skipReason = ex.Message;
}
catch (PathTooLongException ex)
{ skipReason = ex.Message;
}
}
if (null != skipReason)
{ // perhaps do some error logging, stating skipReason
return; // we skip this directory
}
foreach (var f in files)
{ var fileAttribs = new FileInfo( f ).Attributes;
// do stuff with file if the attributes are to your liking
}
try
{ var dirs = Directory.GetDirectories( path );
foreach (var d in dirs)
{ scan_dir( d ); // recursive call
}
}
catch (PathTooLongException ex)
{ Trace.WriteLine(ex.Message);
}
}
关于c# - 如何防止Directory.GetFiles到 "check"回收站等 "unsafe"地方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27010203/
我写了一些代码来获取目录中的文件。为此,我使用了 Directory.GetFiles 方法。我以前用过它,所以我知道它存在并且有效。 所以我开始使用它(我正在使用 System.IO 命名空间),它
System.IO.Directory.GetFiles() 返回一个string[]。返回值的默认排序顺序是什么?我是按名字假设的,但如果是的话,当前的文化对其影响有多大?您可以将其更改为创建日期之
这个问题在这里已经有了答案: Exact file extension match with GetFiles()? (8 个答案) 关闭 4 年前。 我需要获取指定文件夹的所有子文件夹中的所有 .
我在一个临时文件夹中有几个文件。我可以使用以下脚本将它们加载到 Photoshop 中: var sourceFolder = Folder("C:\\temp"); if (sourceFo
我尝试在 ImageJ 插件中使用 java.awt.FileDialog,但由于某种原因,我收到一个错误,指出 Java 找不到 getFiles 方法: C:\File_Opener3.java:
我需要每天从网络驱动器复制文件。为此,我尝试了以下方法: var dir = new DirectoryInfo(@"Z:\"); var filesA300 = dir.GetFil
使用下面的代码,我能够获取名称中包含 string 的所有文件: DirectoryInfo dirInf = new DirectoryInfo(path); FileInfo[] fInfArra
是否有更有效的方法来从带有日期过滤器的目录中填充文件名列表? 目前,我正在这样做: foreach (FileInfo flInfo in directory.GetFiles()) { Da
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Can you call Directory.GetFiles() with multiple filter
我有这个基本代码 Directory.GetFiles(filepath, "*.OUT") 文件路径中有一个名为 Filename.OUT 的文件。 上面的代码可以很好地找到该文件。 现在,我通过附
我有一个包含 2 个子文件夹的文件夹,每个子文件夹包含大约 1000 个文件。这些文件如下所示: 38485303_SARA_N211_T.ygx 38485303_SARA_N211_B.ygx 3
TDirectory.GetFiles 有一个名为 SearchPattern 的参数。 Embarcadero 的文档显示 The mask used when matching file name
我正在使用 Directory.GetFiles 从特定文件夹中获取文件。默认情况下,该文件夹中的文件将按文件名排序,即。按文件名的字母顺序排列。我想按照文件修改的顺序获取文件。 我无法使用 Dire
我正在尝试实现一种将文件上传到特定本地目录的方法。 我认为: 这是在我的 Controller 操作中: def upload(){ def f
我正在实现带有输入字段和文件上传的简单表单。我浏览了教程:http://grails.org/Simple+Avatar+Uploader和文档:http://grails.org/doc/2.0.x
我在运行 System.IO.Directory.GetFiles("my_directory_name_on_network")并且对大多数 11320 来说都很好目录中的文件,但其中一个在那里两次
我在以下代码段中遇到行为差异 DirectoryInfo di = new DirectoryInfo("c:\"); FileInfo[] textFiles = di.GetFiles("log_
我有以下代码: private string[] FindExistingDocuments() { string supportedImageFormats = "jpg,pdf,doc,d
我想计算文件夹中具有特定扩展名且大小超过 X kb 的文件数。任何人都可以帮忙解决这个问题吗? 我可以做扩展部分: Directory.GetFiles("c:\\temp\\", "*.txt",
我想使用 Directory.GetFiles 从文件夹中读取文件。从msdn网站我发现:对于c,我包括: using System; using System.IO; 对于 C++,我包括: #us
我是一名优秀的程序员,十分优秀!