vb.net文本编辑 vbs文本

vb.net使用打开对话框,打开一个文本文件显示在文本框中,然后对文本框中的信息进行编辑后读文件内容到TextBox :
Try
Using sr As StreamReader = New StreamReader("C:\\TestFile.txt")
textbox1.Text = sr.ReadToEnd()
End Using
Catch E As Exception
Console.WriteLine(E.Message)
End Try
修改完成后,保存到文件:
Using sw As StreamWriter = New StreamWriter("C:\\TestFile.txt")
sw.Write(textbox1.Text)
sw.Close()
End Using
其中,流构造的参数
New StreamReader("C:\\TestFile.txt")
New StreamWriter("C:\\TestFile.txt")
中的"C:\\TestFile.txt"可以用OpenFileDialog和SaveFileDialog的FileName属性替换.
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
Using sr As StreamReader = New StreamReader(openFileDialog1.FileName)
textbox1.Text = sr.ReadToEnd()
End Using
Catch E As Exception
Console.WriteLine(E.Message)
End Try
End If
保存的时候换成SaveFileDialog就好
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.TextsvbCrLf
Loop
r.Close()
End Sub
End Class
补充:你要把读出的数据赋值给一个变量,只要:声明一个变量为数值类型,然后只要读取一行就可以了,把这行数据经过转换成数值后赋给这个变量.
VB.NET修改txt文件指定行?.or Example:
1.txt文件内容如下:
The 1st line.
#The 2nd line.
The 3rd line.
The 4th line.
.
修改第二行内容,将#除去.修改后的文本如下:
The 1st line.
The 2nd line.
The 3rd line.
The 4th line.
1 输入方式打开原文件 1.txt;
2 输出方式打开新文件 2.txt;
3 逐行 Line Input 从 1.txt 中读数据,Print 写入2.txt , 直至要修改的行;
4 丢弃从 1.txt中读出的要修改的行,将新内容行写入2.txt;
5 仿照第 3 步,将其余的行从 1.txt 复制到2.txt 。
6 关闭两个文件
7 删除1.txt , 将2.txt 的文件名改为原来 1.txt 的 。
用vb.net编写记事本源代码Dim sFileName As String
Dim Search
Private Sub dateTimeMenu_Click()
Text1.Text = Now
End Sub
Private Sub deleteMenu_Click()
Text1.Text = Left(Text1.Text, Text1.SelStart) + Mid(Text1.Text, Text1.SelStart + Text1.SelLength + 1)
End Sub
Private Sub findMenu_Click()
Search = InputBox("请输入要查找的字词:")
Dim Where1 '获取需要查找的字符串变量

推荐阅读