0

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;
         }
    }
Cubicle.Jockey
  • 3,288
  • 1
  • 19
  • 31
Miza
  • 49
  • 1
  • 1
  • 8
  • I find this requirement little weird. – Sushil Mate Jun 29 '17 at 03:56
  • @SushilMate what weird? I want the search button cannot be click if the value in text box is not exist in the folder and the search button can be click if value in text box is exist in the folder. – Miza Jun 29 '17 at 04:00
  • then whats the use of search button? you are allowing something to search which is already in the folder, IMHO you shouldn't enable/disable button, let the user clicks on it & find out if it is there or not. – Sushil Mate Jun 29 '17 at 04:03
  • @SushilMate the search button is to open the file dialog. The situation is like this, user entered empoyee no. in the textbox, then click search button to open file dialog, when the file dialog open, user will select the employee photo according to employee no. textbox to load the image into the database. Can u help me? :( – Miza Jun 29 '17 at 04:12
  • Possible duplicate of [How to check if a specific file exists in directory or any of its subdirectories](https://stackoverflow.com/questions/3994448/how-to-check-if-a-specific-file-exists-in-directory-or-any-of-its-subdirectories) – Tien Nguyen Ngoc Jun 29 '17 at 04:17
  • @Miza have you tried Zhyke's answer? i think that will work. – Sushil Mate Jun 29 '17 at 04:20
  • @SushilMate already tried but have error and still trying. can u edit in my code? maybe I wrongly code :( – Miza Jun 29 '17 at 04:34
  • @Miza check my solution hope that works. – Sushil Mate Jun 29 '17 at 04:50

2 Answers2

2

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;
}`
Zhyke
  • 427
  • 7
  • 22
0
 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;
            }
        }
Sushil Mate
  • 583
  • 6
  • 14