如何在 Visual Basic 中执行另存为 (V11 2012)
Posted
技术标签:
【中文标题】如何在 Visual Basic 中执行另存为 (V11 2012)【英文标题】:How to do a Save As in Visual Basic (V11 2012) 【发布时间】:2013-05-04 02:22:56 【问题描述】:我正在寻找一个“另存为”对话框来处理我在 Visual Basic 中制作的应用程序的保存。
这是我正在使用的代码,我也不明白代码:
Private Sub MenuExportTo_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MenuExportTo.Click
Dim myStream As IO.Stream 'I don't understand this line
Dim saveFileDialog1 As New SaveFileDialog() 'I don't understand this line
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
'I understand this
saveFileDialog1.FilterIndex = 1 ' I understand this
saveFileDialog1.RestoreDirectory = True ' I understand this
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
' I don't understand the rest
myStream = saveFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
' Code to write the stream goes here. (This was what the example
I referenced put here. the important code goes here, but the example was no help)
myStream.Close()
End If
End If
问题 1:我在这方面比较新,没有来源给我足够深入的解释来理解任何保存方法的真正含义。我需要一个可靠的解释,我迷路了。
问题 2:我可以将代码写入某个位置的文本文件,但返回的插入符号会丢失,它会在一行中保存到文本文件中,使用以下方法:
My.Computer.FileSystem.WriteAllText("c:\savelocation", TextBox1.Text, False
如何解决 retun caret 问题?还, 我将如何让用户选择保存位置而不是代码的一部分?
问题 3:如何将用户在另存为对话框中指定的文件路径放入变量中?
任何帮助,即使只是回答我的一个问题,也将不胜感激! 谢谢!
【问题讨论】:
【参考方案1】:看看这个例子是否有帮助:
Private Sub MenuExportTo_Click(sender As System.Object, e As System.EventArgs) Handles MenuExportTo.Click
Dim sfd As New SaveFileDialog() ' this creates an instance of the SaveFileDialog called "sfd"
sfd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
sfd.FilterIndex = 1
sfd.RestoreDirectory = True
If sfd.ShowDialog() = DialogResult.OK Then
Dim FileName As String = sfd.FileName ' retrieve the full path to the file selected by the user
Dim sw As New System.IO.StreamWriter(FileName, False) ' create a StreamWriter with the FileName selected by the User
sw.WriteLine(TextBox1.Text) ' Write the contents of TextBox to the file
sw.Close() ' close the file
End If
End Sub
【讨论】:
效果很好,我对它以及它的工作原理有了更好的理解。谢谢!但是,只剩下一个问题,它仍然保存在一行中的文本文件中。可能是因为我正在编写的程序会自动在文本框中输入文本(不是用户输入的)。此外,它是一个富文本框。 将 WordWrap 设置为 False,将 MultiLine 设置为 True。然后文件应该看起来像您在 TextBox 中的内容。 如果是RichTextBox,可以使用RichTextBox1.SaveFile(sfd.FileName)
,完全跳过StreamWriter。
Richtextbox 属性已设置为 WordWrap False 和 Multiline True。仍然将文本文件写入一行。
你能贴一张你的 RichTextBox 的截图吗?【参考方案2】:
Private Sub MenuExportTo_Click(sender As System.Object, e As System.EventArgs) Handles MenuExportTo.Click
Dim sfd As New SaveFileDialog() ' this creates an instance of the SaveFileDialog called "sfd"
sfd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
sfd.FilterIndex = 1
sfd.RestoreDirectory = True
If sfd.ShowDialog() = DialogResult.OK Then
Dim FileName As String = sfd.FileName ' retrieve the full path to the file selected by the user
Dim sw As New System.IO.StreamWriter(FileName, False) ' create a StreamWriter with the FileName selected by the User
sw.WriteLine(TextBox1.Text) ' Write the contents of TextBox to the file
sw.Close() ' close the file
End If
End Sub
【讨论】:
以上是关于如何在 Visual Basic 中执行另存为 (V11 2012)的主要内容,如果未能解决你的问题,请参考以下文章