简体   繁体   中英

How to check if a value in a text box exists in a folder

currently i'm developing the image load system using C# (windows form). I have problem on how to enable/disable search button if value entered in text box exist or not exist in folder. I want the search button cannot be click if the value in text box not exist in the folder and the search button can be click if value in text box exist. The issue is the button search cannot be click even tough the value I entered is exist in a folder. Please someone help me. Here is my code:

 private void textBoxEmpNo_TextChanged(object sender, EventArgs e)
 {

        string baseFolder = @"\\\\egmnas01\\hr\\photo";

        string checkEmpNo = "*" + textBoxEmpNo.Text + "*.jpg";

        bool fileFound = false;

        DirectoryInfo di = new DirectoryInfo(baseFolder);


        foreach (var folderName in baseFolder)
        {
          var path = Path.Combine(baseFolder, checkEmpNo);

          if (File.Exists(checkEmpNo))
          {
            buttonSearch.Enabled = true;

            fileFound = true;
            break;
            //If you want to stop looking, break; here 
           }
         }
         if (!fileFound)
         {
           //Display message that No such image found
           buttonSearch.Enabled = false;
         }
    }

try to use the following.

//Search for the filename that you have entered in textBoxempNo.

string[] fileFound = Directory.GetFiles(baseFolder, "*" + textBoxEmpNo.Text 
+ "*.jpeg", SearchOption.AllDirectories)

//Then check if there are files found.

`if (fileFound.Length ==0 )
{
  buttonSearch.Enabled = false;
}
else
{
  buttonSearch.Enabled = true;
}`
 private void textBoxEmpNo_TextChanged(object sender, EventArgs e)
        {
            bool fileFound = false;
            const string baseFolder = @"\\\\egmnas01\\hr\\photo";

            string[] matchedFiles = Directory.GetFiles(baseFolder, "*" + textBoxEmpNo.Text + "*.jpeg", SearchOption.AllDirectories);

            if (matchedFiles.Length == 0)
            {
                buttonSearch.Enabled = false;
            }
            else
            {
                buttonSearch.Enabled = true;
                fileFound = true;
            }
        }

Addressing Adriano Repetti suggestion.

 private void textBoxEmpNo_TextChanged(object sender, EventArgs e)
        {
            bool fileFound = false;
            const string baseFolder = @"C:\Users\matesush\Pictures";

            if (Directory.EnumerateFiles(baseFolder, "*" + textBoxEmpNo.Text + "*.jpeg", SearchOption.AllDirectories).Any())
            {
                buttonSearch.Enabled = true;
                fileFound = true;
            }
            else
            {
                buttonSearch.Enabled = false;
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM