none
Write to a file using button click in VB

    Question

  • Hi.
    I am needing help with on button click write the username, computer name, date, and time. Can anyone help with this?
    Tuesday, September 22, 2009 8:55 PM

Answers

  • Hi rboles,

    With one button on a FORM try this please and don't forget to type
     a new filename in to SAVE the infomation.

    :-)

    You could also try

    Now.ToShortDateString instead of Now.ToLongDateString

    also

    Now.ToShortTimeString instead of Now.ToLongTimeString


    Regards,

    John


    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim StringToWrite As String
    
            'the username, computer name, date, and time
            StringToWrite = System.Environment.UserName & "," & My.Computer.Name & "," & Now.ToLongDateString & "," & Now.ToLongTimeString
    
            Dim sfd As New SaveFileDialog
            sfd.Filter = "Text files only ( *.txt )|*.txt"
            sfd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
    
            Dim result As DialogResult = sfd.ShowDialog
    
            If result = Windows.Forms.DialogResult.OK Then
                If sfd.FileName <> String.Empty Then
                    Dim SelectedFilename As String = sfd.FileName
                    My.Computer.FileSystem.WriteAllText(SelectedFilename, StringToWrite, False)
                End If
            End If
    
            Process.Start("Notepad.exe", sfd.FileName)
    
        End Sub
    End Class

    • Edited by John Anthony Oliver Wednesday, September 23, 2009 12:30 AM Editd with a suggestion from Joe, forum user jwavila. Please see below. :-) ;-)
    • Marked as answer by rboles Thursday, September 24, 2009 12:42 PM
    Tuesday, September 22, 2009 11:08 PM

All replies

  • My.Computer.FileSystem.WriteAllText("C:\Test.txt", "Your String Here", False) 
    Just replace the first parameter with the location and name of the file you want to write.

    And replace the second parameter with the string you want to store. Maybe something like this:

    "UserName: " & userName & " ComputerName: " & computerName

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Tuesday, September 22, 2009 9:07 PM
  • Hi rboles,

    With one button on a FORM try this please and don't forget to type
     a new filename in to SAVE the infomation.

    :-)

    You could also try

    Now.ToShortDateString instead of Now.ToLongDateString

    also

    Now.ToShortTimeString instead of Now.ToLongTimeString


    Regards,

    John


    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim StringToWrite As String
    
            'the username, computer name, date, and time
            StringToWrite = System.Environment.UserName & "," & My.Computer.Name & "," & Now.ToLongDateString & "," & Now.ToLongTimeString
    
            Dim sfd As New SaveFileDialog
            sfd.Filter = "Text files only ( *.txt )|*.txt"
            sfd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
    
            Dim result As DialogResult = sfd.ShowDialog
    
            If result = Windows.Forms.DialogResult.OK Then
                If sfd.FileName <> String.Empty Then
                    Dim SelectedFilename As String = sfd.FileName
                    My.Computer.FileSystem.WriteAllText(SelectedFilename, StringToWrite, False)
                End If
            End If
    
            Process.Start("Notepad.exe", sfd.FileName)
    
        End Sub
    End Class

    • Edited by John Anthony Oliver Wednesday, September 23, 2009 12:30 AM Editd with a suggestion from Joe, forum user jwavila. Please see below. :-) ;-)
    • Marked as answer by rboles Thursday, September 24, 2009 12:42 PM
    Tuesday, September 22, 2009 11:08 PM
  • Hey John

    hope you've recovered from that other thread :)

    instead of My.User.Name and taking a Substring, you can also use System.Environment.UserName


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Label1.Text = "Computer: " & My.Computer.Name & vbCrLf & _
                            "User: " & System.Environment.UserName & vbCrLf & _
                            "Date: " & Now.ToString
        End Sub


    Wednesday, September 23, 2009 12:11 AM
  • Hi Joe,

    I had another severe attack of pain earlier so I didn't go to work this evening.  :-(   :-[   :-{

    I'd rather not talk about that "other thread" as I am now avoiding posting to any threads
     where and when a certain other forum member chooses to add a post. Even if I feel
     like doing so.

    Thanks for asking though. :-)

    Thanks too for the info.>> System.Environment.UserName

    ;-)    :-)  So I will edit my earlier post as that is much neater.  :-)

    It is now 01:20 here in the U.K. so I may try going off to bed around 3am.


    Regards,

    John

    I'm currently looking for work in Vb.Net software development. :-)
    Wednesday, September 23, 2009 12:20 AM
  • Thanks to everyone who responded.  I need to the file to write to my server so I can have a log of the users that accepted the terms of service and computer usage agreement.  So can I just point to path i.e. \\server\sharedfolder\folder . I am using John example.  Thanks in advance.

    Wednesday, September 23, 2009 7:22 PM
  • Thanks to everyone who responded.  I need to the file to write to my server so I can have a log of the users that accepted the terms of service and computer usage agreement.  So can I just point to path i.e. \\server\sharedfolder\folder . I am using John example.  Thanks in advance.


    Hi rboles,

    Please start a new question like;

    "How do I write the following information to my server?"

    Refer your question to this thread using this link please.>>

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/6e47ef45-86db-4013-bd15-14e4d2ba2019




    Regards,

    John
     

    I'm currently looking for work in Vb.Net software development. :-)
    Wednesday, September 23, 2009 10:55 PM
  • Hello everyone,

    How do I write the following information to my server? that was dicussed in the following thread http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/6e47ef45-86db-4013-bd15-14e4d2ba2019

    Thanks to everyone who has helped.
    Thursday, September 24, 2009 12:45 PM
  • Hello everyone,

    How do I write the following information to my server? that was dicussed in the following thread http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/6e47ef45-86db-4013-bd15-14e4d2ba2019

    Thanks to everyone who has helped.

    Hi rboles,

    Try clicking on the ASK A QUESTION to re-post your above post.

    It near the top-left of the forum website page.>>




    Regards,

    John

    P.S. By asking a new question it keeps threads shorter which, I believe helps
     other forum members to find answers quicker.

    :-)

    I'm currently looking for work in Vb.Net software development. :-)
    Thursday, September 24, 2009 1:23 PM
  • Thanks John I am new at this.
    Thursday, September 24, 2009 2:17 PM