仅打开带有 *.bin 的文件

Open only files with *.bin

是否可以在打开文件对话框中添加扩展名为 *.bin 的只打开文件? 这是我的代码。也许有人可以修复它。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim OFD As New OpenFileDialog
        Dim fullFile() As Byte
        If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
            fullFile = File.ReadAllBytes(OFD.FileName)
            TextBox1.AppendText(fullFile(&H1E).ToString("X2") & " ")
            TextBox1.AppendText(fullFile(&H1F).ToString("X2"))
        End If

如果文件有另一个扩展名 msg,box : 错误的文件

您需要使用javax.swing.JFileChooser

使用这个:

FileNameExtensionFilter filter = new FileNameExtensionFilter("Binary Files", "bin");
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(filter);

您需要使用 Filter 属性: MSDN

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim OFD As New OpenFileDialog
    OFD.Filter = "BIN Files (*.bin)|*.bin"
    Dim fullFile() As Byte
    If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
        fullFile = File.ReadAllBytes(OFD.FileName)
        TextBox1.AppendText(fullFile(&H1E).ToString("X2") & " ")
        TextBox1.AppendText(fullFile(&H1F).ToString("X2"))
    End If
End Sub

过滤器字符串中使用竖线 | 字符将其分成块:第一个是用户在下拉列表中看到的内容,第二个是实际过滤器 运行在文件上。您也可以使用多个过滤器。这是过滤器字符串的另一个示例:Text files (*.txt)|*.txt|All files (*.*)|*.*