?
Solved

C# code question

Posted on 2014-02-25
2
Medium Priority
?
198 Views
Last Modified: 2014-02-26
Is there anything wrong with the code below? Does the logic skip any files/folders in any conditions? In particular, the "Continue" statement is giving me doubt.

void Find(string folder)
{
	/*
	search this directory and all its subdirectories for files which 
	names/contents (chkMatchFileName/chkMatchFileContent) match the entered criteria
	*/

	bool matchFileName = chkMatchFileName.Checked;
	bool matchFileContent = chkMatchFileContent.Checked;

	// loop through files
	string[] arrFiles = Directory.GetFiles(folder);
	foreach (string file in arrFiles)
	{
		if (matchFileName)
		{
			// file name matches entered criteria
			if (file.Contains(txtSearchText.Text))
			{
				AddToList(file);
				continue; // to avoid dups, if item is added to ListBox, no need to add it again even if file content matches criteria.
			}
		}

		if (matchFileContent)
		{
			// file content matches entered criteria
			if (File.ReadAllText(file).Contains(txtSearchText.Text))
			{
				AddToList(file);
			}
		}
	}

	string[] arrDirs = Directory.GetDirectories(folder);
	foreach (string dir in arrDirs)
	{
		Find(dir);
	}
}

Open in new window

0
Comment
Question by:pzozulka
2 Comments
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 1000 total points
ID: 39886070
I thnk this is the same logic, maybe clearer to understand :
      {
            if (matchFileName)
            {
                  // file name matches entered criteria
                  if (file.Contains(txtSearchText.Text))
                  {
                        AddToList(file);
                  }
            }

            else if (matchFileContent)// to avoid dups, if item is added to ListBox, no need to add it again even if file content matches criteria.
            {
                  // file content matches entered criteria
                  if (File.ReadAllText(file).Contains(txtSearchText.Text))
                  {
                        AddToList(file);
                  }
            }


ps.  Looks to be pretty standard recursive technique.  It will have problems if a file/directory can not be read due to permissions - so you could do with try....catch blocks to improve the robustness, otherwise it should read all files/folders in the given root
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 1000 total points
ID: 39888193
The logic looks sound from the bit we can see. We can't see the Find() method, but i'm guessing the code you posted makes up that method. The continue is perfectly valid - could be replaced with an if...else, but that's purely symantic.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month16 days, 11 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question