如何在我使用 FileSteam 对象的代码中实现 SaveFileDialog

How to implement a SaveFileDialog in my code, where I am using a FileSteam object

所以这是我的方法代码,但我似乎无法弄清楚如何在其中实现保存文件对话框...任何信息或指导将不胜感激。

Private Sub btSave_Click(sender As System.Object, e As System.EventArgs) Handles btSave.Click
        If (cbPeriod.SelectedItem IsNot Nothing) Then
            Try
                Using connect As New SqlConnection(connectionString)
                    Dim command As New SqlCommand()
                    command.CommandText = selectQuery
                    command.Parameters.AddWithValue("@Period", cbPeriod.SelectedItem.ToString)
                    command.Connection = connect
                    Dim fileName As String
                    fileName = "Data.txt"
                    Dim seqNo As Integer = 0
                    Dim currDocNo As String = ""
                    Dim prevDocNo As String = ""
                    Using FileObject As New FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)
                        Using StreamWriterObj As New StreamWriter(FileObject)
                            connect.Open()
                            Using reader As SqlDataReader = command.ExecuteReader()
                                Dim FieldCount As Integer = reader.FieldCount - 1
                                For i = 0 To FieldCount
                                    StreamWriterObj.Write(reader.GetName(i).ToString)
                                    StreamWriterObj.Write(" @ ")
                                Next
                                StreamWriterObj.WriteLine()
                                Do While reader.Read()
                                    currDocNo = reader.GetValue(reader.GetOrdinal("ДокументNo")).ToString
                                    StreamWriterObj.Write(reader.Item(0))
                                    For i = 1 To FieldCount
                                        currDocNo = reader.GetValue(reader.GetOrdinal("ДокументNo")).ToString
                                        If (reader.GetName(i).Equals("ПореденНомер", StringComparison.InvariantCultureIgnoreCase)) Then
                                            If (currDocNo = prevDocNo) Then
                                                seqNo += 1
                                                StreamWriterObj.Write(seqNo)
                                            Else
                                                seqNo = 1
                                                StreamWriterObj.Write(" @ ")
                                                StreamWriterObj.Write(seqNo)
                                            End If
                                        Else
                                            StreamWriterObj.Write(" @ ")
                                            StreamWriterObj.Write(reader.Item(i))
                                        End If
                                    Next
                                    prevDocNo = currDocNo
                                    StreamWriterObj.WriteLine()
                                Loop
                                reader.Close()
                            End Using
                            connect.Close()
                            MessageBox.Show("Export was successful.")
                        End Using
                    End Using
                End Using
            Catch ex As SqlException
                MessageBox.Show(ex.Message.ToString)
            End Try
        Else
            MessageBox.Show("Please select a value!")
        End If
    End Sub

如果您需要更多信息,请告诉我。如您所见,我有 File 对象和所有内容,所以我想我只需要添加一些 savefiledialog 但如何将 streamwriter 拥有的数据获取到 savefiledialog 中?

使用 SaveFileDialog 获取这样的文件名

Dim fileName As String
' fileName = "Data.txt"
Using sfd As New SaveFileDialog()
    sfd.Filter = "Text Files (*.txt)|*.txt"
    If sfd.ShowDialog() = Windows.Forms.DialogResult.OK Then
        fileName = sfd.FileName
    Else
        Throw New Exception("User canceled out of save dialog")
    End If
End Using

不要对包裹在 Using 块中的一次性物品显式调用 Close()。 Dispose 方法被调用并为您关闭它@ End Using

最后,您可以删除 FileStream 并使用 StreamWriter 创建文件,也可以使用 this constructor

' Using FileObject As New FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)
' Using StreamWriterObj As New StreamWriter(FileObject)
Using StreamWriterObj As New StreamWriter(path:=fileName, append:=False)