Hello every body

I am working on VS2010 C# windows application

how can i search a particular file by name sample1.txt not only in one drive, how can i search a file in complete system

please help me

Hello every body

I am working on VS2010 C# windows application

how can i search a particular file by name sample1.txt not only in one drive, how can i search a file in complete system

please help me

how to search a file in a external hard disk by using C#.Net please guide me with a code

how to search a file in a external hard disk by using C#.Net please guide me with a code

Some untested code listed below to help you get started...

private static void SearchForFiles(string path, string pattern, List<string> SearchResults)
        {
            foreach (string file in Directory.GetFiles(path, pattern))
            {
                SearchResults.Add(file);
            }
            foreach (string dir in Directory.GetDirectories(path))
            {
                try
                {
                    SearchForFiles(dir, pattern, SearchResults);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Cannot access " + dir);
                }
            }
        }

        static void Main(string[] args)
        {
            List<string> SearchResults = new List<string>();
            SearchForFiles(@"C:\", "*.cpp", SearchResults);

            foreach (string s in SearchResults)
            {
                Console.WriteLine(s);
            }
        }

You'll need to use the DriveInfo class to help you check for all available drives

ya the code is ok for C drive but my problem is how to search a text file in a external hard disk

Some untested code listed below to help you get started...

private static void SearchForFiles(string path, string pattern, List<string> SearchResults)
        {
            foreach (string file in Directory.GetFiles(path, pattern))
            {
                SearchResults.Add(file);
            }
            foreach (string dir in Directory.GetDirectories(path))
            {
                try
                {
                    SearchForFiles(dir, pattern, SearchResults);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Cannot access " + dir);
                }
            }
        }

        static void Main(string[] args)
        {
            List<string> SearchResults = new List<string>();
            SearchForFiles(@"C:\", "*.cpp", SearchResults);

            foreach (string s in SearchResults)
            {
                Console.WriteLine(s);
            }
        }

You'll need to use the DriveInfo class to help you check for all available drives

ya the code is ok for C drive but my problem is how to search a text file in a external hard disk

Search all the drives....

static void Main(string[] args)
        {
            string myString;
            List<string> SearchResults = new List<string>();
            DriveInfo[] allDrives = DriveInfo.GetDrives();

            foreach (DriveInfo d in allDrives)
            {
                if (d.IsReady == true)
                {
                    myString = String.Format(" {0}", d.Name);
                    Console.WriteLine("Checking drive {0}", d.Name);
                    SearchForFiles(myString, "*.log", SearchResults);
                 
                }
            }
            foreach (string s in SearchResults)
            {
                Console.WriteLine(s);
            }
        }
foreach (string file in Directory.GetFiles(path, pattern)) {
    SearchResults.Add(file);
}

I'd have done this a little different by getting rid of the foreach:

SearchResults.AddRange(Directory.GetFiles(path, pattern));

I'd have done this a little different by getting rid of the foreach:

SearchResults.AddRange(Directory.GetFiles(path, pattern));

Hey thank you everyone the code is working perfectly

thank you once again

Edited 4 Years Ago by skp03: n/a

Hey thank you everyone the code is working perfectly

thank you once again

the above code works no problem
but only thing is
it will search a file in the complete computer, thats why it takes a lot of time
so
how can i search a file only in a external hard disk

please help me

the above code works no problem
but only thing is
it will search a file in the complete computer, thats why it takes a lot of time
so
how can i search a file only in a external hard disk

please help me

What do you mean by an "external hard disk"?

using System;
using System.IO;
using System.Linq;

namespace DW_416752_CS_CON
{
   class Program
   {
      static void Main(string[] args)
      {
         char chrDriveLetter = 'c'; //<<-- change this letter
         string strSearchPattern = "*.*";

         Directory
            .GetFiles(chrDriveLetter + ":\\", strSearchPattern, SearchOption.TopDirectoryOnly)
            .ToList().ForEach(strFileName => Console.WriteLine(strFileName));
      }
   }
}
This article has been dead for over six months. Start a new discussion instead.