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

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;
        }

Recommended Answers

All 4 Replies

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

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.

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

thanks for the idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.