VB.NET 如何实现打开文本加载到新编的程序
我在VB.NET中做了个简单记事薄
要怎么做才能实现打开文本直接加载到我编的程序里
文本内容跟名称也都要改变哦
[解决办法]
放一个textbox 一个button
public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Txt Files (*.txt)|*.txt"; ofd.Title = "打开txt文件"; if (ofd.ShowDialog() == DialogResult.OK) { Stream myStream; myStream = ofd.OpenFile(); //设置窗体标题 this.Text = ofd.FileName; using (StreamReader sr = new StreamReader(myStream)) { //导入文本内容 textBox1.Text = sr.ReadToEnd(); } } }
[解决办法]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim d As New OpenFileDialog
d.Filter = "Txt Files (*.txt)|*.txt"
d.Title = "打开txt文件"
Dim r As IO.StreamReader
If d.ShowDialog() = DialogResult.OK Then
r = New IO.StreamReader(d.OpenFile)
TextBox1.Text = r.ReadToEnd()
Me.Text = d.FileName
End If
End Sub
[解决办法]
相关代码贴出来如下,请参考:
Dim DialogOpen As New System.Windows.Forms.OpenFileDialog
DialogOpen.Multiselect = True '允许选择多文件
DialogOpen.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"
If DialogOpen.ShowDialog() = Windows.Forms.DialogResult.OK Then '多选OK后
Dim fileName As String
For Each fileName In DialogOpen.FileNames
System.Windows.Forms.Application.DoEvents() '释放控制权给程序的其他事件--不卡用的
' Dim outstrs As String = Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText(fileName, System.Text.Encoding.Default) '读取文件到字符串'
Dim outstrs As String = My.Computer.FileSystem.ReadAllText(fileName.ToString, System.Text.Encoding.Default)
FFFTEXTBOX.AppendText(outstrs)
Next