2013-12-11 70 views
0

大家早上好, 我正在使用visual basic处理我的决赛项目,我想实现一个保存文件选项。如何使用VB.net将特定文本框的内容保存到文件中

现在应用程序有2个文本框(不是richtextbox),它是信息的输入和输出。

我想要做的只是保存输出文本框的内容。我设法让它保存一些文件,但是当它打开时它总是变成空的。

保存文件按钮的代码示例如下,我感觉它没有保存内容,因为它没有指定,但我不知道如何指定只保存一个文本框的内容,即使使用许多小时的论坛/谷歌搜索iive完成尝试,并找出我自己的。

 Dim myStream As Stream 
    Dim nsavetxtoutput As New SaveFileDialog() 
    '|All files (*.*)|*.* 
    nsavetxtoutput.Filter = "txt files (*.txt)|*.text" 
    nsavetxtoutput.FilterIndex = 2 
    nsavetxtoutput.RestoreDirectory = True 

    If nsavetxtoutput.ShowDialog() = DialogResult.OK Then 
     myStream = nsavetxtoutput.OpenFile() 
     If (myStream IsNot Nothing) Then 
      ' Code to write the stream goes here. 
      myStream.Close() 
     End If 
    End If 

任何和所有的洞察力将不胜感激!

谢谢你们!


本程序允许使用此功能仅保存文本框的内容 - 非常感谢所有回复的人。它帮助了分配!

Private Sub NOTEPAD_BUTTON(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTSave2Notepad.Click 
    Dim nsavetxtoutput As New SaveFileDialog() 
    nsavetxtoutput.Filter = "txt files (*.txt)|*.text" 
    nsavetxtoutput.FilterIndex = 2 
    nsavetxtoutput.RestoreDirectory = True 

    If nsavetxtoutput.ShowDialog() = DialogResult.OK Then 
     IO.File.WriteAllText(nsavetxtoutput.FileName, txtoutput.Text) 
    End If 
End Sub 
+0

基本上我唯一想做的功能就是保存2个文本框之一的特定信息。然后允许用户用自己选择的名称和他选择的路径保存它。 – Viralwarrior012

+1

[如何使用vb.net将文本框值写入.txt文件](http://stackoverflow.com/questions/5002529/how-to-write-textbox-values-to-txt-file-with- vb-net) – SysDragon

回答

1

试试这个:

If nsavetxtoutput.ShowDialog() = DialogResult.OK Then 
    IO.File.WriteAllText(nsavetxtoutput.FileName, TextBox2.Text) 
End If 

哪里TextBox2是你的输出文本框。

更多信息请登录MSDN Documentation

+0

功能像魅力一样工作!必须删除savefiledialog选项打开,因为它是要输入秒,但现在它保存输出内容没有任何问题!谢谢sysdragon:D – Viralwarrior012

1

您可以使用File.WriteAllText方法写入文件。它需要两个参数。第一个是从SaveFileDialog获得的文件路径。第二个是你想要写入文件的值。

See the article on MSDN

+0

我相信我们有一个赢家!生病试试这个,生病回到你身边。 :P – Viralwarrior012

相关问题