| | |
Can't read all the directories, interrupted by unauthorized exception
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 5
Reputation:
Solved Threads: 0
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
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
C# Syntax (Toggle Plain Text)
public TreeNode GetDepthTwoChildNodes(TreeNode parentNode) { parentNode.Nodes[0].Remove(); TreeNode Level1Dir; try { foreach (string dir1 in SubDirs(parentNode.FullPath)) { Level1Dir = new TreeNode(); Level1Dir.ImageIndex = 3; Level1Dir.SelectedImageIndex = 3; Level1Dir.Text = dir1.Substring(dir1.LastIndexOf('\\') + 1, dir1.Length - dir1.LastIndexOf('\\') - 1); foreach (string dir2 in SubDirs(dir1)) { TreeNode thisDir = new TreeNode(); thisDir.ImageIndex = 3; thisDir.SelectedImageIndex = 3; thisDir.Text = dir2.Substring(dir2.LastIndexOf('\\') + 1, dir2.Length - dir2.LastIndexOf('\\') - 1); thisDir.Nodes.Add(""); Level1Dir.Nodes.Add(thisDir); } foreach (string file2 in FileList(dir1)) { TreeNode Level2Files = new TreeNode(); Level2Files.ImageIndex = 4; Level2Files.SelectedImageIndex = 4; Level2Files.Text = file2.Substring(file2.LastIndexOf('\\') + 1, file2.Length - file2.LastIndexOf('\\') - 1); Level1Dir.Nodes.Add(Level2Files); } parentNode.Nodes.Add(Level1Dir); } } catch (UnauthorizedAccessException AUE) { TreeNode AccesDeniedDir = new TreeNode(); AccesDeniedDir.ImageIndex = 3; AccesDeniedDir.SelectedImageIndex = 3; AccesDeniedDir.Text = AUE.InnerException.Message.ToString(); parentNode.Nodes.Add(AccesDeniedDir); } foreach (string file1 in FileList(parentNode.FullPath)) { TreeNode Level1Files = new TreeNode(); Level1Files.ImageIndex = 4; Level1Files.SelectedImageIndex = 4; Level1Files.Text = file1.Substring(file1.LastIndexOf('\\') + 1, file1.Length - file1.LastIndexOf('\\') - 1); parentNode.Nodes.Add(Level1Files); } return parentNode; } private List<string> SubDirs(string drv) { List<string> mydirs = new List<string>(); try { foreach (string dirs in Directory.GetDirectories(drv)) { mydirs.Add(dirs); } } catch (IOException) { } return mydirs; } private List<string> FileList(string pathnme) { List<string> myFileList = new List<string>(); try { foreach (string myfile in Directory.GetFiles(pathnme)) { myFileList.Add(myfile); } } catch (IOException) { } catch (UnauthorizedAccessException) { } return myFileList; }
Last edited by cscgal; Oct 3rd, 2008 at 11:53 am. Reason: Fixed code tags
![]() |
Other Threads in the C# Forum
- Previous Thread: Patching problem
- Next Thread: C# Help! Arrays, ListBox's and Buttons
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access algorithm array barchart bitmap box buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees draganddrop drawing encryption enum event excel file files form format forms ftp function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






