vb.net2008 如何读取txt文件
vb.net2008 如何读取txt文件,文件里是一行一行的,每一行是一个邮箱,有的时候行是空格, 去掉空格读取所有行, 再循环写到数据库中。谢谢。有人会吗
[解决办法]
Dim fileContents As String
Dim arrText As New ArrayList()
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
strFileName = OpenFileDialog1.FileName
fileContents = My.Computer.FileSystem.ReadAllText(strFileName, System.Text.Encoding.GetEncoding("GB2312"))
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
Dim strReader As New StringReader(fileContents)
Dim sLine As String = ""
Dim i As Integer
Do
sLine = strReader.ReadLine()
If Not sLine Is Nothing Then
arrText.Add(sLine)
End If
Loop Until sLine Is Nothing
strReader.Close()
[解决办法]
fileopen(1,"C:\1.txt",openmode.input)
dim xc as string
xc=lineinput(1)
if xc<>""
'存入
.......
[解决办法]
Imports System.IO
Imports System.Web
Public Class EmailList
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'是system.io方法读取
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(Application.StartupPath & "\files\user_email.Txt", System.Text.Encoding.Default)
Dim line As String
ListBox1.Items.Clear()
line = "test" '随便赋一个初值
'添加到控件中
Do While sr.Peek() >= 0
line = sr.ReadLine
ListBox1.Items.Add(line)
Loop
Label1.Text = "总共读取" & ListBox1.Items.Count
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim newList As New ArrayList
With ListBox1
For i = 0 To .Items.Count - 1
If Not newList.Contains(.Items(i)) Then
newList.Add(.Items(i))
End If
Next i
.Items.Clear()
For i = 0 To newList.Count - 1
Dim StrA As String = newList(i)
Dim StrB As String = "@"
Dim Stu As Boolean = InStr(StrA, StrB)
If Stu = True Then
.Items.Add(newList(i))
Else
End If
Next i
End With
Label2.Text = " 整理后,总共" & ListBox1.Items.Count
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim SaveFileName As String
Dim myStreamWriter As StreamWriter
SaveFileName = Application.StartupPath + "\files\ok.txt"
myStreamWriter = File.CreateText(SaveFileName)
Dim MyOutput As String
'循环写入TXT
With ListBox1
For i = 0 To ListBox1.Items.Count - 1
MyOutput = ListBox1.Items(i)
myStreamWriter.WriteLine(MyOutput)
Next i
End With
myStreamWriter.Close()
Label5.Text = "写入成功"
End Sub
End Class