这是我打开文件的代码:
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文件?有可能吗?谢谢。
发布于 2013-09-24 00:33:03
我的问题是,如何知道我打开的文件是否为.txt文件?有可能吗?谢谢。
您可以在用户选择文件后检查它的扩展名:
If (Path.GetExtension(filename).ToLower() = ".txt") Then
' It's a .txt file
End Ifhttps://stackoverflow.com/questions/18971076
复制相似问题