?
Solved

C# code question

Posted on 2014-02-25
2
Medium Priority
?
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 44

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 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