vb.net读数据文件 vb读取文件内容

VB.net 读取文本文件?1、实现上传按钮方法代码 。
2、判断图片对象是否为空代码 。
3、取得数据库字段 dt.Rows(0)("Pic")方法代码 。
4、字节数组转换为Image类型方法代码 。
5、处理SQL中操作Image类型方法代码 。
6、实现vb.net读数据文件的上传结果 。
vb.net 怎样读取文件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中如何将数据写入txt文件、再从txt文件读出?软糖来告诉你吧 。
VB.net中读写文件主要使用System.IO命名空间 。
① 使用 File.ReadAllText 读取
Dim s As String = System.IO.File.ReadAllText("C:\a.txt")
② 使用 StreamReader 读取,注意编码格式和写入的编码保持一致 。
Dim sr As StreamReader = New StreamReader("C:\a.txt", System.Text.Encoding.UTF8)
Dim s As String = sr.ReadToEnd()
sr.Close()
③ 使用 File.WriteAllText 写入 , 会覆盖同名的文件 。
Dim 要写的内容 As String = ""
File.WriteAllText(文件路径, 要写的内容, System.Text.Encoding.UTF8)
④ 使用 StreamWriter 写入 。
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter("C:\a.txt", False,System.Text.Encoding.UTF8)
sw.WriteLine(TextTB.Text)
sw.Close()
⑤ 使用 StreamWriter 追加写入 。
将上面代码的第二个参数False改为True 。
◆ 满意请采纳,谢谢 ◆
VB.NET读取TXT文件数据保存为数组VB.NET编程读取txt文本文档中的数据,并把数据保存为数组,代码如下:
'写配件文件
Private Sub saveIni(ByVal filePath As String, ByVal str As String)
Dim sw As StreamWriter = New StreamWriter(filePath, True) 'true是指以追加的方式打开指定文件
sw.WriteLine(str)
sw.Flush()
sw.Close()
sw = Nothing
End Sub
'读配件文件
Private Function readIni(ByVal filePath As String)
Dim iniDt As New DataTable
iniDt.Columns.Add("text")
iniDt.Columns.Add("value")
Try
Dim sr As StreamReader = New StreamReader(filePath, System.Text.Encoding.Default)
Dim line As String = ""
While Not sr.EndOfStream
Dim str = sr.ReadLine()'读取当前行
iniDt.Rows.Add(New String() {
str(0),
str(1)
})
End While
sr.Close()
sr = Nothing
Catch ex As Exception
End Try
Return iniDt
End Function
VB.net 如果读取txt数据(或十进制dat数据)vb.net虽也有input语句,但一次只能读取到一个变量中,可以用TextFieldParser类代替,但似乎没以前的方便 。不过比以前的更灵活 。写入文件Write还是可以用,在Microsoft.VisualBasic.FileIO中 。

推荐阅读