2

在我之前的问题中:如何知道我正在打开的文件是否是 .txt 文件在 VB.net 中

我在这里问如何知道我是否正在打开 .txt 文件。

下面的代码是我打开 .txt 文件并提示用户文件是否为 .txt 的代码。

Dim filename As String = String.Empty
Dim TextLine As String = ""
Dim SplitLine() As String


Dim ofd1 As New OpenFileDialog()

ofd1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
ofd1.FilterIndex = 2
ofd1.RestoreDirectory = True
ofd1.Title = "Open Text File"

'get the filename of the txt file
If ofd1.ShowDialog() = DialogResult.OK Then
'if the file is not .txt file
        If (Path.GetExtension(filename).ToLower() <> ".txt") Then
            MessageBox.Show("Please select text Files only", _
                            "RMI", _
                             MessageBoxButtons.OK, _
                             MessageBoxIcon.Warning)

            'show the open file dialog
            ofd1.ShowDialog()

            'if the file is .txt file
        Else
            filename = ofd1.FileName
 End If

'if the filename is existing
If System.IO.File.Exists(filename) = True Then

    Dim objReader As New System.IO.StreamReader(filename)

    'read the text file and populate the datagridview
    Do While objReader.Peek() <> -1
        TextLine = objReader.ReadLine()
        TextLine = TextLine.Replace(" ", "")
        SplitLine = Split(TextLine, ",")
        dvList.Rows.Add(SplitLine)
    Loop

End If

如果我选择的文件不是 .txt 文件,则输出如下:

在此处输入图像描述

如果我打开一个不存在的文件,输出如下:

在此处输入图像描述

在第一张图片中,它只显示错误消息框,但在第二张图片中,错误消息框位于打开文件对话框中。

我的问题是如何在打开文件对话框中显示第一张图像的错误消息框?

谢谢你。

4

2 回答 2

1

笔记:

  • 显示表单后无需检查扩展名,但您应该设置适当的过滤器以限制.txt仅选择文件 "txt files (*.txt)|*.txt"
  • 您可以使用OpenFileDialiog.CheckFileExistsOpenFileDialiog.CheckPathExists属性来防止用户输入无效的文件名/路径(显示错误消息)
  • CheckFileExists如果您使用/ ,不确定是否需要再次检查文件是否存在CheckPathExists
  • 您应该始终处理使用ShowDialog()方法显示的表单。
  • 你应该处置StreamReader

Dim filename As String = String.Empty
Dim TextLine As String = ""
Dim SplitLine() As String

Using ofd1 As New OpenFileDialog()
        ofd1.Filter = "txt files (*.txt)|*.txt"
        ofd1.FilterIndex = 2
        ofd1.CheckPathExists = True
        ofd1.CheckPathExists = True
        ofd1.RestoreDirectory = True
        ofd1.Title = "Open Text File"

        'get the filename of the txt file
        If ofd1.ShowDialog() = DialogResult.OK Then
            filename = ofd1.FileName

            Using objReader As New System.IO.StreamReader(filename)

                'read the text file and populate the datagridview
                Do While objReader.Peek() <> -1
                    TextLine = objReader.ReadLine()
                    TextLine = TextLine.Replace(" ", "")
                    SplitLine = Split(TextLine, ",")
                    dvList.Rows.Add(SplitLine)
                Loop
            End Using
        End If
End Using
于 2013-09-24T05:01:35.093 回答
0

在这里,我添加了隐藏的标签。(名称:路径标签)按钮(打开文件)从工具箱添加 openfiledialog

这很简单。打开文件按钮:

openfiledialog.showdialog()

OpenFileDialog_FileOk :

PathLabel.Text = System.IO.Path.GetExtension(OpenFileDialog.FileName)
    If PathLabel.Text = ".txt" Then
        Dim Objectreader As New System.IO.StreamReader(OpenFileDialog.FileName)
        TextBox1.Text = Objectreader.ReadToEnd
        Objectreader.Close()
    Else
        MsgBox("please select only Text Document (.txt)", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error")
    End If

谢谢...


相反,您必须将过滤器设置为 openfiledialog 按钮代码(打开文件)

Openfiledialog.showdialog()
openfiledialog.filter = "Text Document|*.txt"
于 2013-11-07T04:40:55.570 回答