[英]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 驱动器的文件夹树,我认为这是可能的:
Let me know if you continued the research by my suggestion and what was the results:-)如果您按照我的建议继续研究,请告诉我,结果如何:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.