重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

关于vb.nettxt的信息

请教在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文件中的数据?

1、新建一个标准的VB EXE工程,只有一个Form,Form上有两个按钮:Command1和Command2。

2、双击Command1添加如下代码

Private Sub Command1_Click()

Dim strFile     As String

Dim intFile     As Integer

Dim strData     As String

strFile = "c:\学生成绩.txt"

intFile = FreeFile

Open strFile For Input As intFile

strData = StrConv(InputB(FileLen(strFile), intFile), vbUnicode)

Debug.Print strData

Close intFile

End Sub

3、按F8开始单步调试代码,点击Command1,进入单步调试功能,

4、多次按下F8或直接按下F5运行完成,就完成了读取文本文件内容并输出到立即窗口。


文章标题:关于vb.nettxt的信息
分享路径:http://cqcxhl.com/article/dogeige.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP