-1

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;
        }
    }
}
Community
  • 1
  • 1
Man in Black
  • 89
  • 2
  • 2
  • 12
  • 2
    What happens when you step through in the debugger? Where does the behavior differ from what you expect? – David Oct 09 '15 at 01:03
  • @David if I enter a folder that contains subfolders and files it works.but if the folder does not contain subfolders or does not contain files then it just go out of the try but not going in to the catch – Man in Black Oct 09 '15 at 12:33
  • If it's working then what's the problem? – David Oct 09 '15 at 12:34
  • @david I pressed enter by mistake. I edited it now – Man in Black Oct 09 '15 at 12:34
  • Why do you expect an exception to be thrown? According to the documentation for `GetDirectories()` it isn't expected to throw an exception on a valid directory which happens to contain no sub-directories. `catch` blocks are only entered when an exception is caught. It's still not clear what the problem is here or what sort of unexpected behavior you're seeing in the code. – David Oct 09 '15 at 12:37
  • well thats a little bit awkward. I could swear I saw an example code with these[those?] try and catch. After rereading the getfiles and getdirectories methods I see you are right and there is no such exception . The problem is that I thought that if a directory is empty I will get 2 exceptions - one for no subfolders in the directory and one for no files in the directory. thank you for your help – Man in Black Oct 09 '15 at 13:01

1 Answers1

0

Add Console.ReadKey(); at the end of your Main method. So, application will not close after executing.

Backs
  • 24,430
  • 5
  • 58
  • 85