vb.net 如何在 Visual Basic (V11 2012) 中执行另存为

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16369960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:29:14  来源:igfitidea点击:

How to do a Save As in Visual Basic (V11 2012)

vb.netdialogsavestreamwritersave-as

提问by user2348797

I'm looking to do a "Save As" dialog box to handle my saves from an application I am making in Visual Basic.

我正在寻找一个“另存为”对话框来处理我在 Visual Basic 中制作的应用程序的保存。

This is the code I am using and I don't understand it the code either:

这是我正在使用的代码,我也不明白它的代码:

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

Problem 1: I'm newer at this, no source gives me an in-depth enough explanation to understand what's really going with any saving method. I need a solid explanation, I'm lost here.

问题 1:我在这方面比较新,没有任何来源给我足够深入的解释来了解任何保存方法的真正情况。我需要一个可靠的解释,我在这里迷路了。

Problem 2: I can get the code to write to a text file at a location, but return carets are lost and it saves to the text file all on one line, using this:

问题 2:我可以将代码写入某个位置的文本文件,但返回的插入符号丢失,并且使用以下命令将其全部保存到文本文件中:

My.Computer.FileSystem.WriteAllText("c:\savelocation", TextBox1.Text, False

How do I fix the retun caret problem? Also, How would I make it so the user selects the save location instead of that being part of the code?

如何解决返回插入符问题?另外,我将如何让用户选择保存位置而不是作为代码的一部分?

Problem 3: How do I get the file path specified by the user, in the save as dialog, into a variable?

问题 3:如何将用户指定的文件路径(在另存为对话框中)放入变量中?

Any help, even just an answer to one of my questions would be appreciated! Thanks!

任何帮助,即使只是回答我的一个问题,将不胜感激!谢谢!

回答by Idle_Mind

See if this example helps:

看看这个例子是否有帮助:

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

回答by hana

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