Getting name(s) of FOLDER containing a specific SUBSTRING from the C:Temp directory in C#

Guys as the title says it I have to get the names of FOLDERS having a particular (user indicated) sub string.

I have a text box where the user will input the wanted sub string. and I am using the codes below to achieve my goal.

 string name = txtNameSubstring.Text;
            string[] allFiles = System.IO.Directory.GetFiles("C:\\Temp");//Change path to yours
            foreach (string file in allFiles)
            {
                if (file.Contains(name))
                {
                    cblFolderSelect.Items.Add(allFiles);
                    // MessageBox.Show("Match Found : " + file);
                }
                else
                {
                    MessageBox.Show("No files found");
                }
            }

It does not work. When I trigger it,only the message box appears. Help ?

Answers


You can use the appropriate API to let the framework filter the directories.

var pattern = "*" + txtNameSubstring.Text + "*";
var directories = System.IO.Directory.GetDirectories("C:\\Temp", pattern);

Because the MessageBox will appear for the first path that does not contain the substring

You could use Linq to get the folders, but you will need to use GetDirectories not GetFiles

string name = txtNameSubstring.Text;
var allFiles = System.IO.Directory.GetDirectories("C:\\Temp").Where(x => x.Contains(name));//

if (!allFiles.Any())
{
   MessageBox.Show("No files found");
}

cblFolderSelect.Items.AddRange(allFiles);

You don't want to have the message box inside the loop.

string name = txtNameSubstring.Text;
string[] allFiles = System.IO.Directory.GetFiles("C:\\Temp");//Change path to yours
foreach (string file in allFiles)
{
   if (file.Contains(name))
   {
      cblFolderSelect.Items.Add(file);
      // MessageBox.Show("Match Found : " + file);
   }
}
if(cblFolderSelect.Items.Count==0)
{
   MessageBox.Show("No files found");
}

(Assuming cblFolderSelect was empty before this code runs)

As you currently have it, you're deciding whether to show the message box for each file that you examine. So if the first file doesn't match, you'll be told "No files found" even though the next file might match.

(I've also changed the Add to add the individual file that matches, not all of the files (for which one or more matches))


Need Your Help

storing multiple values in one field

php mysql sql database-design

I want users to be able to select their majors.

Apple is not currently accepting applications built with this version of the SDK

iphone iphone-sdk-3.0 ios4 ios-simulator cocos2d-iphone

My SDK version is "Latest" which means 4.2 and the other thing is set to 3.0. App Loader is giving me this error.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.