Link to home
Start Free TrialLog in
Avatar of Anjeneya Murthy
Anjeneya MurthyFlag for India

asked on

Issue in accesing the drive using Directory.GetFiles in C#

HI,
I am writing a program to find all the files with the specified filter. The program works fine if i give path to some folder, but throws an exception if i pass the drive letter as parameter. Can you please help me resolve this problem?
I am getting error UnauthorizedAccessException: Access to the path 'E:\System Volume Information' is denied.
string[] FileNames = Directory.GetFiles("E:\\","*.chm", SearchOption.AllDirectories);            
            foreach (string FullPath in FileNames)
            {                
                FileInfo file = new FileInfo(FullPath);
                string Path = file.DirectoryName;
                string FileName = file.Name;
                checkedListBox1.Items.Add(FileName);
            }

Open in new window

Avatar of SaedSalman
SaedSalman
Flag of Jordan image

you do not have access to "System Volume Information"
Solution:
except "System Volume Information" from your code.
Or, to avide each and every inaccessable file: you have to you use try catch block as follows:

try
{
string[] FileNames = Directory.GetFiles("E:\\","*.chm", SearchOption.AllDirectories);            
            foreach (string FullPath in FileNames)
            {                
                FileInfo file = new FileInfo(FullPath);
                string Path = file.DirectoryName;
                string FileName = file.Name;
                checkedListBox1.Items.Add(FileName);
            }
}
catch(UnauthorizedAccessException){//do nothing}

Open in new window

Avatar of Anjeneya Murthy

ASKER

i did use try and catch to avoid the exception. but my criteria will not be solved. i want to gain access to the drive so that i want to search all the files and folders in that drive.
i want to somehow get access. i have admin privilages in my system, but still unable to do this :(
Do not know why accessing this folder ;-)
But here is what you are looking for:
http://support.microsoft.com/kb/309531
this will solve "System Volume Information" problem
string[] FileNames = Directory.GetFiles("E:\\","*.chm", SearchOption.AllDirectories);            
            foreach (string FullPath in FileNames)
            {                
                FileInfo file = new FileInfo(FullPath);
                string Path = file.DirectoryName;
                string FileName = file.Name;
                if(FileName == "System Volume Information")   //Except System Volume Information folder
                       continue;
                checkedListBox1.Items.Add(FileName);
            }

Open in new window

Avatar of Cebik
this don't fix the problem..
exception will throw in 1 line..
(not if you change access to directory but it isn't good idea [http://support.microsoft.com/kb/309531])
ASKER CERTIFIED SOLUTION
Avatar of Cebik
Cebik
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lot. works fine.