fihovi
Member
- Joined
- Nov 26, 2019
- Messages
- 7
- Programming Experience
- Beginner
Hello,
I want to scan drive in my app, but I can't scan $Recyclebin and folders with whitespace in name.
I can't find any resolution for these errors, I tried to find NuGet packages for resolving this issues for me.
I'm using System.IO
Thanks a lot
I want to scan drive in my app, but I can't scan $Recyclebin and folders with whitespace in name.
I can't find any resolution for these errors, I tried to find NuGet packages for resolving this issues for me.
I'm using System.IO
Program.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace FileHandler
{
class Program
{
private static void Main(string[] args)
{
GetAllFilesFromFolder(@"E:\", true);
}
private static List<string> GetAllFilesFromFolder(string root, bool searchSubfolders)
{
Queue<string> folders = new Queue<string>();
List<string> folderCount = new List<string>();
List<string> files = new List<string>();
folders.Enqueue(root);
while (folders.Count != 0){
string currentFolder = folders.Dequeue();
try {
string[] filesInCurrent = Directory.GetFiles(currentFolder, "*.*", System.IO.SearchOption.TopDirectoryOnly);
files.AddRange(filesInCurrent);
}
catch
{
//Console.WriteLine("Error: " + currentFolder);
// Do Nothing
}
try{
if (searchSubfolders){
string[] foldersInCurrent = Directory.GetDirectories(currentFolder, "*.*", System.IO.SearchOption.TopDirectoryOnly);
foreach (string _current in foldersInCurrent){
folderCount.AddRange(foldersInCurrent);
folders.Enqueue(_current);
}
}
}
catch{
Console.WriteLine("Error: " + currentFolder);
// Do Nothing
}
}
countFiles = files.Count();
List<string> distinct = folderCount.Distinct().ToList(); //Remove Duplicates from scan
Console.WriteLine("Number of folders AFTER: " + distinct.Count);
Console.WriteLine("Number of files is: " + files.Count());
Console.ReadLine();
return files;
}
}
}
Thanks a lot
Last edited: