0

这是我的代码:

Private Sub btnDisplayOrderDetails_Click(ByVal sender As Object, _
                                         ByVal e As EventArgs) _
                                         Handles btnDisplayOrder Details.Click

  Dim myStreamReader As StreamReader
  Try
    myStreamReader = File.OpenText("Textfile1.txt")
    Me.txtOrderDetails.Text = myStreamReader.ReadToEnd()
  Catch exc As Exception
  Finally
    If Not myStreamReader Is Nothing Then
      myStreamReader.Close()
    End If
  End Try

 If txtOrderDetails.Text = "" Then
   Dim mystreamreader1 As StreamReader
   Try
     mystreamreader1 = File.OpenText("textfile2.txt")
     Me.txtOrderDetails.Text = myStreamReader.ReadToEnd()
   Catch ex As Exception
   Finally
     If Not myStreamReader Is Nothing Then
       mystreamreader1.Close()
     End If
   End Try
 End If

End Sub

我希望这段代码做的是:

单击按钮时读取第一个文本文件,然后当我清除文本框(使用已编码的不同按钮)时,我想在按钮单击同一个文本框时读取第二个文本文件和以前一样。

4

1 回答 1

0

目前尚不清楚您要对这些文件做什么。将它们全部组合到一个文本框中,或者每个文件是否属于特定的文本框。

将所有文件放入单个文本框中的循环方法:

Dim filePath As String = "C:\Users\Practice\Practice\bin\Debug"
Dim sb As New StringBuilder
For Each f As String In Directory.GetFiles(filePath, "*.txt")
  sb.AppendLine(File.ReadAllText(f))
Next
txtOrderDetails.Text = sb.ToString()

或者,如果它不是一个列表,那么您将一个一个地浏览您的文件:

Dim test1 As String = Path.Combine(filePath, "Textbox1.txt")
If File.Exists(test1) Then
  TextBox1.Text = File.ReadAllText(test1)
End If
Dim test2 As String = Path.Combine(filePath, "Textbox2.txt")
If File.Exists(test2) Then
  TextBox2.Text = File.ReadAllText(test2)
End If
于 2012-09-03T13:05:48.877 回答