使用VB在文件对话框中打开多个文件

问题描述 投票:-2回答:1

在使用VB的Windows应用程序中,我需要从特定路径一个接一个地打开三个xml文件。如果选择了第一个xml文件,则弹出下一个xml的文件对话框。

我尝试了以下代码,但它是用于一起选择文件(即,类似于shift +选择文件)。但是我不想要这种方式,我需要一个接一个地选择文件。如果选择第一个xml,则会弹出文件对话框以选择下一个文件。就像我需要选择的三个文件。完成后,文件对话框关闭。

''''

  Private Sub SystemDescriptionToolStripMenuItem_Click()

      OpenFileDialog1.Filter = "All Files *.xml | *.xml"
      OpenFileDialog1.MultiSelect = True
      OpenFileDialog1.InitialDirectory = "D:\"
      OpenFileDialog1.Title = "Select description files"
      OpenFileDialog1.ShowDialog()

  End Sub

''''

vb.net openfiledialog
1个回答
2
投票

使用对话框中的单选(默认)

Private Sub SystemDescriptionToolStripMenuItem_Click()
    OpenFileDialog1.Filter = "All Files *.xml | *.xml"
    OpenFileDialog1.InitialDirectory = "D:\"
    OpenFileDialog1.Title = "Select description file"
    For i = 1 To 3
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            ProcessXMLFile(OpenFileDialog1.FileName)
        Else
            MessageBox.Show($"Error on File #{i}. Try Again")
            Exit Sub
        End If
    Next
End Sub

Private Sub ProcessXMLFile(path As String)
    'Your code to process the file
End Sub

要使用多选,请指示用户在对话框中单击文件时按住Ctrl键。

Private Sub SystemDescriptionToolStripMenuItem_Click()
    OpenFileDialog1.Filter = "All Files *.xml | *.xml"
    OpenFileDialog1.Multiselect = True
    OpenFileDialog1.InitialDirectory = "D:\"
    OpenFileDialog1.Title = "Select description file"
    Dim files(2) As String
    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
        files = OpenFileDialog1.FileNames
        For Each file In files
            ProcessXMLFile(file)
        Next
    Else
        MessageBox.Show($"Error on selecting files")
    End If
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.