Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查文件夹中是否存在文本框中的值_C#_Winforms_Visual Studio 2013 - Fatal编程技术网

C# 如何检查文件夹中是否存在文本框中的值

C# 如何检查文件夹中是否存在文本框中的值,c#,winforms,visual-studio-2013,C#,Winforms,Visual Studio 2013,目前,我正在使用C windows窗体开发图像加载系统。若在文本框中输入的值在文件夹中存在或不存在,我就无法启用/禁用搜索按钮。如果文件夹中不存在文本框中的值,则无法单击搜索按钮;如果文本框中的值存在,则可以单击搜索按钮。问题是,即使我输入的值存在于文件夹中,也无法单击按钮搜索。请有人帮帮我。这是我的密码: private void textBoxEmpNo_TextChanged(object sender, EventArgs e) { string baseFolde

目前,我正在使用C windows窗体开发图像加载系统。若在文本框中输入的值在文件夹中存在或不存在,我就无法启用/禁用搜索按钮。如果文件夹中不存在文本框中的值,则无法单击搜索按钮;如果文本框中的值存在,则可以单击搜索按钮。问题是,即使我输入的值存在于文件夹中,也无法单击按钮搜索。请有人帮帮我。这是我的密码:

 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;
         }
    }

试着使用下面的方法

//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;
}`

试着使用下面的方法

//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 = @"C:\Users\matesush\Pictures";

            if (Directory.EnumerateFiles(baseFolder, "*" + textBoxEmpNo.Text + "*.jpeg", SearchOption.AllDirectories).Any())
            {
                buttonSearch.Enabled = true;
                fileFound = true;
            }
            else
            {
                buttonSearch.Enabled = false;
            }
        }
解决阿德里亚诺·雷佩蒂的建议

 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;
            }
        }

我觉得这个要求有点奇怪。@SushilMate什么奇怪?如果文件夹中不存在文本框中的值,则无法单击搜索按钮;如果文件夹中存在文本框中的值,则可以单击搜索按钮。那么,搜索按钮的用途是什么?您允许搜索文件夹中已存在的内容,但不应启用/禁用按钮,让用户单击它并查看它是否存在。@SushilMate“搜索”按钮用于打开“文件”对话框。情况是这样的,用户在文本框中输入员工编号,然后点击搜索按钮打开文件对话框,当文件对话框打开时,用户将根据员工编号文本框选择员工照片,将图像加载到数据库中。你能帮我吗我觉得这个要求有点奇怪。@SushilMate什么奇怪?如果文件夹中不存在文本框中的值,则无法单击搜索按钮;如果文件夹中存在文本框中的值,则可以单击搜索按钮。那么,搜索按钮的用途是什么?您允许搜索文件夹中已存在的内容,但不应启用/禁用按钮,让用户单击它并查看它是否存在。@SushilMate“搜索”按钮用于打开“文件”对话框。情况是这样的,用户在文本框中输入员工编号,然后点击搜索按钮打开文件对话框,当文件对话框打开时,用户将根据员工编号文本框选择员工照片,将图像加载到数据库中。你能帮我吗可能的副本仍然不工作。你能在我的代码中编辑吗?也许我错误地编码了:仍然不工作。你能在我的代码中编辑吗?也许我把代码搞错了:谢谢你的回复。它可以工作,但为什么加载速度很慢?你有没有什么代码可以避免搜索速度慢?我想你是在访问远程位置&文件的数量会很大。搜索可能需要时间。如果photo directory只有文件,则可以使用file.exists,这可能比上述解决方案更快。EnumerateFiles.Any,一个匹配就足以满足条件。更好的是,让它异步。感谢您的回复。它可以工作,但为什么加载速度很慢?你有没有什么代码可以避免搜索速度慢?我想你是在访问远程位置&文件的数量会很大。搜索可能需要时间。如果photo directory只有文件,则可以使用file.exists,这可能比上述解决方案更快。EnumerateFiles.Any,一个匹配就足以满足条件。更好的是,让它异步。