Resolved Writing actual time in line in text file

Ab99

Member
Joined
Dec 19, 2021
Messages
8
Location
Netherlands
Programming Experience
3-5
Now I get the streamwriter working.
The next what I should want is write before every line in the text file the date and time.

This is what I have:

Time adding:
                Dim fPath = "C:\TimingForHorses\Logging.txt"
                Dim afile As New IO.StreamWriter(fPath, True)
                afile.WriteLine("F4 verbinding probleem")
                afile.Close()

Example:

Line: F4 verbinding probleem

Needed: 06-02-2022, 12:52 F4 verbinding probleem
 
Firstly, while the code you presented is not wrong, you ignored my last recommendation in your previous thread that would improve it, i.e. make it more succinct:
VB.NET:
IO.File.AppendAllText("C:\TimingForHorses\Logging.txt", "F4 verbinding probleem" & Environment.NewLine)
If I recall correctly, you said that you wanted to use that file for logging so I would suggest that you write a method for that purpose and call that as needed, e.g.
VB.NET:
Public Sub LogMessage(message As String)
    IO.File.AppendAllText("C:\TimingForHorses\Logging.txt", message & Environment.NewLine)
End Sub
As for the question, it has nothing to do with writing to a file. You know how to write text to a file. Your question is how to get text containing the current date and time. That is also a two-stage process, i.e. get the current date and time and get a date and time in text. Both of those things you could have found with a search. The DateTime.Now property will give you the current date and time as a DateTime value and you can call ToString on any DateTime value to turn it into a String. You should learn about formatting date and time strings to get it exactly as you want. How you get that into your full text is up to you. You can use simple string concatenation, call String.Format or use string interpolation.
 
Firstly, while the code you presented is not wrong, you ignored my last recommendation in your previous thread that would improve it, i.e. make it more succinct:
VB.NET:
IO.File.AppendAllText("C:\TimingForHorses\Logging.txt", "F4 verbinding probleem" & Environment.NewLine)
If I recall correctly, you said that you wanted to use that file for logging so I would suggest that you write a method for that purpose and call that as needed, e.g.
VB.NET:
Public Sub LogMessage(message As String)
    IO.File.AppendAllText("C:\TimingForHorses\Logging.txt", message & Environment.NewLine)
End Sub
As for the question, it has nothing to do with writing to a file. You know how to write text to a file. Your question is how to get text containing the current date and time. That is also a two-stage process, i.e. get the current date and time and get a date and time in text. Both of those things you could have found with a search. The DateTime.Now property will give you the current date and time as a DateTime value and you can call ToString on any DateTime value to turn it into a String. You should learn about formatting date and time strings to get it exactly as you want. How you get that into your full text is up to you. You can use simple string concatenation, call String.Format or use string interpolation.


I do not get it right. Maybe I am not that smart.
This is what I have.
code:
        Label15.Text = DateTime.Now.ToString("dd-MM-yy")
               'result = 08-02-2022
        Label16.Text = "Log " & Label15.Text
               'result = Log 08-02-2022
        VarDate = "Log " & Label15.Text
               'result = Log 08-02-2022

        Dim fPath = "C:\TimingForHorses\Logging\log.txt"
        'How do I get the result in the place of log?

        'The result must be: "C:\TimingForHorses\Logging\Log 08-02-2022.txt"

I need a little help.........
 
You're asking us how to do something that you already know how to do. You obviously already know how to concatenate strings because you're doing it in the code you just posted. If you can concatenate your date string here:
VB.NET:
Label16.Text = "Log " & Label15.Text
what's stopping you doing it for the file path? It's just a string, so do the same thing, i.e. concatenate the folder path with the date with the file extension. Programming is all about using principles you already know in situations you haven't encountered before. You know how to concatenate strings so use that principle. It doesn't change because of what the strings represent.
 
Back
Top