在地图文件夹中循环存在文件

时间:2014-01-17 00:36:35

标签: c#

我正在尝试验证用户点击btnCheck的时间。这将触发isAllRequiredFilesExist。该过程需要检查指定的所有文件名fileNameRequired。找不到文件时,请终止进程并显示错误消息,否则进程将继续,直到所有文件名都被检查并存在于地图文件夹中。

我拥有的当前代码,它只迭代一次然后停止。我认为isAllRequiredFilesExist中的循环语句有问题。在检查并存在所有文件名之前,代码不应命中validateData()

有人可以建议是否有更好的方法。谢谢

protected void btnCheck_Click(object sender, EventArgs e)
{
    string Message = string.Empty;
    string pathDirectory = Server.MapPath("~/UploadFiles/");
    try
    {
        //need to check all file names exist before execute ValidateData()
        if (isAllRequiredFilesExist()) 
        {
            validateData();
        }

    }
    catch (Exception ex)
    {
        Message = ex.Message;

    }

}

/// <summary>
/// Check if all required data Exist
/// If one required file is missing then notify the user and end the process otherwise it will loop until all names in filenameRequired.

/// </summary>
/// <returns></returns>
public bool isAllRequiredFilesExist()
{
    string pathDirectory = Server.MapPath("~/UploadFiles/");
    string[] fileNameRequired = { "test1.txt", "test2.txt", "test3.txt" };
    //Check if all the required file exist in the folder
    foreach (string names in fileNameRequired)
    {
        //Loop through the folder
        //if there is a missing file then notified the user 

        foreach (string fileNameToCheck in Directory.EnumerateFiles(pathDirectory, names, SearchOption.AllDirectories))
        {
            if (!File.Exists(fileNameToCheck))
            {
                lblMessage.Text = "Missing file: " + names;
                return false;
            }
            else
            {
                return true;
            }

        }
    }

    return false;
}

2 个答案:

答案 0 :(得分:0)

问题在于您的isAllRequiredFilesExist - 如果单个文件存在,则返回true。改变它:

public bool isAllRequiredFilesExist()
{
    string pathDirectory = Server.MapPath("~/UploadFiles/");
    string[] fileNameRequired = { "test1.txt", "test2.txt", "test3.txt" };
    //Check if all the required file exist in the folder
    foreach (string names in fileNameRequired)
    {
        //Loop through the folder
        //if there is a missing file then notified the user 

        foreach (string fileNameToCheck in Directory.EnumerateFiles(pathDirectory, names, SearchOption.AllDirectories))
        {
            if (!File.Exists(fileNameToCheck))
            {
                lblMessage.Text = "Missing file: " + names;
                return false;
            }    
        }
    }

    return true;
}

因此,如果缺少任何给定文件,它将返回false,如果所有必需文件都存在 - 它将在循环后返回true。

同样EnumerateFiles将返回现有文件的集合,因此您无需再次使用File.Exists进行检查 - 只需检查集合的大小,如:

var files = Directory.EnumerateFiles(pathDirectory, names, SearchOption.AllDirectories);
if (!files.Any())
{
   lblMessage.Text = "Missing file: " + names;
   return false;
}

但是由你决定:)

答案 1 :(得分:0)

在最内循环中取出两个return语句

if (!File.Exists(fileNameToCheck))
            {
                lblMessage.Text = "Missing file: " + names;
                return false;
            }
            else
            {
                return true;
            }

而不是返回,做其他事情,然后在最后,做出回报 因为,一旦返回,您将退出带有返回值的方法。在您的情况下,您不希望返回true,直到检查所有名称