0

I have below code to retrieve all pdf files from MyComputer. But i am getting error like below. Is it possible to retrive all pdf files from one computer using C# code.

string path = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);            
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path); // Error : The path is not of a legal form.
IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.pdf", System.IO.SearchOption.AllDirectories);
amit_g
  • 30,880
  • 8
  • 61
  • 118
user3366358
  • 23
  • 1
  • 6
  • What is the value of path when it's passed to the `DirectoryInfo` constructor? – evanmcdonnal Mar 06 '14 at 19:17
  • 1
    The error says just that, you are specifying an illegal path. – user959631 Mar 06 '14 at 19:17
  • 1
    You need to look in all drives (see http://stackoverflow.com/questions/781905/getting-a-list-of-logical-drives), and recursively within subfolders – Julián Urbano Mar 06 '14 at 19:18
  • I am setting path as MyComputer folder/directory. string path = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer); – user3366358 Mar 06 '14 at 19:18
  • path is coming as "". So it thorws error. Why path is coming as empty to me? – user3366358 Mar 06 '14 at 19:22
  • [The My Computer folder](http://msdn.microsoft.com/en-us/library/system.environment.specialfolder%28v=vs.110%29.aspx) The MyComputer constant always yields the empty string ("") because no path is defined for the My Computer folder. – amit_g Mar 06 '14 at 19:23

2 Answers2

9

You can get all drives and then get all files.

EDIT: You can also use Directory.EnumerateFiles method which would let you get the file path and you can add that in your list. This would give you a List<string> for all the file paths. Like:

List<string> filePathList = new List<string>();
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
    try
    {
        var filenames = Directory.EnumerateFiles(drive.Name, "*.pdf", SearchOption.AllDirectories);
        foreach (string fileName in filenames)
        {
            filePathList.Add(fileName);
        }
    }
    catch (FieldAccessException ex)
    {

        //Log, handle Exception
    }
    catch (UnauthorizedAccessException ex)
    {
        //Log, handle Exception
    }
    catch (Exception ex)
    {
        //log , handle all other exceptions
    }
}

Old Answer.

List<FileInfo> fileList = new List<FileInfo>();
foreach (var drive in System.IO.DriveInfo.GetDrives())
{
    try
    {
        DirectoryInfo dirInfo = new DirectoryInfo(drive.Name);
        foreach (var file in dirInfo.GetFiles("*.pdf", SearchOption.AllDirectories))
            fileList.Add(file);

    }
    catch (FieldAccessException ex)
    {

        //Log, handle Exception
    }
    catch (UnauthorizedAccessException ex)
    {
        //Log, handle Exception
    }
    catch (Exception ex)
    {
        //log , handle all other exceptions
    }
}
Habib
  • 219,104
  • 29
  • 407
  • 436
  • I have tried your code but getting error. Error : "Access to the path 'C:\$Recycle.Bin\blahblah...\' is denied" – user3366358 Mar 06 '14 at 19:28
  • @user3366358, Modified with respect to that error, check the edited portion of the answer. – Habib Mar 06 '14 at 19:44
  • I am getting the below error for your old answer. After this error, the code skip to search in my C Drive. System.IO.Exception The device is not ready. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) – user3366358 Mar 06 '14 at 19:46
  • 1
    @user3366358, you can filter your drives on [DriveInfo.IsReady](http://msdn.microsoft.com/en-us/library/system.io.driveinfo.isready(v=vs.110).aspx) property – Habib Mar 06 '14 at 19:48
  • this is working perfect. I have another question,if i want to fetch pdfs for a particular searchterm in this list. This all drive search is taking too much of time. I have used WMI SystemIndex and it sometimes not returns the instantly added files. – user3366358 Mar 06 '14 at 20:46
  • @user3366358, I am not sure about WMI SystemIndex and this should be out of scope with the current question. – Habib Mar 06 '14 at 20:54
  • Okay. Still i am getting UnauthorizedAccessException "Access to the path 'C:\$Recycle.Bin\blahblah...\' is denied" with my C drive after applying DriveInfo.IsReady property but i could see drive.isready=true :( Kindly help – user3366358 Mar 06 '14 at 21:02
  • Do you have any answer for my last question? Thanks for your help in advance. – user3366358 Mar 06 '14 at 22:04
0

You can use the System.IO.DriveInfo class to loop through all drives available on the machine (call DriveInfo.GetDrives() to get a list of all drives). You will probably have to do this for each drive and combine the results from all of them. My guess with what is wrong with your current code is that giving it the MyComputer folder isn't enough to tell it to loop through all drives.

str8killinit
  • 167
  • 7