怎样用vb.net读取本地.txt文件?
我在写一个程序,用的语言是vb.net,设计视图是一个简单的页面,上面有“On”跟“Off”两个Button。现在我想要在点击“On”的时候,程序自动读取本地文件A.tx...
我在写一个程序,用的语言是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
lblMessage.Text = "LED is turn off."
btnON.Enabled = True
btnOFF.Enabled = False
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
很愚的我意识到一个问题,像我的这种要求应该是嵌入(embed)文件而不是读取文件吧???请教各位大大我的认识对不对...... 展开
现在我想要在点击“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
lblMessage.Text = "LED is turn off."
btnON.Enabled = True
btnOFF.Enabled = False
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
很愚的我意识到一个问题,像我的这种要求应该是嵌入(embed)文件而不是读取文件吧???请教各位大大我的认识对不对...... 展开
3个回答
展开全部
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 Select
Try
If File.Exists(path) = True Then
Dim fs As New FileStream(path, op)
Dim sr As New StreamWriter(fs)
sr.WriteLine(msg)
sr.Close()
fs.Close()
writetext = "操作完成!"
Else
writetext = "操作失败!"
End If
Catch ex As Exception
writetext = "操作失败!"
End Try
End Function
参考这个吧
'
'vb.net源代码来自www.c-pet.com
'
读取指定文件
'
'读取指定文本文件
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 Select
Try
If File.Exists(path) = True Then
Dim fs As New FileStream(path, op)
Dim sr As New StreamWriter(fs)
sr.WriteLine(msg)
sr.Close()
fs.Close()
writetext = "操作完成!"
Else
writetext = "操作失败!"
End If
Catch ex As Exception
writetext = "操作失败!"
End Try
End Function
参考这个吧
'
'vb.net源代码来自www.c-pet.com
'
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询