0

これはファイルを開くための私のコードです:

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
    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

1 に答える 1

1

私の質問は、開いているファイルが .txt ファイルであるかどうかをどのように確認できますか? 出来ますか?ありがとうございました。

ユーザーがファイルを選択した後、ファイルの拡張子を確認できます。

If (Path.GetExtension(filename).ToLower() = ".txt") Then
     ' It's a .txt file
End If
于 2013-09-24T00:33:03.663 に答える