2014-01-17 46 views
0

我試圖驗證用戶何時單擊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; 
} 

回答

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

但它給你:)

+0

謝謝你。我更喜歡你的第二個選擇。 – Supermode

+0

不客氣。另外,如果在/ UploadFiles /下沒有真正的目錄,並且只是在那裏,你最終可能沒有EnumerateFiles,並檢查更可讀的File.Exists – JleruOHeP

0

採取兩個return語句出你最內環

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

取而代之的回報,做別的事情,然後在電子nd,返回 原因,一旦你返回,就退出帶有返回值的方法。在你的情況下,你不想回到真實的,直到所有的名字都檢查