怎样用vb.net读取本地.txt文件?

来源:百度知道 编辑:UC知道 时间:2024/05/13 16:37:59
我在写一个程序,用的语言是vb.net,设计视图是一个简单的页面,上面有“On”跟“Off”两个Button。

现在我想要在点击“On”的时候,程序自动读取本地文件A.txt(这个文件的内容只有一个字母“A”),让A.txt里面的内容(就是字母A了)自动发送给与电脑连接的电路板,让电路板上的灯亮起来;而在点击“Off”的时候读取本地文件B.txt(文件内容只有一个字母“B”),从而自动发送字母B到电路板,使灯熄灭。
(注:电路板的程序为收到A就开灯,收到B就关灯。)

我不知道这段读取及发送内容到电路板的命令要怎么写,希望各位懂vb.net的大大不吝赐教,感激涕零!!!(如果没弄明白我的问题,可以再问我,因为在这方面实在是太菜了,所以连表达都不清楚了。)谢谢!顺便祝看贴的各位圣诞快乐&元旦快乐! ^_^
以下是我的源码:

Public Class Form1
Dim fp As IO.StreamReader

Private Sub btnON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnON.Click
Try
lblMessage.Text = "LED is turn on."
btnON.Enabled = False
btnOFF.Enabled = True
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOFF.Click
Try

imports System.IO

读取指定文件
'
'读取指定文本文件
Public Function readtext(ByVal path As String)
If path = "" Then
readtext = "操作失败!"
Exit Function
End If
Try
If File.Exists(path) = True Then
Dim fs As New FileStream(path, FileMode.Open)
Dim sr As New StreamReader(fs)
Dim str As String
str = sr.ReadToEnd.ToString
sr.Close()
fs.Close()
readtext = str
Else
readtext = "操作失败!"
End If
Catch ex As Exception
readtext = "操作失败!"
End Try
End Function
'向指定文件写入数据
Public Function writetext(ByVal path As String, ByVal opi As Integer, ByVal msg As String)
If path = "" Then
writetext = "操作失败!"
Exit Function
End If
Dim op As FileMode
Select Case opi
Case 1
op = FileMode.Append
Case 2
op = FileMode.Create
Case Else
op = FileMode.Create
End Sele