
正文
vb.net远程txt的简单介绍
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
如何用vb.net编写读取txt内容的代码?
窗体上添加2个文本框vb.net远程txt,设置成多行vb.net远程txt,2个按钮vb.net远程txt,在文本框1里随便输入若干文字,可以多行,单击按钮1,保存到文件。然后单击按钮2,把刚才写入vb.net远程txt的文件读到文本框2里。
代码如下vb.net远程txt:
'写文本文件
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'创建(写入)一个文本文件
Dim MyStream As New System.IO.FileStream(Application.StartupPath "\Ssk.txt", System.IO.FileMode.Create)
Dim MyWriter As New System.IO.StreamWriter(MyStream, System.Text.Encoding.Default)
MyWriter.WriteLine(TextBox1.Text)
MyWriter.Flush()
MyWriter.Close()
MyStream.Close()
End Sub
'读文本文件
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'读取一个文本文件
Dim MyReader As New System.IO.StreamReader(Application.StartupPath "\Ssk.txt", System.Text.Encoding.UTF8)
TextBox2.Text = MyReader.ReadToEnd()
MyReader.Close()
End Sub
气斜射入水或其他介质,折射光线与入射光线法线在
相关问答
Q1: VB.net窗体设计中,如何读取.txt文件中的数据?
1、新建一个标准的VB EXE工程vb.net远程txt,只有一个Formvb.net远程txt,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运行完成,就完成了读取文本文件内容并输出到立即窗口。
Q2: 如何用vb.net 打开一个既存的txt文档。
可以调用CMD
方法一:
Shell("cmd.exe /c call c:\新建文本文档.txt", AppWinStyle.NormalFocus)
缺点:不但会打开文本文件,同时还会显示一个cmd窗体。
方法二:
需要新建一个bat文件到资源里,输入start C:\新建文本文档.txt
代码
shell "bat文件的路径.bat"
这样就好了,两种方法各有好坏。一个会显示cmd窗体,一个需要bat文件。
Q3: 请教在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。
◆ 满意请采纳,谢谢 ◆
Q4: vb.net 如何打开txt文件?
说明:以下代码在Microsoft Visual Basic 2005 (简体中文版)中通过。
创建新项目:
在窗体上添加文本框2个:TextBox1,TextBox2
TextBox1 -- 用来编辑要写入的文本文件的内容,或显示打开的文本文件的内容
TextBox2 -- 用来输入要打开或要写入的文件名(包括盘符,路径)(例如:c:\123.txt)
在窗体上添加2个按钮:Button1,Button2
Button1 -- 写入文件
Button2 -- 打开文件
代码如下:
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim w As New StreamWriter(TextBox2.Text)
w.Write(TextBox1.Text)
w.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim r As New StreamReader(TextBox2.Text)
Dim s As String
TextBox1.Text = ""
Do While r.Peek -1 '是否到文件尾
s = r.ReadLine
' MessageBox.Show(r.Peek)
TextBox1.Text = TextBox1.Text s vbCrLf
Loop
r.Close()
End Sub
End Class
补充:你要把读出的数据赋值给一个变量,只要:声明一个变量为数值类型,然后只要读取一行就可以了,把这行数据经过转换成数值后赋给这个变量.
vb.net远程txt的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、vb.net远程txt的信息别忘了在本站进行查找喔。







