Solved

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

Posted on 2009-04-06
7
1,947 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:yamurthy
  • 3
  • 2
  • 2
7 Comments
 
LVL 6

Expert Comment

by:SaedSalman
ID: 24078617
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

0
 
LVL 1

Author Comment

by:yamurthy
ID: 24079627
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 :(
0
 
LVL 6

Expert Comment

by:SaedSalman
ID: 24079685
Do not know why accessing this folder ;-)
But here is what you are looking for:
http://support.microsoft.com/kb/309531
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Expert Comment

by:SaedSalman
ID: 24079725
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

0
 
LVL 6

Expert Comment

by:Cebik
ID: 24082749
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])
0
 
LVL 6

Accepted Solution

by:
Cebik earned 250 total points
ID: 24082853
this one will work good

namespace ConsoleApplication1

{

    class Program

    {

        public static void Main(string[] args)

        {

            ArrayList items = new ArrayList();
 

            // Modify the path to work on your machine

            FindFile(@"X:\",

             "*.cs", items);
 

            Console.WriteLine("Files found - {0}", items.Count);

            foreach (FoundFile file in items)

                Console.WriteLine("File {0} - Full Path {1}", file.Name, file.FullPath);

        }
 

        public static void FindFile(string dir, string file, ArrayList items)

        {

            DirectoryInfo di = new DirectoryInfo(dir);

            try

            {

                DirectoryInfo[] subs = di.GetDirectories();
 

                foreach (DirectoryInfo sub in subs)

                    FindFile(sub.FullName, file, items);
 

                FileInfo[] files = di.GetFiles(file);

                foreach (FileInfo info in files)

                    items.Add(new FoundFile(info.Name, info.FullName));

            }

            catch (UnauthorizedAccessException)

            {

            }

        }
 

    }
 

    public struct FoundFile

    {

        private readonly string mName;

        private readonly string mFullPath;
 

        public string Name

        {

            get { return mName; }

        }
 

        public string FullPath

        {

            get { return mFullPath; }

        }
 

        public FoundFile(string name, string fullPath)

        {

            mName = name;

            mFullPath = fullPath;

        }

    }

}

Open in new window

0
 
LVL 1

Author Closing Comment

by:yamurthy
ID: 31567050
Thanks a lot. works fine.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now