Solved

C# code question

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

737 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