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
Printable View
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
Here's skeleton code that will visit every accessible file on every hard drive on the current system:vb.net Code:
Private Sub SearchComputer() For Each drive As IO.DriveInfo In IO.DriveInfo.GetDrives() If drive.DriveType = IO.DriveType.Fixed Then SearchFolder(drive.Name) End If Next drive End Sub Private Sub SearchFolder(ByVal folder As String) Try For Each subfolder As String In IO.Directory.GetDirectories(folder) SearchFolder(subfolder) Next subfolder For Each file As String In IO.Directory.GetFiles(folder) 'Do whatever is appropriate here with the file path. Console.WriteLine(file) Next file Catch ex As UnauthorizedAccessException 'Ignore this folder as the user does not have permission to access it. End Try End Sub
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:
Private Sub SearchComputer() For Each drive As IO.DriveInfo In IO.DriveInfo.GetDrives() If drive.DriveType = IO.DriveType.Fixed Then SearchFolder(drive.RootDirectory) End If Next drive End Sub Private Sub SearchFolder(ByVal folder As IO.DirectoryInfo) Try For Each subfolder As IO.DirectoryInfo In folder.GetDirectories() SearchFolder(subfolder) Next subfolder For Each file As IO.FileInfo In folder.GetFiles() 'Do whatever is appropriate here with file info. Console.WriteLine(file.FullName) Next file Catch ex As UnauthorizedAccessException 'Ignore this folder as the user does not have permission to access it. End Try End Sub
Ah bummer! I just realised I'm in the C# forum. Here's the translations:C# Code:
private void SearchComputer() { foreach (System.IO.DriveInfo drive in System.IO.DriveInfo.GetDrives()) { if (drive.DriveType == System.IO.DriveType.Fixed) { SearchFolder(drive.Name); } } } private void SearchFolder(string folder) { try { foreach (string subfolder in System.IO.Directory.GetDirectories(folder)) { SearchFolder(subfolder); } foreach (string file in System.IO.Directory.GetFiles(folder)) { // Do whatever is appropriate here. Console.WriteLine(file); } } catch (UnauthorizedAccessException) { // Ignore this folder as the user doesn't have permission to access it. } }C# Code:
private void SearchComputer() { foreach (System.IO.DriveInfo drive in System.IO.DriveInfo.GetDrives()) { if (drive.DriveType == System.IO.DriveType.Fixed) { SearchFolder(drive.RootDirectory); } } } private void SearchFolder(System.IO.DirectoryInfo folder) { try { foreach (System.IO.DirectoryInfo subfolder in folder.GetDirectories()) { SearchFolder(subfolder); } foreach (System.IO.FileInfo file in folder.GetFiles()) { // Do whatever is appropriate here. Console.WriteLine(file.FullName); } } catch (UnauthorizedAccessException) { // Ignore this folder as the user doesn't have permission to access it. } }
Thanks for the reply
its working. Thanks again
Regards
Vinay Kumar