Vb.net 如何在Visual Basic中执行另存为(V11 2012)
我正在寻找一个“另存为”对话框来处理我在Visual Basic中制作的应用程序的保存 这是我正在使用的代码,我也不理解该代码:Vb.net 如何在Visual Basic中执行另存为(V11 2012),vb.net,dialog,save,streamwriter,save-as,Vb.net,Dialog,Save,Streamwriter,Save As,我正在寻找一个“另存为”对话框来处理我在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
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插入符号问题?也,
我怎样才能让用户选择保存位置而不是代码的一部分
问题3:如何将用户在“另存为”对话框中指定的文件路径放入变量中
任何帮助,哪怕只是回答我的一个问题,都将不胜感激!
谢谢 看看这个例子是否有帮助:
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
看看这个例子是否有帮助:
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
这很有效,我对它以及它是如何工作的有了更好的理解。谢谢但是,只剩下一个问题,它仍然在一行上保存到文本文件中。可能是因为我正在编写的程序正在文本框中自动输入文本(不是用户输入的)。此外,它是一个richtextbox。将WordWrap设置为False,将MultiLine设置为True。然后该文件应该与文本框中的文件相似。如果它是RichTextBox,则可以使用
RichTextBox1.SaveFile(sfd.FileName)
,并完全跳过StreamWriter。RichTextBox属性已设置为WordWrap False和Multiline True。仍然将文本文件写在一行上。你能发布RichTextBox的屏幕截图及其内容吗?效果很好,我对它以及它的工作原理有了更好的理解。谢谢但是,只剩下一个问题,它仍然在一行上保存到文本文件中。可能是因为我正在编写的程序正在文本框中自动输入文本(不是用户输入的)。此外,它是一个richtextbox。将WordWrap设置为False,将MultiLine设置为True。然后该文件应该与文本框中的文件相似。如果它是RichTextBox,则可以使用RichTextBox1.SaveFile(sfd.FileName)
,并完全跳过StreamWriter。RichTextBox属性已设置为WordWrap False和Multiline True。仍然将文本文件写在一行上。您可以发布RichTextBox的屏幕截图及其内容吗?