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

                // Loop through the list of directories.
                foreach (string strDirName in Directory.GetDirectories(strDir))
                    // Display the directory name.
                    // 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;
                            checksum c = new checksum();
                            string value = c.calculator(strFileName);
                            defmatcher m = new defmatcher();
                            string matched = m.go(value);
                            if (matched != null)
                            // 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")

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

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.


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).


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

This article has been dead for over six months. Start a new discussion instead.