2017-01-19 96 views
2

我想讓我的應用程序無法關閉,直到特定目錄內的所有文件都有特定的前綴。我的主目錄有5個子目錄,但是我必須讓它只搜索3個特定的子目錄呢?我想要的應用無法關閉,除非所有的文件在這些3子目錄帶前綴......是否可以搜索目錄內的特定目錄?

這是我的代碼到目前爲止

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing 

e.Cancel = True 

    Dim directory As New IO.DirectoryInfo(dir) 

    For Each file In directory.EnumerateFiles("*.txt", IO.SearchOption.TopDirectoryOnly) 

     If file.Name.Contains("prefix.") Then 
      e.Cancel = False 
     Else e.Cancel = True 

     End If 


    Next 

回答

0

如果你只搜索5個子目錄中的3個,我假設它們的名稱/路徑是靜態的。如果是這樣,這將工作。

Dim directoriesToSearch() As String = {"C:\TopDirectory\SubDir1", "C:\TopDirectory\SubDir2", "C:\TopDirectory\SubDir3"} 
Dim sb As New StringBuilder() 

For Each d In directoriesToSearch 
    Dim directory As New IO.DirectoryInfo(d) 

    For Each f As FileInfo In directory.EnumerateFiles() 
    If f.Name.IndexOf("myPrefix") <> 0 Then 
     Dim message As String = String.Format("The directory {0} does not have the valid prefix.", d) 
     sb.AppendLine(message) 
    End If 
    Next 
Next 

If sb.Length > 0 Then 
    MessageBox.Show(sb.ToString) 
End If 
+1

謝謝你,你真的幫我過了 – jdwee