visual basic写入文本文件

时间:2017-01-01 13:56:40

标签: visual-studio

我有一个关于在visual basic 2008中使用 StreamWriter 的问题 ,下面是使用visual basic 2008进行数据库过滤的代码

  On Error Resume Next

    If e.KeyCode = Keys.Enter Then
        ''  Me.Table1BindingSource.Filter = "EmpID = ' " & Me.txtsearch.Text & "'"
        On Error Resume Next
        Dim temp As Integer = 0
        Me.Table1BindingSource.Filter = "EmpID = ' " & Me.txtsearch.Text & "'"
        For i As Integer = 0 To Table1DataGridView.RowCount - 1
            For j As Integer = 0 To Table1DataGridView.ColumnCount - 1
                If Table1DataGridView.Rows(i).Cells(j).Value.ToString = txtsearch.Text Then
                    ''if item found then we play sound ok
                    My.Computer.Audio.Play("F:\beep.wav", AudioPlayMode.WaitToComplete)
                    My.Computer.Audio.Play("F:\beep.wav", AudioPlayMode.WaitToComplete)
                    temp = 1

                    ''write the user name that logged in
                    Dim file As System.IO.StreamWriter
                    file = My.Computer.FileSystem.OpenTextFileWriter("C:\Users\haydeer\Desktop\test.txt", True)
                    file.WriteLine(txtsearch.Text)
                    file.Close()
                    txtsearch.Text = ""

                End If
            Next
        Next
        '' trytime += 1 'Increment if not found
        If temp = 0 And trytime <= 2 Then 'Check if not found 3 times (or more)

            ''if item not found then we play sound err
            My.Computer.Audio.Play("F:\computer_access.wav", AudioPlayMode.WaitToComplete)
            Me.Table1TableAdapter.Fill(Me.MydbDataSet.Table1)
            Me.Table1DataGridView.Refresh()
            trytime += 1 'Increment if not found
            txtsearch.Text = ""

        ElseIf temp = 0 And trytime >= 3 Then
            '' Alarm Gose on
            MsgBox("three time")

        End If
    End If

我的问题是,当用户登录时,它会写入该用户的登录日期,iam成功存储用户,但我也想存储日期,例如(00:00:00 AM)?任何想法?

1 个答案:

答案 0 :(得分:0)

要将机器上的当前时间转换为字符串:

My.Computer.Clock.LocalTime.ToLongTimeString(上午1:23:45)

My.Computer.Clock.LocalTime.ToShortTimeString(上午1:23)

My.Computer.Clock.LocalTime.ToLongDateString(2000年1月1日,星期六)

My.Computer.Clock.LocalTime.ToShortDateString(1/1/2000)

My.Computer.Clock.LocalTime.ToString(2000年1月1日上午1:23:45) 我建议这个

-镁