2016-11-29 25 views
0

這就是我需要做的,我點擊打開文件按鈕,彈出打開文件對話框。我打開一個文本文件並將其顯示在informationbox.text字段中,同時我想在該文件中搜索一個ID號並將其顯示在IDbox.text字段中。 我已經搜索了其他論壇,但他們只是使用替換方法或其他我不知道的方法。它變得太混亂。在TextFile中查找ID號碼

這是我迄今爲止 - 文本文件的

Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click 
    Dim oReader As StreamReader 
    OpenFileDialog1.CheckFileExists = True 
    OpenFileDialog1.CheckPathExists = True 
    OpenFileDialog1.DefaultExt = "txt" 
    OpenFileDialog1.FileName = "" 
    OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*" 
    OpenFileDialog1.Multiselect = False 
    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then 
     oReader = New StreamReader(OpenFileDialog1.FileName, True) 
     InformationBox.Text = oReader.ReadToEnd 
     My.Forms.Home.TextBox5.AppendText(Environment.NewLine & "Opened Customer : " & OpenFileDialog1.FileName & " File") 
     oReader.Close() 
    End If 
    IDBox.Text = "" 
    Label11.Text = OpenFileDialog1.FileName 
End Sub 

例如:

客戶的姓名:姓名

姓客戶:姓

身份證號碼:12345678910

備案號:001

客戶地址:地址

任何人都可以幫助我嗎?

+2

請顯示這樣的文本文件的示例。 ID是如何正確存儲在文件中的? –

+0

補充說,問題 –

回答

0

在您的示例代碼中,您使用StreamReader來讀取文本文件。

Streams的「缺點」是您必須手動管理其處置。
在您的示例中,如果在oReader.ReadToEnd中發生錯誤,則行oReader.Close將不會被命中,並且該流可能會保持不存在,從而造成麻煩。
所以你最好把你的Stream放在Using範圍內(另一個選擇是使用靜態的System.IO.File.ReadAllLines|ReadAllText方法)。

Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click 
    'Dim oReader As StreamReader <-- DELETE THIS 
    OpenFileDialog1.CheckFileExists = True 
    OpenFileDialog1.CheckPathExists = True 
    OpenFileDialog1.DefaultExt = "txt" 
    OpenFileDialog1.FileName = "" 
    OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*" 
    OpenFileDialog1.Multiselect = False 
    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then 
     'MY CODE STARTS HERE: 
     Dim customerInfo As String 
     Using sr = New StreamReader(OpenFileDialog1.FileName, True) 
      customerInfo = sr.ReadToEnd() 
     End Using 'Close the stream early since we have all data needed 

    'Write all lines into a string array 
     Dim lines As String() = customerInfo.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries) 
    'Get the line where the ID Number is in 
     Dim idLine As String = lines.Where(Function(l) l.StartsWith("ID number")).FirstOrDefault() 
     Dim id As String = String.Empty 
     If Not String.IsNullOrEmpty(idLine) Then 
     Dim aIdLine() = idLine.Split(":"c) 'Split the ID line by : 
     If aIdLine.Length >= 1 Then 
      id = aIdLine(1) 'This should be the actual ID 
     End If 
     End If 

    'Set UI 
    My.Forms.Home.TextBox5.AppendText(Environment.NewLine & "Opened Customer : " & OpenFileDialog1.FileName & " File") 
    InformationBox.Text = customerInfo 
    IDBox.Text = id 
    Label11.Text = OpenFileDialog1.FileName 
End If 
+0

我複製你的代碼,我試了一下,沒有發生在IDBox.text字段。 informationbox.text字段填充了文本,這很好。沒有錯誤出現 –

+0

您可以[上傳](http://textuploader.com/)您正在測試的文本文件嗎?自從它爲我工作以來,我和你的可能會有不匹配。同時:調試我的代碼並檢查'If Not String.IsNullOrEmpty(idLine)Then'是true還是false。如果爲真,也檢查'如果aIdLine.Length> = 1 Then'是真或假。 –

+0

我如何上傳我的txt文件? –