如何使用 FolderBrowserdialog 将文本框数据保存到文件

How to save textbox data to file using FolderBrowsedialog

我是 VB.NET 的新手。在下面的代码中,当我编译它时,当我检查 Select 单选按钮并浏览文件夹并点击 GENERATE HL7 Message[= 时出现错误19=] 当我检查 Default Radiobutton 它就像一个魅力。但是当我检查 Select 单选按钮时出现错误。我不知道我的代码有什么问题。你可以在下面URL找到我的设计:[URL=http://s1065.photobucket.com/user/Izaz_Ahmed/media/Capture_zpst4jjgvxb.jpg.html]

Private Sub HL_Click(sender As Object, e As EventArgs) Handles HL.Click

        If vld(TxtProcode) = False Then
            Exit Sub
        End If

        Dim file As System.IO.StreamWriter
        Dim folderBrowser As New FolderBrowserDialog
        Dim fileDateTime As String = DateTime.Now.ToString("yyyyMMdd") & DateTime.Now.ToString("HHmmss") & ".HL7"
        Dim ts As String = DateTime.Now.ToString("yyyyMMdd") & DateTime.Now.ToString("HHmmss")
        'file = My.Computer.FileSystem.OpenTextFileWriter("C:\pdata\New folder\" & fileDateTime, True)
        folderBrowser.ShowNewFolderButton = True
        If RadioBtndefault.Checked Then
            TxtDob.Format = DateTimePickerFormat.Custom
            TxtDob.CustomFormat = "yyyyMMdd"
            TxtExamtime.Format = DateTimePickerFormat.Custom
            TxtExamtime.CustomFormat = "hhMMss"
            TxtExamdate.Format = DateTimePickerFormat.Custom
            TxtExamdate.CustomFormat = "yyyyMMdd"
            file = My.Computer.FileSystem.OpenTextFileWriter("C:\pdata\New folder\" & fileDateTime, True)
            file.WriteLine("MSH|^~\&|||||" & TxtExamdate.Text & "" & TxtExamtime.Text & "||ORM^O01||P|2.3.1")
            file.WriteLine("PID|||" & TxtId.Text & "||" & TxtFamilyname.Text & "^" & TxtGivenname.Text & "||" & TxtDob.Text & "||" & TxtGender.Text & "|||" & TxtStreet.Text & " " & TxtHouse.Text & "^^" & TxtCity.Text & "^^" & TxtPostcode.Text)
            file.WriteLine("PV1||O|||||||||||||||||" & TxtId.Text & "|||||||||||||||||||||||||" & ts)
            file.WriteLine("ORC|NW|" & ts & "|||||^^^S||" & TxtExamdate.Text)
            file.WriteLine("OBR||" & ts & "^" & ts & "||" & TxtProcode.Text & "|||" & TxtExamdate.Text & "" & TxtExamtime.Text & "|" & TxtExamdate.Text & "" & TxtExamtime.Text)
            file.WriteLine()
            file.Close()
        End If

        If RadioBtnselect.Checked Then
            If folderBrowser.ShowDialog() = DialogResult.OK Then
                file.WriteLine = folderBrowser.SelectedPath
                file.WriteLine("MSH|^~\&|||||" & TxtExamdate.Text & "" & TxtExamtime.Text & "||ORM^O01||P|2.3.1")
                file.WriteLine("PID|||" & TxtId.Text & "||" & TxtFamilyname.Text & "^" & TxtGivenname.Text & "||" & TxtDob.Text & "||" & TxtGender.Text & "|||" & TxtStreet.Text & " " & TxtHouse.Text & "^^" & TxtCity.Text & "^^" & TxtPostcode.Text)
                file.WriteLine("PV1||O|||||||||||||||||" & TxtId.Text & "|||||||||||||||||||||||||" & ts)
                file.WriteLine("ORC|NW|" & ts & "|||||^^^S||" & TxtExamdate.Text)
                file.WriteLine("OBR||" & ts & "^" & ts & "||" & TxtProcode.Text & "|||" & TxtExamdate.Text & "" & TxtExamtime.Text & "|" & TxtExamdate.Text & "" & TxtExamtime.Text)
                file.WriteLine()
                file.Close()
                Dim root As Environment.SpecialFolder = folderBrowser.RootFolder
            End If
        End If
End Class

看你的第二个条件,RadioBtnselect语句。你永远不会告诉 StreamWriter 路径,因此你的错误。你确实在你的第一个中给了它一条路径,但在第二个中没有。

例如:

file = My.Computer.FileSystem.OpenTextFileWriter("C:\pdata\New folder\" & fileDateTime, True)

确切的错误信息是由这一行引起的

 file.WriteLine = folderBrowser.SelectedPath

WriteLine 是一种方法,而不是 属性。语法应该是 WriteLine(....)。

在任何情况下,您的代码都会失败,因为在 Select 情况下使用的 StreamWriter 没有像在第一种情况下那样正确初始化。
您需要这样的东西,将 SelectedPath 与您想要的文件名

结合起来
If RadioBtnselect.Checked Then
    If folderBrowser.ShowDialog() = DialogResult.OK Then
        Dim destFile = Path.Combine(folderBrowser.SelectedPath,fileDateTime)
        file = My.Computer.FileSystem.OpenTextFileWriter(destFile,True)      
        file.WriteLine(.....)
        .....