文件和文件夹检索错误 [英] files and folder retrieval error
问题描述
您好,先生...我可以问一下如何使用C#检索目录列表吗?我能够构建树形视图并检索闪存驱动器中的所有文件和文件夹,但是当我尝试检索驱动器c:或驱动器d中的所有文件时,由于我试图打开一个受限文件夹(例如,系统卷和其他有打开限制的文件夹...当我循环播放时,当我遇到那种文件夹时,我会跳过它而不会检索文件夹名称,这可能吗?请帮我解决这个问题...在此先感谢...
当我尝试运行检索文件夹的代码时出现此错误,这是错误
Hello sir... may i ask how can i retrieve a lists of directories using c#? i was able to build my treeview and retrieve all the files and folders in my flash drive but when i try to retrieve all files in drive c: or drive d: it gives me an error since i was trying to open a restricted folder such as system volume and other folder that has a restriction to open... when i loop and when i encounter that kind of folder i will just skip it and not retrieve the folder name is that possible? please help me with this... thanks in advance...
I have this Error when i try to run my code of retrieving the folder this are the error
A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Access to the path 'D:\$RECYCLE.BIN\S-1-5-21-3717064836-2505550092-2449041842-1014\' is denied.
Access to the path 'D:\ae1a6083e25e839c285e48eaaff1ee\' is denied.
A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Access to the path 'D:\System Volume Information\' is denied.
这是列出所有文件夹的代码
this is the code for listing all folder
public void listDirectories(string path, TreeNode m) {
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
TreeNode Main = m;
Main.Tag = "";
foreach (System.IO.DirectoryInfo g in dir.GetDirectories())
{
try
{
//LOAD FOLDERS
TreeNode MainNext = Main.Nodes.Add(g.Name);
MainNext.Tag = (g.Name);
listDirectories(g.FullName, MainNext);
}catch(Exception e){
MessageBox.Show("Error in folders " + e.Message);
Console.WriteLine(e.Message);
}
}
}
这将列出每个文件夹中的文件
This will list the files inside each folders
public void listFiles(string location) {
// MessageBox.Show(location);
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(location);
try
{
foreach (System.IO.FileInfo f in dir.GetFiles("*.*"))
{
try
{
//LOAD FILES
ListViewItem lSingleItem = listView1.Items.Add(f.Name);
//SUB ITEMS
lSingleItem.SubItems.Add(Convert.ToString(f.Length));
lSingleItem.SubItems.Add(f.Extension);
}
catch (Exception e)
{
MessageBox.Show("Error in files " + e.Message);
}
}
}
catch (Exception e)
{
MessageBox.Show("Error in files " + e.Message);
Console.WriteLine(e.Message);
}
}
推荐答案
RECYCLE.BIN \ S-1-5-21-3717064836-2505550092-2449041842-1014 \'被拒绝. 拒绝访问路径"D:\ ae1a6083e25e839c285e48eaaff1ee \". mscorlib.dll中发生类型为'System.UnauthorizedAccessException'的第一次机会异常 拒绝访问路径"D:\ System Volume Information \".
RECYCLE.BIN\S-1-5-21-3717064836-2505550092-2449041842-1014\' is denied. Access to the path 'D:\ae1a6083e25e839c285e48eaaff1ee\' is denied. A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll Access to the path 'D:\System Volume Information\' is denied.
这是列出所有文件夹的代码
this is the code for listing all folder
public void listDirectories(string path, TreeNode m) {
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
TreeNode Main = m;
Main.Tag = "";
foreach (System.IO.DirectoryInfo g in dir.GetDirectories())
{
try
{
//LOAD FOLDERS
TreeNode MainNext = Main.Nodes.Add(g.Name);
MainNext.Tag = (g.Name);
listDirectories(g.FullName, MainNext);
}catch(Exception e){
MessageBox.Show("Error in folders " + e.Message);
Console.WriteLine(e.Message);
}
}
}
这将列出每个文件夹中的文件
This will list the files inside each folders
public void listFiles(string location) {
// MessageBox.Show(location);
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(location);
try
{
foreach (System.IO.FileInfo f in dir.GetFiles("*.*"))
{
try
{
//LOAD FILES
ListViewItem lSingleItem = listView1.Items.Add(f.Name);
//SUB ITEMS
lSingleItem.SubItems.Add(Convert.ToString(f.Length));
lSingleItem.SubItems.Add(f.Extension);
}
catch (Exception e)
{
MessageBox.Show("Error in files " + e.Message);
}
}
}
catch (Exception e)
{
MessageBox.Show("Error in files " + e.Message);
Console.WriteLine(e.Message);
}
}
您正在尝试读取所有文件位置-但是您无权访问所有文件.
请参见此处 [
You are trying to read all the file locations - however you do not have access to all of them.
See here[^].
是的,我知道这一点,并且我发现了如何排除所有不需要的文件夹,特别是隐藏的文件夹...但是由于某种原因,它包括此文件
yes i know that and i found out how to exclude all the unwanted folders specially the hidden one... but for some reason it includes this file
ae1a6083e25e839c285e48eaaff1ee
,并且根据其属性,它没有隐藏,读取,系统,压缩的属性,现在我不知道为什么无法打开它...即时通讯正在执行此操作,以检查是否包含有效的文件夹
and based from its attributes it does not have hidden,read,system,compressed attributes now i dont know why i cant open it... im doing this checking if the folder is valid to be included
<pre>public bool isValidDirectory(string path) {
bool isvalid = true;
bool isReadOnly = ((File.GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
// check whether a file is hidden
bool isHidden = ((File.GetAttributes(path) & FileAttributes.Hidden) == FileAttributes.Hidden);
// check whether a file has archive attribute
bool isArchive = ((File.GetAttributes(path) & FileAttributes.Archive) == FileAttributes.Archive);
// check whether a file is system file
bool isSystem = ((File.GetAttributes(path) & FileAttributes.System) == FileAttributes.System);
bool isEncryted = ((File.GetAttributes(path) & FileAttributes.Encrypted ) == FileAttributes.Encrypted);
bool isCompressed = ((File.GetAttributes(path) & FileAttributes.Compressed) == FileAttributes.Compressed);
bool isNotContentIndexed = ((File.GetAttributes(path) & FileAttributes.NotContentIndexed) == FileAttributes.NotContentIndexed);
bool isSparseFile = ((File.GetAttributes(path) & FileAttributes.SparseFile) == FileAttributes.SparseFile);
bool isTemporary = ((File.GetAttributes(path) & FileAttributes.Temporary) == FileAttributes.Temporary);
bool isNormal = ((File.GetAttributes(path) & FileAttributes.Normal) == FileAttributes.Normal);
if (isReadOnly || isHidden || isArchive || isSystem || isEncryted || isCompressed || isNotContentIndexed || isSparseFile || isTemporary)
{
if (!isNormal)
{
isvalid = false;
}
}
return isvalid;
}
但是它仍然列出了该文件夹,当我尝试打开它时,它给了我一个错误,我不知道该如何解决...我不知道如何排除这种类型的文件...是否有办法找出是否我可以打开"文件吗?
but it still lists the folder and when i try to open it, it gives me an error i dont know how to solve this... i dont know how to exclude this type of file... is there a way to find out if i can "OPEN" the file?
这篇关于文件和文件夹检索错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!