Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Searching an entire hard drive for a file

Searching an entire hard drive for a file

Scheduled Pinned Locked Moved C#
algorithmsquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kyledunn
    wrote on last edited by
    #1

    Anyone have the code worked out to scan every file in every directory on a hard drive to locate a single file possibly using DirectoryInfo and FileInfo? Kyle

    N 1 Reply Last reply
    0
    • K kyledunn

      Anyone have the code worked out to scan every file in every directory on a hard drive to locate a single file possibly using DirectoryInfo and FileInfo? Kyle

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      You could do something like this, inside the loop I am adding the file to a listbox:

      string m_CurrentDirectory = "C:\";
      string[] AllFiles = Directory.GetFiles(m_CurrentDirectory, "*.*");
      foreach (string aFile in AllFiles)
      {
      string aFileName = GetFileNameFromPath(aFile);
      this.listBox1.Items.Add(aFileName);
      }

      Nick Parker

      K J 2 Replies Last reply
      0
      • N Nick Parker

        You could do something like this, inside the loop I am adding the file to a listbox:

        string m_CurrentDirectory = "C:\";
        string[] AllFiles = Directory.GetFiles(m_CurrentDirectory, "*.*");
        foreach (string aFile in AllFiles)
        {
        string aFileName = GetFileNameFromPath(aFile);
        this.listBox1.Items.Add(aFileName);
        }

        Nick Parker

        K Offline
        K Offline
        kyledunn
        wrote on last edited by
        #3

        Thanks Nick. This works well for a single directory but I need to develop a routine that will search not only the root of drive C but also every sub-directory until the entire harddrive has been searched. Kyle

        1 Reply Last reply
        0
        • N Nick Parker

          You could do something like this, inside the loop I am adding the file to a listbox:

          string m_CurrentDirectory = "C:\";
          string[] AllFiles = Directory.GetFiles(m_CurrentDirectory, "*.*");
          foreach (string aFile in AllFiles)
          {
          string aFileName = GetFileNameFromPath(aFile);
          this.listBox1.Items.Add(aFileName);
          }

          Nick Parker

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          public void SearchHardDrive(string harddrive)
          {
          DirectoryInfo di = new DirectoryInfo(harddrive);

          SearchDirectory(di);
          }

          private void SearchDirectory(DirectoryInfo di)
          {
          DirectoryInfo [] dirs = di.GetDirectories();
          foreach(DirectoryInfo dir in dirs)
          {
          SearchDirectory(dir);
          }

          FileInfo [] files = di.GetFiles();
          foreach(FileInfo file in files)
          {
          DoSomethingWithFileInfo(file);
          }
          }

          private void DoSomethingWithFileInfo(FileInfo file)
          {

          }

          Enjoy :) [Edit: Okay, I'm an idiot I click on the wrong post to reply too ;P] James Simplicity Rules!

          N 1 Reply Last reply
          0
          • J James T Johnson

            public void SearchHardDrive(string harddrive)
            {
            DirectoryInfo di = new DirectoryInfo(harddrive);

            SearchDirectory(di);
            }

            private void SearchDirectory(DirectoryInfo di)
            {
            DirectoryInfo [] dirs = di.GetDirectories();
            foreach(DirectoryInfo dir in dirs)
            {
            SearchDirectory(dir);
            }

            FileInfo [] files = di.GetFiles();
            foreach(FileInfo file in files)
            {
            DoSomethingWithFileInfo(file);
            }
            }

            private void DoSomethingWithFileInfo(FileInfo file)
            {

            }

            Enjoy :) [Edit: Okay, I'm an idiot I click on the wrong post to reply too ;P] James Simplicity Rules!

            N Offline
            N Offline
            Nick Parker
            wrote on last edited by
            #5

            James T. Johnson wrote: [Edit: Okay, I'm an idiot I click on the wrong post to reply too ] No way James, I didn't have time to reply to Kyles response yet. Thanks Nick Parker

            K 1 Reply Last reply
            0
            • N Nick Parker

              James T. Johnson wrote: [Edit: Okay, I'm an idiot I click on the wrong post to reply too ] No way James, I didn't have time to reply to Kyles response yet. Thanks Nick Parker

              K Offline
              K Offline
              kyledunn
              wrote on last edited by
              #6

              Thanks guys, Here is an approach that works well. Kyle using System.Management; string drive = "\"c:\""; string filename = "\"test\"" ; string extension = "\"exe\""; string qry = String.Format( "select * from cim_logicalfile where drive={0} and filename={1} and extension={2}", drive, filename, extension); ManagementObjectSearcher query = new ManagementObjectSearcher(qry); ManagementObjectCollection queryCollection = query.Get(); foreach(ManagementObject mo in queryCollection) { Console.WriteLine( "Name '{0}' :Path: '{1}' ",mo["Name"], mo["Path"]); } [Edit: The : P shows up as a smily face. Although I am happy with this result the smily face was unintentional. ]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups