目录拒绝访问问题

seven8977 2010-10-15 11:07:56
做文件查找,可是照下面这样做,由于有些目录是拒绝访问的,要是用异常的话,就不执行了,下面的东西就查找不了了!求老手指教!

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;

namespace Explorer
{
class Search
{
FrmExplorer f;
public Search(FrmExplorer FM)
{
f = FM;
}
public string key { get; set; }
public string path { get; set; }
/// <summary>
/// 搜索方法
/// </summary>
public int i = 0;
public void searchDirAndFile()
{

foreach (DirectoryInfo df in (new DirectoryInfo(path)).GetDirectories(key, SearchOption.AllDirectories))
{
i++;
f.listView1.Items.Add(new System.Windows.Forms.ListViewItem(new string[] { df.Name, "", "文件夹", "", df.LastWriteTime.ToString(), df.FullName }, 1));
}

foreach (FileInfo fi in (new DirectoryInfo(path)).GetFiles(key, SearchOption.AllDirectories))
{
i++;
f.listView1.Items.Add(new System.Windows.Forms.ListViewItem(new string[] { fi.Name, "", fi.Extension.Substring(fi.Extension.LastIndexOf(".") + 1) + "文件", fi.LastWriteTime.ToString(), fi.FullName }, 1));
}
System.Windows.Forms.MessageBox.Show("共找到" + i.ToString() + "个符合条件的结果", "查找结果", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);

}
}
}
...全文
635 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
li709107947 2012-11-15
  • 打赏
  • 举报
回复
我也遇过这样的问题,你把代码依次执行,用try{}catch检查错误语句就可以了,然后根据异常找原因,看看时候有权限
谎言 2010-10-16
  • 打赏
  • 举报
回复
遇到过这样的问题,我当时是有一个目录不能访问,... 我把这个目录名字找到,然后访问时先判断目录名,如果不是这个目录再访问
应当还有更好的方法吧
seven8977 2010-10-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 jointan 的回复:]
你的的代码的两个catch块中,应该return,因为取得子目录或文件失败后,再在集合中遍历已经没有意义了,而且会抛出空引的异常.
[/Quote]
还有麻烦您下,您有什么好的方法去实现对指定的路径下的所有子文件进行搜索,找出符合条件的呢!
seven8977 2010-10-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 jointan 的回复:]
你的的代码的两个catch块中,应该return,因为取得子目录或文件失败后,再在集合中遍历已经没有意义了,而且会抛出空引的异常.
[/Quote]
是啊,之前也这样做过,可是return过后,就直接跳出去了,方法执行不了了!有没有一种方法可以过滤掉那些访问被拒绝的目录,比如一些系统目录。还有可不可以在catch中做些东西,让它跳过这个异常,继续执行!麻烦了!
jointan 2010-10-16
  • 打赏
  • 举报
回复
你的的代码的两个catch块中,应该return,因为取得子目录或文件失败后,再在集合中遍历已经没有意义了,而且会抛出空引的异常.
seven8977 2010-10-16
  • 打赏
  • 举报
回复
没人帮忙啊。。。。
xrongzhen 2010-10-16
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 seven8977 的回复:]
引用 9 楼 jointan 的回复:
你的的代码的两个catch块中,应该return,因为取得子目录或文件失败后,再在集合中遍历已经没有意义了,而且会抛出空引的异常.

是啊,之前也这样做过,可是return过后,就直接跳出去了,方法执行不了了!有没有一种方法可以过滤掉那些访问被拒绝的目录,比如一些系统目录。还有可不可以在catch中做些东西,让它跳过这个异常,继续执行!麻烦了!
[/Quote]


在for循环中用continue(return是退出函数) 跳过此次循环,继续下一次循环
jointan 2010-10-16
  • 打赏
  • 举报
回复
这个错误应该是GetDirectories(key, SearchOption.AllDirectories))
抛出的,即这个函数自已中断了查找操作

不太清楚能不能从代码中临时提升权限

如果不能的话,只能从根目录启动,递归查找了
seven8977 2010-10-16
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 yudengchang 的回复:]
System.Security.AccessControl
[/Quote]
这个怎么弄,能稍微具体点吗?谢谢!
yudengchang 2010-10-16
  • 打赏
  • 举报
