Solved

C# code question

Posted on 2014-02-25
2
186 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 44

Accepted Solution

by:
AndyAinscow earned 250 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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now