使用Directory.GetFiles方法时如何避免访问被拒绝的路径异常 [英] how i can avoid access denied path exception when working with Directory.GetFiles method
本文介绍了使用Directory.GetFiles方法时如何避免访问被拒绝的路径异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我想要使用以下代码在硬盘之一中获取文件列表:
Hi all
I want get list of file in one of hard drive with this code:
String[] file= Directory.GetFiles(DriveLeterComboBox.SelectedItem.ToString(),"*.txt", SearchOption.AllDirectories);
当遇到诸如系统卷信息之类的系统目录时,抛出
when it Be encountered with system directory such as System Volume Information throw
Access to the path 'C:\System Volume Information' is denied.
例外
我如何避免这种异常?
exception
how i can avoid this exception?
推荐答案
我自己有一个thius问题,而你真的不能-最好的办法就是检查一下而不要尝试打开它:
I''v had thius problem myself, and you can''t really - the best you can do is check for it and not try to open it:
private DirNode SizeDirFiles( string dir )
{
long size = 0;
int count = 0;
if (IsIgnorable( dir ))
{
// Perfectly normal - we don't want to look here anyway, as it will throw an acess exception...
}
else
{
try
...
private bool IsIgnorable( string dir )
{
if (dir.EndsWith( ":System Volume Information" )) return true;
if (dir.Contains( ":
RECYCLE.BIN"))) true ; 返回 假; }
RECYCLE.BIN" )) return true; return false; }
这篇关于使用Directory.GetFiles方法时如何避免访问被拒绝的路径异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文