我试图让我的应用程序无法关闭,直到特定目录中的所有文件都有一定的前缀。我的主目录中有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 :(得分: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