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

时间:2015-06-07 17:05:58

标签: vb.net folderbrowserdialog

是VB.NET的新手。在我编译它的下面的代码中,当我检查选择 Radiobutton并浏览文件夹并且clikc 生成HL7消息时出现错误,我收到错误“< strong>错误:表达式是一个值,因此不能成为作业的目标。“当我检查默认 Radiobutton时,它就像一个魅力。但当我检查选择 Radiobutton时出现错误。我不知道我的代码中有什么问题。您可以在以下网址中找到我的设计:[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

2 个答案:

答案 0 :(得分:0)

看看你的第二个条件,RadioBtnselect声明。你永远不会告诉StreamWriter路径,因此你的错误。你确实给了它第一个路径,但不是第二个路径。

例如:

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

答案 1 :(得分:0)

确切的错误消息是由此行引起的

 file.WriteLine = folderBrowser.SelectedPath

WriteLine是一种方法而非财产。语法应该是WriteLine(....)。

在任何情况下,您的代码都会失败,因为Select Case中使用的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(.....)
        .....