I am trying to build a program will show all files and directories in a given directory , and the size of each item . for the folders size I used https://stackoverflow.com/a/2981241/4645644 as it seems nice and understandable for me. I noticed that when I try to use GetFiles() or GetDirecories() I get exception if there is none exist . I tried to write to console when this happens but nothing is written to the console yet it doesnt do the try part and I don't understand what is happening or what I missed.
public static void Main(string[] args)
{
Console.WriteLine("write path folder");
string path = Console.ReadLine();
DirectoryInfo di = new DirectoryInfo(@path);
//int check=1;
bool iterating = true;
if (!Directory.Exists(path))
{
Console.WriteLine("{0} not found , path is wrong or there is no such directory", path);
}
else
{
while (iterating)
{
Console.WriteLine("Name,Root,Parent -> {0},{1},{2}", di.Name, di.Root, di.Parent);
Console.WriteLine("{0} full size is : {1}", di.Name, DirSize(di));
try
{
foreach (DirectoryInfo sfolder in di.GetDirectories())
{
Console.WriteLine("Folder Name: {0} , Folder size - {1} KB", sfolder.Name, DirSize(sfolder));
}
}
catch
{
Console.WriteLine("No subfolder in thie folder : {0}", di.FullName);
}
try
{
foreach (FileInfo sfile in di.GetFiles())
{
Console.WriteLine("File name : {0} , File size - {1} KB", sfile.Name, sfile.Length);
}
}
catch
{
Console.WriteLine("No files in thie folder : {0}", di.FullName);
}
iterating = false;
}
}
}