Filename, date. A VB.NET program can be used to generate a report on a daily basis. But where should it store this report? Sometimes a file (named with the current day) is a good place.
Some details, file names. By using some common String and DateTime functions, we can generate a file name for each day. Some surrounding characters can also be added.
Part 2 We build up the full path for the output file name. You can change this to point to a folder on your computer.
Part 3 We write a file using the full name and path we generated. The contents also contain the date.
Imports System.IO
Module Program
Sub Main()
' Part 1: generate file name from date.
Dim name As String = String.Format("log-{0:yyyy-MM-dd}.txt", DateTime.Now)
' Part 2: get full path for file.
Dim path As String = "C:\\programs\\" + name
' Part 3: write file with date in file name.
File.WriteAllText(path, "Today is " + DateTime.Today.ToString())
Console.WriteLine("DONE")
End Sub
End ModuleDONE
Notes, output. Let us consider the output of the program in the year 2020. I tested that the file name is correct. And the file contents also matches the current day (when tested).
File: log-2020-06-19.txt
Contents: Today is 6/19/2020 12:00:00 AM
A summary. Programming languages like VB.NET are often used for server-based applications, or for data analysis. Writing a daily file (from the results of a run) is a common requirement.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.