我正在尝试通过输入文件名或文件内容来搜索扩展名为.dcm的文件。我能够在一个目录中搜索,但是当我尝试在驱动器中搜索时,我收到一条错误,指出缺少目录或汇编引用。
string startFolder = @"C:\";
// Take a snapshot of the file system.
System.IO.DriveInfo dir = new System.IO.DriveInfo(startFolder);
// This method assumes that the application has discovery permissions
// for all folders under the specified path.
IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.
SearchOption.
AllDirectories);
答案 0 :(得分:3)
尝试在驱动器中搜索它的工作原理:
string startFolder = @"c:\";
DirectoryInfo directoryInfo = new DirectoryInfo(startFolder);
IEnumerable<System.IO.FileInfo> fileList = directoryInfo.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
答案 1 :(得分:0)
您可以立即过滤掉* .dcm扩展名
的所有文件string startFolder = @"c:\";
DirectoryInfo directoryInfo = new DirectoryInfo(startFolder);
IEnumerable<System.IO.FileInfo> fileList = directoryInfo.GetFiles("*.dcm", System.IO.SearchOption.AllDirectories);
之后使用foreach循环查找具有所需名称的文件或打开文件进行读取并搜索文件中的值。
是* .dcm基于文本的文件还是二进制文件?如果它们是基于文本的文件,您可以使用regularre expression来确定搜索字符串是否存在。
编辑:这是完成工作的示例递归函数。它是一个控制台应用程序,因此可以根据您的需求进行调整:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace FileSearcher
{
class Program
{
static string searchValue = "Windows";
static List<FileInfo> files = new List<FileInfo>();
static void Main(string[] args)
{
string startFolder = @"C:\";
DirectoryInfo diParent = new DirectoryInfo(startFolder);
FindSearchValue(diParent);
foreach (var file in files)
{
Console.WriteLine("{0}", file.FullName);
}
Console.WriteLine("Press any key to continue...");
Console.ReadLine();
}
/// <summary>
/// Recursive function that searches for a file that matches the criteria.
/// If the file is not found, the current file is opened and it's contents is
/// scanned for search value.
/// </summary>
/// <param name="diParent">Current parent folder being searched.</param>
private static void FindSearchValue(DirectoryInfo diParent)
{
FileInfo[] foundFiles = diParent.GetFiles("*.doc");
foreach (var file in foundFiles)
{
Console.WriteLine(file.FullName);
if (file.FullName.Contains(searchValue)) // There is a search string in a file name
{
files.Add(file);
}
else
{
string fileContents = File.ReadAllText(file.FullName);
if (fileContents.Contains(searchValue)) // Here you can use Regex.IsMatch(fileContents, searchValue)
{
files.Add(file);
}
}
}
foreach (var diChild in diParent.GetDirectories())
{
try
{
FindSearchValue(diChild);
}
catch (Exception exc)
{
Console.WriteLine("ERROR: {0}", exc.Message);
}
}
}
}
}
此函数使用try-catch块拦截可能发生的异常。例如,找不到文件或拒绝访问。希望这有帮助!
答案 2 :(得分:0)
使用此递归函数来避免异常:
public static IEnumerable<string> GetFiles(string path, string pattern)
{
IEnumerable<string> result;
try
{
result = Directory.GetFiles(path, pattern);
}
catch (UnauthorizedAccessException)
{
result = new string[0];
}
IEnumerable<string> subDirectories;
try
{
subDirectories = Directory.EnumerateDirectories(path);
}
catch (UnauthorizedAccessException)
{
subDirectories = new string[0];
}
foreach (string subDirectory in subDirectories)
{
IEnumerable<string> subFiles = GetFiles(subDirectory, pattern);
result = result.Concat(subFiles); //This is LINQ concatenation
}
return result;
}
static void Main(string[] args)
{
string startFolder = @"c:\";
foreach (string fileName in GetFiles(startFolder, "*.chm"))
{
Console.WriteLine(fileName);
}