Reputation: 11
I'm trying to create a text file with Visual Studio in VB.Net, and I want the filename to be the current date and time.
I have this code:
Dim d As System.DateTime
DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
Dim filepath As String = d + ".txt"
If Not System.IO.File.Exists(filepath) Then
System.IO.File.Create(filepath).Dispose()
End If
But every time I use d
it gives me the System.NotSupportedException
error. I can only create a text file by specifying the filename. How can i fix it and make my text file's name the current date and time?
Upvotes: 0
Views: 3708
Reputation: 4534
Your code fails to assign a value to the variable called d
. Also, you want d
to be a string, not a DateTime. Finally, the /
and :
characters in from your date and time are not valid in a windows filename. Either remove them or replace them with valid characters. Replace your first three lines of code with:
Dim d As String = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss")
Dim filepath As String = d & ".txt"
Upvotes: 2
Reputation: 31
You are trying with '/' and ':' in file name, while creating file this special characters not allowed.
Dim dateString = DateTime.Now.ToString("yyyyMMdd_HH_mm_ss")
Dim filepath As String = dateString + ".txt"
If Not System.IO.File.Exists(filepath) Then
System.IO.File.Create(filepath).Dispose()
End If
Upvotes: 1