在特定驱动器c#中搜索扩展名为.dcm的文件

本文关键字:扩展名 dcm 文件 搜索 驱动器 | 更新日期: 2023-09-27 18:05:11

我试图通过输入文件名或文件的内容来搜索扩展名。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);

在特定驱动器c#中搜索扩展名为.dcm的文件

尝试在驱动器中搜索,就像在文件夹中一样:

    string startFolder = @"c:'";
    DirectoryInfo directoryInfo = new DirectoryInfo(startFolder);
    IEnumerable<System.IO.FileInfo> fileList = directoryInfo.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

使用这个递归函数来避免异常:

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);
    }

您可以立即过滤掉所有带有*的文件。dcm扩展

string startFolder = @"c:'";
DirectoryInfo directoryInfo = new DirectoryInfo(startFolder);
IEnumerable<System.IO.FileInfo> fileList = directoryInfo.GetFiles("*.dcm", System.IO.SearchOption.AllDirectories);

之后,使用foreach循环查找具有所需名称的文件,或者打开文件进行读取并在文件中搜索值。

*。DCM文本文件还是二进制文件?如果它们是基于文本的文件,您可以使用正则表达式来确定搜索字符串是否存在。

编辑:下面是完成这项工作的示例递归函数。它是一个控制台应用程序,因此根据您的需要调整它:

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块来拦截可能发生的异常。例如,未找到文件或拒绝访问。希望这对你有帮助!