Returning to execution after exception?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Phat Twat
    New Member
    • Jul 2009
    • 1

    Returning to execution after exception?

    Hi folks, this i probably rather simple, but i just cant to figure out how to make it work :/

    i have this, tiny tiny piece of code, which im actually rather proud of :b

    Code:
                try
                {
                    string[] folders = Directory.GetDirectories(@"C:\\", "*", SearchOption.AllDirectories);
                }
                catch (UnauthorizedAccessException)
                {
                    Console.WriteLine("Skipping folder....");
                }
    I thought it was clever atleast....

    To sum it up, i created that little search thingie to find all folders, and subfolders on the C: drive. But everytime i tried to run, it threw an exception, saying that i did not have acces to System Volume Information. I read up on what the heck it was, and turned out not even administrators have acces to this folder. Oh well, then i tried to create that catch, and hoped i had solved the problem.

    But after the "catch" the program stops running, what i really wanted it to do was to skip inaccesible folders.... any clue on how i could do this ?

    Thanks a lot !

    Ps. oh yes, i have tried to make this work myself.... for quite a couple of hours....
  • IanWright
    New Member
    • Jan 2008
    • 179

    #2
    Firstly, why do you want to do this? It effects the answer to your problem quite dramatically.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      But after the "catch" the program stops running
      Of course it does... the one active line of code fails... gets caught... execution falls through to the next statement after the Catch. That's proper behavior.

      what i really wanted it to do was to skip inaccesible folders....
      Getting directory contents is a common need for a lot of programs. There are lots of examples of this around the C# tutorial sites and I know it falls pretty early in most of my "Learn C#" text books. You'll have to do it in recursive steps, handling each folder individually. Most don't do it by just dumping all the paths into one big array. Instead a TreeView handles it rather nicely because you can more closely mimic the hard drive structure. A Node contains other nodes, just a folder contains other folders.

      Code:
      Nodes of a treeview
      + C:
        |- Level  1
        |-   |- Level 1.1
        |-   |-   |- Level 1.1.1
        |-   |- Level 1.2
        |-   |- Level 1.3
        |- Level 2
      
      string[]
      C: Level 1
      C: Level 1\Level 1.1\
      C: Level 1\Level 1.1\Level 1.1.1
      C: Level 1\Level 1.2\
      C: Level 1\Level 1.3\
      C: Level 2
      A quick Google for the terms "C# directory treeview" got a number of good hits. Here's the code from one as a sample of what I was talking about.

      Code:
      /*
       * A recursive method to populate a TreeView
       * Author: Danny Battison
       * Contact: gabehabe@googlemail.com
       */
      
      /// <summary>
      /// A method to populate a TreeView with directories, subdirectories, etc
      /// </summary>
      /// <param name="dir">The path of the directory</param>
      /// <param name="node">The "master" node, to populate</param>
      public void PopulateTree(string dir, TreeNode node) {
      	// get the information of the directory
      	DirectoryInfo directory = new DirectoryInfo(dir);
      	// loop through each subdirectory
      	foreach(DirectoryInfo d in directory.GetDirectories()) {
      		// create a new node
      		TreeNode t = new TreeNode(d.Name);
      		// populate the new node recursively
      		PopulateTree(d.FullName, t);
      		node.Nodes.Add(t); // add the node to the "master" node
      	} 
      	// lastly, loop through each file in the directory, and add these as nodes
      	foreach(FileInfo f in directory.GetFiles()) {
      		// create a new node
      		TreeNode t = new TreeNode(f.Name);
      		// add it to the "master"
      		node.Nodes.Add(t);
      	}
      }

      Comment

      Working...