Results 1 to 5 of 5

Thread: Get all System Directories

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    91

    Get all System Directories

    Hi i want to search for files in every directory of system

    like in one system it will have C:,D: and E: directories

    and in another system it will have C:,D:,E: and F:


    so how can i search in whole system directories?


    Thanks and Regards
    Vinay kumar

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get all System Directories

    Here's skeleton code that will visit every accessible file on every hard drive on the current system:
    vb.net Code:
    1. Private Sub SearchComputer()
    2.     For Each drive As IO.DriveInfo In IO.DriveInfo.GetDrives()
    3.         If drive.DriveType = IO.DriveType.Fixed Then
    4.             SearchFolder(drive.Name)
    5.         End If
    6.     Next drive
    7. End Sub
    8.  
    9. Private Sub SearchFolder(ByVal folder As String)
    10.     Try
    11.         For Each subfolder As String In IO.Directory.GetDirectories(folder)
    12.             SearchFolder(subfolder)
    13.         Next subfolder
    14.  
    15.         For Each file As String In IO.Directory.GetFiles(folder)
    16.             'Do whatever is appropriate here with the file path.
    17.             Console.WriteLine(file)
    18.         Next file
    19.     Catch ex As UnauthorizedAccessException
    20.         'Ignore this folder as the user does not have permission to access it.
    21.     End Try
    22. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get all System Directories

    If you need a bit more info about the files and/or folders you mat choose to use DirectoryInfo and FileInfo objects instead of just path strings. Here's a variation on the code above that does that:
    vb.net Code:
    1. Private Sub SearchComputer()
    2.     For Each drive As IO.DriveInfo In IO.DriveInfo.GetDrives()
    3.         If drive.DriveType = IO.DriveType.Fixed Then
    4.             SearchFolder(drive.RootDirectory)
    5.         End If
    6.     Next drive
    7. End Sub
    8.  
    9. Private Sub SearchFolder(ByVal folder As IO.DirectoryInfo)
    10.     Try
    11.         For Each subfolder As IO.DirectoryInfo In folder.GetDirectories()
    12.             SearchFolder(subfolder)
    13.         Next subfolder
    14.  
    15.         For Each file As IO.FileInfo In folder.GetFiles()
    16.             'Do whatever is appropriate here with file info.
    17.             Console.WriteLine(file.FullName)
    18.         Next file
    19.     Catch ex As UnauthorizedAccessException
    20.         'Ignore this folder as the user does not have permission to access it.
    21.     End Try
    22. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get all System Directories

    Ah bummer! I just realised I'm in the C# forum. Here's the translations:
    C# Code:
    1. private void SearchComputer()
    2. {
    3.     foreach (System.IO.DriveInfo drive in System.IO.DriveInfo.GetDrives())
    4.     {
    5.         if (drive.DriveType == System.IO.DriveType.Fixed)
    6.         {
    7.             SearchFolder(drive.Name);
    8.         }
    9.     }
    10. }
    11.  
    12. private void SearchFolder(string folder)
    13. {
    14.     try
    15.     {
    16.         foreach (string subfolder in System.IO.Directory.GetDirectories(folder))
    17.         {
    18.             SearchFolder(subfolder);
    19.         }
    20.  
    21.         foreach (string file in System.IO.Directory.GetFiles(folder))
    22.         {
    23.             // Do whatever is appropriate here.
    24.             Console.WriteLine(file);
    25.         }
    26.     }
    27.     catch (UnauthorizedAccessException)
    28.     {
    29.         // Ignore this folder as the user doesn't have permission to access it.
    30.     }
    31. }
    C# Code:
    1. private void SearchComputer()
    2. {
    3.     foreach (System.IO.DriveInfo drive in System.IO.DriveInfo.GetDrives())
    4.     {
    5.         if (drive.DriveType == System.IO.DriveType.Fixed)
    6.         {
    7.             SearchFolder(drive.RootDirectory);
    8.         }
    9.     }
    10. }
    11.  
    12. private void SearchFolder(System.IO.DirectoryInfo folder)
    13. {
    14.     try
    15.     {
    16.         foreach (System.IO.DirectoryInfo subfolder in folder.GetDirectories())
    17.         {
    18.             SearchFolder(subfolder);
    19.         }
    20.  
    21.         foreach (System.IO.FileInfo file in folder.GetFiles())
    22.         {
    23.             // Do whatever is appropriate here.
    24.             Console.WriteLine(file.FullName);
    25.         }
    26.     }
    27.     catch (UnauthorizedAccessException)
    28.     {
    29.         // Ignore this folder as the user doesn't have permission to access it.
    30.     }
    31. }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    91

    Resolved Re: Get all System Directories

    Thanks for the reply


    its working. Thanks again


    Regards
    Vinay Kumar

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width