C# 目录获取目录访问被拒绝异常和长路径问题
我有以下代码,它发现并显示空文件夹,不幸的是它不能处理所有文件夹,回收站和应用程序数据文件夹导致访问异常 下面是另一个使用枚举的用户的示例,使用它我可以访问受限文件夹,但不能处理长路径 我正在尝试Delimon.Win32.IO;它的名称空间显然可以处理长路径(我还没有测试过它) 我需要一个解决方案,可以处理访问限制和长路径-如果可能的话C# 目录获取目录访问被拒绝异常和长路径问题,c#,winforms,C#,Winforms,我有以下代码,它发现并显示空文件夹,不幸的是它不能处理所有文件夹,回收站和应用程序数据文件夹导致访问异常 下面是另一个使用枚举的用户的示例,使用它我可以访问受限文件夹,但不能处理长路径 我正在尝试Delimon.Win32.IO;它的名称空间显然可以处理长路径(我还没有测试过它) 我需要一个解决方案,可以处理访问限制和长路径-如果可能的话 private void button1_Click(object sender, EventArgs e) { //Open fol
private void button1_Click(object sender, EventArgs e)
{
//Open folder browser for user to select the folder to scan
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
//Clear text fields
listBoxResults.Items.Clear();
listBoxPath.Items.Clear();
txtFoldersFound.Clear();
//Store selected folder path
string dirPath = folderBrowserDialog1.SelectedPath;
//Process the folder
try
{
foreach (string dir in Directory.GetDirectories(dirPath, "*.*", SearchOption.AllDirectories))
{
//Populate List Box with all folders found
this.Invoke(new Action(() => listUpdate2(dir)));
if (Directory.GetDirectories(dir).Length.Equals(0))
{
//Populate List Box with all empty folders found
this.Invoke(new Action(() => listUpdate1(dir + Environment.NewLine)));
}
}
//Count of the empty folders
txtFoldersFound.Text = listBoxResults.Items.Count.ToString();
}
//Catch exceptions, seems to be folders not accessible causing this. Recycle Bin, App Data etc
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
}
在.NET 4.5中,枚举目录似乎仍然是一个问题: 提供的代码使用递归遍历目录结构
private void button1_Click(object sender, EventArgs e)
{
//Open folder browser for user to select the folder to scan
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
//Clear text fields
listBoxResults.Items.Clear();
listBoxPath.Items.Clear();
txtFoldersFound.Clear();
//Store selected folder path
string dirPath = folderBrowserDialog1.SelectedPath;
Action<string> performOnEachFolder = (s) => this.Invoke(new Action(() => listUpdate2(s)));
foreach (string emptyFolder in GetAllEmptyFolders(dirPath, performOnEachFolder))
this.Invoke(new Action(() => listUpdate2(emptyFolder)));
}
}
private static IEnumerable<string> GetAllEmptyFolders(string path, Action<string> performOnEachFolder)
{
performOnEachFolder(path);
EmptyResult result = IsDirectoryEmpty(path);
if (result == EmptyResult.Empty)
yield return path;
if (result == EmptyResult.Error)
yield break;
//A reparse point may indicate a recursive file structure. Cause this to stop the recursion.
//http://blogs.msdn.com/b/oldnewthing/archive/2004/12/27/332704.aspx
if ((File.GetAttributes(path) & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint)
yield break;
IEnumerator<string> it = Directory.EnumerateDirectories(path, "*.*", SearchOption.TopDirectoryOnly).GetEnumerator();
while (it.MoveNext())
{
foreach (string emptyFolder in GetAllEmptyFolders(it.Current, performOnEachFolder))
{
yield return emptyFolder;
}
}
}
private enum EmptyResult
{
Empty = 1,
Used = 2,
Error = 3
}
private static EmptyResult IsDirectoryEmpty(string path)
{
try
{
return !Directory.EnumerateFileSystemEntries(path).Any() ? EmptyResult.Empty : EmptyResult.Used;
}
catch (UnauthorizedAccessException)
{
//We do not want the method to throw as that will cause problems with the iterator block.
return EmptyResult.Error;
}
}
private void按钮1\u单击(对象发送者,事件参数e)
{
//打开文件夹浏览器供用户选择要扫描的文件夹
DialogResult=folderBrowserDialog1.ShowDialog();
if(result==DialogResult.OK)
{
//明文字段
listBoxResults.Items.Clear();
listBoxPath.Items.Clear();
txtFoldersFound.Clear();
//存储选定的文件夹路径
字符串dirPath=folderBrowserDialog1.SelectedPath;
Action performOnEachFolder=(s)=>this.Invoke(新操作(()=>listUpdate2));
foreach(GetAllEmptyFolders(dirPath,performOnEachFolder)中的字符串清空文件夹)
调用(新操作(()=>listUpdate2(emptyFolder));
}
}
私有静态IEnumerable GetAllEmptyFolders(字符串路径、操作性能文件夹)
{
performOnEachFolder(路径);
EmptyResult结果=IsDirectoryEmpty(路径);
if(result==EmptyResult.Empty)
收益返回路径;
if(result==EmptyResult.Error)
屈服断裂;
//重分析点可能指示递归文件结构。导致此操作停止递归。
//http://blogs.msdn.com/b/oldnewthing/archive/2004/12/27/332704.aspx
if((File.GetAttributes(path)&FileAttributes.ReparsePoint)==FileAttributes.ReparsePoint)
屈服断裂;
IEnumerator it=Directory.Enumerator目录(路径“**”,SearchOption.TopDirectoryOnly)。GetEnumerator();
while(it.MoveNext())
{
foreach(GetAllEmptyFolders(it.Current,PerformanceFolder)中的字符串emptyFolder)
{
收益-收益-空折;
}
}
}
私有枚举EmptyResult
{
空=1,
使用=2,
误差=3
}
私有静态EmptyResult IsDirectoryEmpty(字符串路径)
{
尝试
{
return!Directory.EnumerateFileSystemEntries(path).Any()?EmptyResult.Empty:EmptyResult.Used;
}
捕获(未经授权的访问例外)
{
//我们不希望该方法抛出,因为这将导致迭代器块出现问题。
返回EmptyResult.Error;
}
}
开始将GetDirectory替换为。然后,请您解释一下,当您尝试读取诸如RecycleBin之类的保留系统目录时,问题是否只是崩溃,或者是否存在其他问题?嗨,Steve,长度似乎不是枚举目录的选项。它不喜欢Directory.EnumerateDirectories(dir.Length.Equals(0))@RockE70:在你的if条件中添加这个条件,像这样-if(Directory.GetDirectories.Length.Equals(0)| Directory.GetFiles(dir.Length.Equals(0))
@krishnajarana-那不应该是&
而不是
,EnumerateDirectory返回一个IEnumerable,因此您应该使用Any()。谢谢,它成功了。但是,它不能处理长目录路径。