Solved

C# code question

Posted on 2014-02-25
2
184 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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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