Hi,

I am writing a program for finding all the files in the computer but there is a unknown problem.This code is not showing me all the files.
I am posting code please help me

try
            {
                // Loop through the list of directories.
                foreach (string strDirName in Directory.GetDirectories(strDir))
                {
                    // Display the directory name.
                    //Console.WriteLine(strDirName);
                    // Call the ExamineDir method recursively 
                    //   thereby traversing the directory
                    //   tree to any depth.

                    foreach (string strFileName in Directory.GetFiles(strDirName))
                    {
                        FileInfo myfile = new FileInfo(strFileName);

                        if (myfile.Length <= 3000)//detemine scanner file size
                        {
                            statusTxt.Text = strFileName;
                            statusTxt.Refresh();
                            Application.DoEvents();
                            checksum c = new checksum();
                            string value = c.calculator(strFileName);
                            defmatcher m = new defmatcher();
                            string matched = m.go(value);
                            if (matched != null)
                                MessageBox.Show(matched);
                            // MessageBox.Show(value);
                        }
                    }//2foreach end
}
//////////////////////////////////////////////////////////////////////////

 private void button1_Click(object sender, EventArgs e)
        {
            
           
        /////////////////////////////////////////////
                    DriveInfo[] drives = DriveInfo.GetDrives();
            foreach (DriveInfo drive in drives)
            {
                if (drive.DriveType.ToString() == "Fixed")
                {
                  
                        ExamineDir(drive.Name);
                 
                 }
              }
       }            
////////////////////////////////////////////////////////////////////////////////
Member Avatar for ZootAllures

There's a comment in your code that explains the problem, you need to put the directory scanning code into a function of it's own and call it recursively.

private void ExamineDir (string dirName)
{
    foreach (string dir in Directory.GetDirectories(dirName))
    {
        ExamineDir (dir);
        foreach (string file in Directory.GetFiles(dirName))
        {
            // process file
        }
    }
}

Here,

The solution u have gave me is wrong it's making loop.

What's wrong with Dirctory.GetFiles(dir, "*", SearchOption.AllFiles)?

It take much more stack so i am not using it.If u have this solution by new method then please tell me may it's helpful for me.

Thanks

Member Avatar for ZootAllures

What's wrong with Dirctory.GetFiles(dir, "*", SearchOption.AllFiles)?

It has shortcomings; for a whole disk it can produce a massive listing in one go; with the nested loops you can filter better. It also gets into an infinite loop if symbolic directory links point back up the tree. It's also not supported on all platforms (like, I use Windows CE mostly and tend to forget about those methods).

Here,

The solution u have gave me is wrong it's making loop.

How many files on your disk? 100,000 isn't unusual on a busy PC and that will take a long time to find them all

I am unable to understand please give me some sample code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.