Click here to Skip to main content
15,891,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello sir... may i ask how can i retrieve a lists of directories using c#? i was able to build my treeview and retrieve all the files and folders in my flash drive but when i try to retrieve all files in drive c: or drive d: it gives me an error since i was trying to open a restricted folder such as system volume and other folder that has a restriction to open... when i loop and when i encounter that kind of folder i will just skip it and not retrieve the folder name is that possible? please help me with this... thanks in advance...

I have this Error when i try to run my code of retrieving the folder this are the error
A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Access to the path 'D:\$RECYCLE.BIN\S-1-5-21-3717064836-2505550092-2449041842-1014\' is denied.
Access to the path 'D:\ae1a6083e25e839c285e48eaaff1ee\' is denied.
A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Access to the path 'D:\System Volume Information\' is denied.


this is the code for listing all folder

public void listDirectories(string path, TreeNode m) {
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
            TreeNode Main = m;
            Main.Tag = "";
            foreach (System.IO.DirectoryInfo g in dir.GetDirectories())
            {
                
                try
                {
                    //LOAD FOLDERS
                    TreeNode MainNext = Main.Nodes.Add(g.Name);
                    MainNext.Tag = (g.Name);
                    listDirectories(g.FullName, MainNext);
                }catch(Exception e){
                    MessageBox.Show("Error in folders "  +  e.Message);
                    Console.WriteLine(e.Message);

                }
                
            } 
        
        }


This will list the files inside each folders

public void listFiles(string location) {
       //     MessageBox.Show(location);
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(location);
            try
            {
                foreach (System.IO.FileInfo f in dir.GetFiles("*.*"))
                {
                    try
                    {
                        //LOAD FILES
                        ListViewItem lSingleItem = listView1.Items.Add(f.Name);
                        //SUB ITEMS
                        lSingleItem.SubItems.Add(Convert.ToString(f.Length));
                        lSingleItem.SubItems.Add(f.Extension);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Error in files " + e.Message);
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error in files " + e.Message);
                Console.WriteLine(e.Message);
            }
        
        }
Posted
Updated 22-Aug-11 20:03pm
v4
Comments
walterhevedeich 23-Aug-11 1:22am    
If you can provide the exact error and the relevant code, we can probably understand better what you are trying to achieve.

You are trying to read all the file locations - however you do not have access to all of them.
See here[^].
 
Share this answer
 
yes i know that and i found out how to exclude all the unwanted folders specially the hidden one... but for some reason it includes this file
ae1a6083e25e839c285e48eaaff1ee
and based from its attributes it does not have hidden,read,system,compressed attributes now i dont know why i cant open it... im doing this checking if the folder is valid to be included
<pre>public bool isValidDirectory(string path) {
            bool isvalid = true;

            bool isReadOnly = ((File.GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);

            // check whether a file is hidden
            bool isHidden = ((File.GetAttributes(path) & FileAttributes.Hidden) == FileAttributes.Hidden);

            // check whether a file has archive attribute
            bool isArchive = ((File.GetAttributes(path) & FileAttributes.Archive) == FileAttributes.Archive);

            // check whether a file is system file
            bool isSystem = ((File.GetAttributes(path) & FileAttributes.System) == FileAttributes.System);

            bool isEncryted = ((File.GetAttributes(path) & FileAttributes.Encrypted ) == FileAttributes.Encrypted);

            bool isCompressed = ((File.GetAttributes(path) & FileAttributes.Compressed) == FileAttributes.Compressed);

            bool isNotContentIndexed = ((File.GetAttributes(path) & FileAttributes.NotContentIndexed) == FileAttributes.NotContentIndexed);

            bool isSparseFile = ((File.GetAttributes(path) & FileAttributes.SparseFile) == FileAttributes.SparseFile);
            bool isTemporary = ((File.GetAttributes(path) & FileAttributes.Temporary) == FileAttributes.Temporary);
            bool isNormal = ((File.GetAttributes(path) & FileAttributes.Normal) == FileAttributes.Normal);

            if (isReadOnly || isHidden || isArchive || isSystem || isEncryted || isCompressed || isNotContentIndexed || isSparseFile || isTemporary)
            {
                if (!isNormal)
                {
                    isvalid = false;

                }


            }
           

            return isvalid;
        
        
        }





but it still lists the folder and when i try to open it, it gives me an error i dont know how to solve this... i dont know how to exclude this type of file... is there a way to find out if i can "OPEN" the file?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900