回复
System.Security.AccessControl
seven8977 2010-10-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jointan 的回复:]
C# code
foreach (DirectoryInfo df in (new DirectoryInfo(path)).GetDirectories(key, SearchOption.AllDirectories))
{
i++;
try
{
f.listView1.Items.Add(n……
[/Quote]

谢谢,小弟记住了!
seven8977 2010-10-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jointan 的回复:]
异常,肯定是某一单独的行的代码抛出的

如果你的代码是一行一行的写出来的,把抛出异常的一行用try吃掉就可以了.
[/Quote]
改了
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;

namespace Explorer
{
class Search
{
FrmExplorer f;
public Search(FrmExplorer FM)
{
f = FM;
}
public string key { get; set; }
public string path { get; set; }
public int i = 0;
/// <summary>
/// 搜索方法
/// </summary>
public void searchDirAndFile()
{

DirectoryInfo dir = new DirectoryInfo(path);
DirectoryInfo[] dirs = null;
try
{
dirs = dir.GetDirectories(key, SearchOption.AllDirectories);
}
catch (UnauthorizedAccessException)
{

}
foreach (DirectoryInfo df in dirs)
{
i++;
f.listView1.Items.Add(new System.Windows.Forms.ListViewItem(new string[] { df.Name, "", "文件夹", "", df.LastWriteTime.ToString(), df.FullName }, 1));
}



DirectoryInfo file = new DirectoryInfo(path);
FileInfo[] files=null;
try
{
files = file.GetFiles(key, SearchOption.AllDirectories);
}
catch (UnauthorizedAccessException)
{

}
foreach (FileInfo fi in files)
{
i++;
f.listView1.Items.Add(new System.Windows.Forms.ListViewItem(new string[] { fi.Name, "", fi.Extension.Substring(fi.Extension.LastIndexOf(".") + 1) + "文件", fi.LastWriteTime.ToString(), fi.FullName }, 1));
}
System.Windows.Forms.MessageBox.Show("共找到" + i.ToString() + "个符合条件的结果", "查找结果", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);

}
}
}

可是要是那句被吃了,下面遍历的时候dirs就是空的,就获得不了指定路径下的文件了!麻烦给个思路,谢谢了!!!
jointan 2010-10-15
  • 打赏
  • 举报
回复
 foreach (DirectoryInfo df in (new DirectoryInfo(path)).GetDirectories(key, SearchOption.AllDirectories))
{
i++;
try
{
f.listView1.Items.Add(new System.Windows.Forms.ListViewItem(new string[] { df.Name, "", "文件夹", "", df.LastWriteTime.ToString(), df.FullName }, 1));
}
catch(System.Exception)
{
continue;
}
}


另外,new DirectoryInfo(path)本身也有可能抛出异常,把这个过程放到for循环以外去检测.

wuyq11 2010-10-15
  • 打赏
  • 举报
回复
try{}
catch{continue;}
jointan 2010-10-15
  • 打赏
  • 举报
回复
异常,肯定是某一单独的行的代码抛出的

如果你的代码是一行一行的写出来的,把抛出异常的一行用try吃掉就可以了.


seven8977 2010-10-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jointan 的回复:]
按步就班的写,把出错的步骤用try括起来.

in (new DirectoryInfo(path)).GetFiles

这叫啥玩意?这种代码只能来这混分.

混分时,这种代码能省得敲点字,还能混过去,正经写代码时,特别是用于商业代码,该声明变量的必须声明变量,能分两行写的,不要用括号和.往一行糊弄.
[/Quote]
谢谢提醒!小弟记住了!我的问题是,能不能给提供一种思路,访问到拒绝访问的目录时候,如何处理异常,让程序跳过这个目录继续操作!谢谢!
jointan 2010-10-15
  • 打赏
  • 举报
回复
按步就班的写,把出错的步骤用try括起来.

in (new DirectoryInfo(path)).GetFiles

这叫啥玩意?这种代码只能来这混分.

混分时,这种代码能省得敲点字,还能混过去,正经写代码时,特别是用于商业代码,该声明变量的必须声明变量,能分两行写的,不要用括号和.往一行糊弄.
seven8977 2010-10-15
  • 打赏
  • 举报
回复
不知道有什么办法可以忽略掉那些受保护的文件呢!

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