如何知道我要打开的文件是不是.txt文件在VB.net中 [英] How to know if the file I'm opening is a .txt file or not in VB.net
本文介绍了如何知道我要打开的文件是不是.txt文件在VB.net中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我打开文件的代码:
This is my code for opening files:
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文件?是否有可能?谢谢.
My question is how can I know if the file that I am opening is .txt file or not? Is it possible? Thank you.
推荐答案
我的问题是如何知道我打开的文件是否是.txt文件?是否有可能?谢谢.
My question is how can I know if the file that I am opening is .txt file or not? Is it possible? Thank you.
您可以在用户选择文件后检查其扩展名:
You can check the extension of the file after the user picks it:
If (Path.GetExtension(filename).ToLower() = ".txt") Then
' It's a .txt file
End If
这篇关于如何知道我要打开的文件是不是.txt文件在VB.net中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文