从文本文件中取一个日期并将其放入日期时间选择器中

时间:2017-04-27 07:32:20

标签: vb.net date text-files datetimepicker

请帮助我,我想从日期时间选择器中选择一个日期到文本文件,然后再次加载程序我希望日期在日期时间选择器中。

    FileReader = New StreamReader("datefile.txt")
    Output = FileReader.ReadLine()
    FileReader.Close()


    Dim OutputDate As Date = Date.ParseExact(Output, "dd/MM/yyyy",
               System.Globalization.DateTimeFormatInfo.InvariantInfo)
    DateTimePicker1.Value = OutputDate
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    FileWriter = New StreamWriter("datefile.txt", False)
    FileWriter.WriteLine(DateTimePicker1.Value)
    FileWriter.Close()
    MessageBox.Show("Done!")
End Sub

结束班

1 个答案:

答案 0 :(得分:0)

确保你的“M”是正确的情况 - 大写M表示“月”,小写“m”表示分钟。还尝试一些错误捕获,并指定完整路径:

  Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Dim FileReader As StreamReader
    Try
      FileReader = New StreamReader("C:\junk\datefile.txt")
      Dim Output As String = FileReader.ReadLine()
      FileReader.Close()
      Dim OutputDate As Date = Date.ParseExact(Output, "dd/MM/yyyy",
               System.Globalization.DateTimeFormatInfo.InvariantInfo)
      DateTimePicker1.Value = OutputDate
    Catch ex As Exception
      MsgBox(ex.Message)
    End Try

  End Sub

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click    
    Dim FileWriter As New StreamWriter("C:\Junk\datefile.txt", False)
    FileWriter.WriteLine(DateTimePicker1.Value.ToString("dd/MM/yyyy"))
    FileWriter.Close()
    MessageBox.Show("Done!")
  End Sub