我试图显示在所选目录(以及可选的任何子目录)中找到的所有文件的列表.我遇到的问题是,当GetFiles()方法遇到无法访问的文件夹时,它会抛出异常并且进程停止.
如何忽略此异常(并忽略受保护的文件夹/文件)并继续将可访问文件添加到列表中?
try
{
if (cbSubFolders.Checked == false)
{
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
foreach (string fileName in files)
ProcessFile(fileName);
}
else
{
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories);
foreach (string fileName in files)
ProcessFile(fileName);
}
lblNumberOfFilesDisplay.Enabled = true;
}
catch (UnauthorizedAccessException) { }
finally {}
Run Code Online (Sandbox Code Playgroud) 在C#中,如何检查目录或其任何子目录中是否存在特定文件?
System.IO.File.Exists似乎只接受一个没有重载的单个参数来搜索子目录.
我可以使用SearchOption.AllDirectories重载LINQ和System.IO.Directory.GetFiles,但这看起来有点沉重.
var MyList = from f in Directory.GetFiles(tempScanStorage, "foo.txt", SearchOption.AllDirectories)
where System.IO.Path.GetFileName(f).ToUpper().Contains(foo)
select f;
foreach (var x in MyList)
{
returnVal = x.ToString();
}
Run Code Online (Sandbox Code Playgroud) 我正在运行下面的代码并在下面获得例外.我是否被迫将此函数放入try catch中,还是以其他方式递归获取所有目录?我可以编写自己的递归函数来获取文件和目录.但我想知道是否有更好的方法.
// get all files in folder and sub-folders
var d = Directory.GetFiles(@"C:\", "*", SearchOption.AllDirectories);
// get all sub-directories
var dirs = Directory.GetDirectories(@"C:\", "*", SearchOption.AllDirectories);
Run Code Online (Sandbox Code Playgroud)
"拒绝访问路径'C:\ Documents and Settings \'."
从VB6时代开始,当我通过系统上的目录递归时,我能够以相对适当的方式使用"on next error next".如果我的"foreach"循环遇到Permission Denied或Access Denied错误,我所要做的就是调用"resume next"语句.
然而,在C#中,这不存在,我理解为什么.然而,令人难以理解的是在C#中弄清楚这是如何实现的.
我试图通过我的硬盘上的目录递归并填充TreeView控件.
private void PopulateTree(string dir, TreeNode node)
{
try
{
// get the information of the directory
DirectoryInfo directory = new DirectoryInfo(dir);
// loop through each subdirectory
foreach (DirectoryInfo d in directory.GetDirectories("*", SearchOption.AllDirectories))
{
// create a new node
TreeNode t = new TreeNode(d.Name);
// populate the new node recursively
PopulateTree(d.FullName, t);
node.Nodes.Add(t); // add the node to the "master" node
}
// lastly, loop through each file in the directory, and …Run Code Online (Sandbox Code Playgroud) 我正在使用此代码搜索所有驱动器中的所有目录以搜索所有.txt文件:
public List<string> Search()
{
var files = new List<string>();
foreach (DriveInfo d in DriveInfo.GetDrives().Where(x => x.IsReady == true))
{
files.AddRange(Directory.GetFiles(d.RootDirectory.FullName, "*.txt", SearchOption.AllDirectories));
}
return files;
}
Run Code Online (Sandbox Code Playgroud)
但在运行中我遇到了这个错误:

怎么解析呢?
谢谢.
我有一个自定义对象,其中包含返回根目录中所有目录和文件名的方法
string[] dirlist = obj.GetDirectories();
//which returns all dir names in root
string[] filelist = obj.GetFiles();
//which return all file names in root
Run Code Online (Sandbox Code Playgroud)
我无法修改这些方法.一旦我得到了dirlist,我如何获得其中所有子目录的列表以及子目录中的文件,忽略安全异常.它可以嵌套到多个级别..NET 4中有什么东西吗?
更新:string [] dirList也可以读作List dirlist.请提供一个使用.NET最新功能的解决方案
DirectoryOne
- SubDirOne
- SubDirTwo
- FileOne
- FileTwo
- SubDirThree
DirectoryTwo
DirectoryOne
Run Code Online (Sandbox Code Playgroud)