简体   繁体   English

通过 c# 读取 C 驱动器中的所有文件和文件夹

[英]Read all files and folders in C drive via c#

I want to get all files and folders in Drive C.我想获取驱动器 C 中的所有文件和文件夹。 In fact, I want a list of all the files on the drive.事实上,我想要一个驱动器上所有文件的列表。 All the files along with their path.所有文件及其路径。 I use this code.but encounters an error.我使用此代码。但遇到错误。

static void Main(string[] args)
    {
       
        System.IO.DriveInfo di = new System.IO.DriveInfo(@"C:\");
        System.IO.DirectoryInfo dirInfo = di.RootDirectory;

        System.IO.FileInfo[] fileNames = dirInfo.GetFiles("*.*");
        System.IO.DirectoryInfo[] dirInfos = dirInfo.GetDirectories("*.*");

        foreach (System.IO.DirectoryInfo d in dirInfos)
        {
            string[] filePaths = Directory.GetFiles(d.FullName, "*.*", 
            SearchOption.AllDirectories);
        }
    }

在此处输入图像描述

You can simply exclude directories that you can't access by searching them one by one and surrounding all of the searches with a try-catch block.您可以通过逐一搜索并使用 try-catch 块包围所有搜索来简单地排除您无法访问的目录。 Here is an example:这是一个例子:

Console.WriteLine("Input search pattern (or empty to search all):");
        string pattern = Console.ReadLine();
        if (pattern == "")
        {
            pattern = "*";
        }
        List<string> allDirectories = new List<string>{ @"C:\" });
        Stack<string> directories = new Stack<string>(allDirectories);
        List<string[]> allFiles = new List<string[]>();
        while (directories.Count > 0)
        {
            try
            {
                Console.WriteLine("Searching " + directories.Peek() + " for " + pattern);
                foreach (string dir in Directory.GetDirectories(directories.Pop()))
                {
                    directories.Push(dir);
                    allDirectories.Add(dir);
                    try
                    {
                        allFiles.Add(Directory.GetFiles(dir, pattern, SearchOption.TopDirectoryOnly));
                    }
                    catch (UnauthorizedAccessException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }
            catch (UnauthorizedAccessException e)
            {
                Console.WriteLine(e.Message);
            }
        }
        Console.WriteLine("FINISHED");

This will collect all files into the allFiles list (as paths) and directories into the allDirectories list.这会将所有文件收集到 allFiles 列表中(作为路径),并将目录收集到 allDirectories 列表中。 This runs for ~10 minutes for me, so don't debug to many times.这对我来说运行了大约 10 分钟,所以不要调试很多次。

First thing you need to do is compiling your C# app with a manifest file that asks for root privileges (follow instructions from: How do I force my .NET application to run as administrator? ).您需要做的第一件事是使用要求 root 权限的清单文件编译 C# 应用程序(按照以下说明操作: 如何强制我的 .NET 应用程序以管理员身份运行? )。 Next, What you should do is achieve any kind of a user/running-app with admin permissions and let it start your C# app.接下来,您应该做的是获得具有管理员权限的任何类型的用户/运行应用程序,并让它启动您的 C# 应用程序。 I think that if you'll do the above via an app with root privileges then no UAC will pop-up to the user when the C# app will start.我认为,如果您通过具有 root 权限的应用程序执行上述操作,那么当 C# 应用程序启动时,不会向用户弹出 UAC。

If your app don't have root permission you won't be able to read the directory tree of unauthorized folders.如果您的应用没有 root 权限,您将无法读取未经授权文件夹的目录树。 C# is a managed language, which means C# counts on the operating system to run and for that reason it can't bypass the operating system. C# 是一种托管语言,这意味着 C# 依靠操作系统运行,因此它不能绕过操作系统。 Even with root permission, the operating system will be aware of your app actions.即使具有 root 权限,操作系统也会知道您的应用程序操作。

BUT if your target is to figure out if a C# dll can maliciously read the folder tree of C drive, I think it's possible:但是,如果您的目标是确定 C# dll 是否可以恶意读取 C 驱动器的文件夹树,我认为这是可能的:

  1. You need to compile your C# code into a exe file with a manifest as I've described above.如上所述,您需要将 C# 代码编译为带有清单的 exe 文件。
  2. Then, create a batch file that will start a CLR process with root privileges (it'll probably alert the user with a UAC prompt but a common user won't suspect the CLR).然后,创建一个批处理文件,它将以 root 权限启动 CLR 进程(它可能会通过 UAC 提示提醒用户,但普通用户不会怀疑 CLR)。
  3. Make sure your batch will run with the user privileges and not the default ones or the next step won't work.确保您的批处理将以用户权限而不是默认权限运行,否则下一步将无法工作。
  4. Your batch should tell the clr to load C# exe and I believe either no UAC will be prompted or either the batch could accept on behalves of the user without any prompt.您的批次应该告诉 clr 加载 C# exe,我相信要么不会提示 UAC,要么批次可以在没有任何提示的情况下代表用户接受。 4'. 4'。 If I'm wrong, perhaps the article https://www.codeproject.com/Articles/607352/Injecting-NET-Assemblies-Into-Unmanaged-Processes#PuttingItAllTogether will help you inject the exe into the clr without a UAC prompt.如果我错了,也许文章https://www.codeproject.com/Articles/607352/Injecting-NET-Assemblies-Into-Unmanaged-Processes#PuttingItAllTogether将帮助您在没有 UAC 提示的情况下将 exe 注入 clr。

Let me know if you continued the research by my suggestion and what was the results:-)如果您按照我的建议继续研究,请告诉我,结果如何:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何列出硬盘驱动器上的所有文件和文件夹? - c# - How to list all files and folders on a hard drive? 如何从C#中的Google Drive API从所有文件夹中获取所有文件 - How to get all files from all folders from Google Drive API in C# 按大小C#对所有文件和文件夹进行排序 - Sorting all files and folders by size C# 通过c#中的api从sharepoint获取文件/文件夹 - Getting files/folders from sharepoint via the api in c# C#删除一个文件夹以及该文件夹内的所有文件和文件夹 - C# delete a folder and all files and folders within that folder C#中的SevenZipSharp具有所有路径文件夹的压缩文件 - SevenZipSharp in C# Compress files with all path folders 如何删除所有文件/文件夹但将根文件夹保留在 C# 中 - How to delete all files/folders but keep the root folder in C# Opentext ECM - 使用 C# GUI 列出所有文件夹和文件 - Opentext ECM - List all Folders and Files with C# GUI 如何使用 UWP C# 备份 USB 中的所有文件夹和文件? - How to Backup all the folders and files in a USB using UWP C#? 如何通过C#中的WMI根据不同的扩展名查找C驱动器的文件和子文件夹中的文件路径列表 - How to find list of file path in files and in sub folders of C Drive based on different extension through WMI in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM