Can't read all the directories, interrupted by unauthorized exception

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 5
Reputation: potatskie is an unknown quantity at this point 
Solved Threads: 0
potatskie potatskie is offline Offline
Newbie Poster

Can't read all the directories, interrupted by unauthorized exception

 
0
  #1
Oct 2nd, 2008
hi. i'm new to this site. so i somehow not that familiar with the rules in posting so please consider if made some flaws here.

i'm currently developing a software that involves reading the the file system. i'm using C# which i'm still new to it. the software involves reading all the drives, directories and files and place it visually on a treeview. i'm using a foreach loop to go over the list of directories of a given drive but then, the foreach loop stops reading the remaining directories if it encounters an Unauthorized exception. what i want to happen, is for the code to skip those directories that are not supposed be to be accessed or read and proceed reading the remaining directories. example if i read drive C: of my machine which contains the directories A, B, C, D, E, G, H, I ,J and given that G is a System Volume Information directory. My code will only read from directory A to G and stops because of Unauthorized exception. What i want is read all the directories and just mark G as a directory that is protected.

here is my code

  1. public TreeNode GetDepthTwoChildNodes(TreeNode parentNode)
  2. {
  3. parentNode.Nodes[0].Remove();
  4.  
  5. TreeNode Level1Dir;
  6.  
  7. try
  8. {
  9. foreach (string dir1 in SubDirs(parentNode.FullPath))
  10. {
  11. Level1Dir = new TreeNode();
  12.  
  13. Level1Dir.ImageIndex = 3;
  14. Level1Dir.SelectedImageIndex = 3;
  15. Level1Dir.Text = dir1.Substring(dir1.LastIndexOf('\\') + 1, dir1.Length - dir1.LastIndexOf('\\') - 1);
  16.  
  17.  
  18. foreach (string dir2 in SubDirs(dir1))
  19. {
  20. TreeNode thisDir = new TreeNode();
  21. thisDir.ImageIndex = 3;
  22. thisDir.SelectedImageIndex = 3;
  23. thisDir.Text = dir2.Substring(dir2.LastIndexOf('\\') + 1, dir2.Length - dir2.LastIndexOf('\\') - 1);
  24. thisDir.Nodes.Add("");
  25. Level1Dir.Nodes.Add(thisDir);
  26. }
  27.  
  28. foreach (string file2 in FileList(dir1))
  29. {
  30. TreeNode Level2Files = new TreeNode();
  31. Level2Files.ImageIndex = 4;
  32. Level2Files.SelectedImageIndex = 4;
  33. Level2Files.Text = file2.Substring(file2.LastIndexOf('\\') + 1, file2.Length - file2.LastIndexOf('\\') - 1);
  34. Level1Dir.Nodes.Add(Level2Files);
  35. }
  36.  
  37. parentNode.Nodes.Add(Level1Dir);
  38. }
  39. }
  40. catch (UnauthorizedAccessException AUE)
  41. {
  42. TreeNode AccesDeniedDir = new TreeNode();
  43.  
  44. AccesDeniedDir.ImageIndex = 3;
  45. AccesDeniedDir.SelectedImageIndex = 3;
  46. AccesDeniedDir.Text = AUE.InnerException.Message.ToString();
  47. parentNode.Nodes.Add(AccesDeniedDir);
  48. }
  49.  
  50. foreach (string file1 in FileList(parentNode.FullPath))
  51. {
  52. TreeNode Level1Files = new TreeNode();
  53. Level1Files.ImageIndex = 4;
  54. Level1Files.SelectedImageIndex = 4;
  55. Level1Files.Text = file1.Substring(file1.LastIndexOf('\\') + 1, file1.Length - file1.LastIndexOf('\\') - 1);
  56. parentNode.Nodes.Add(Level1Files);
  57. }
  58.  
  59. return parentNode;
  60. }
  61. private List<string> SubDirs(string drv)
  62. {
  63. List<string> mydirs = new List<string>();
  64.  
  65. try
  66. {
  67. foreach (string dirs in Directory.GetDirectories(drv))
  68. {
  69. mydirs.Add(dirs);
  70. }
  71. }
  72. catch (IOException) { }
  73.  
  74. return mydirs;
  75. }
  76.  
  77. private List<string> FileList(string pathnme)
  78. {
  79. List<string> myFileList = new List<string>();
  80.  
  81. try
  82. {
  83. foreach (string myfile in Directory.GetFiles(pathnme))
  84. {
  85. myFileList.Add(myfile);
  86. }
  87. }
  88. catch (IOException) { }
  89.  
  90. catch (UnauthorizedAccessException) { }
  91.  
  92. return myFileList;
  93. }
Last edited by cscgal; Oct 3rd, 2008 at 11:53 am. Reason: Fixed code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Can't read all the directories, interrupted by unauthorized exception

 
0
  #2
Oct 3rd, 2008
Problem is you havent told it what to do about the exception, you need to tell it to do something.. and you then need to tell it to continue.. Without that, its doing exactly what you've told it to do
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 5
Reputation: potatskie is an unknown quantity at this point 
Solved Threads: 0
potatskie potatskie is offline Offline
Newbie Poster

Re: Can't read all the directories, interrupted by unauthorized exception

 
0
  #3
Oct 3rd, 2008
yes, you're right. but how i make it to continue, what's the code. can you help me please. i'm new to c# and oop.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Can't read all the directories, interrupted by unauthorized exception

 
0
  #4
Oct 3rd, 2008
Well your current code reads

while not erroring or end of list do stuff

You need to rework that logic. Im not going to code it for you
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 5
Reputation: potatskie is an unknown quantity at this point 
Solved Threads: 0
potatskie potatskie is offline Offline
Newbie Poster

Re: Can't read all the directories, interrupted by unauthorized exception

 
0
  #5
Oct 6th, 2008
thanks for the idea.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC