ThePlacid
ThePlacid

Reputation: 43

Create a text file with a variable name and write to it

For an application i'm working on, i am trying to create a 'save data' feature.

First off, it needs to create a .txt file with a custom name, this needs to be the date (today) and the text of a textbox, it needs to be in the format of yyyymmdd_textbox1(.txt)

How would i go about doing this? it can create it where ever, but if it already exists it needs to append to it on a new line

thanks for any responses

Upvotes: 0

Views: 740

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460108

If you want a file-name from user-input you first need this method:

Public Function SanitizeFileName(fileName as String) As String  
    For Each c In IO.Path.GetInvalidFileNameChars()
        filename = filename.Replace(c, "_"c)
    Next
    Return fileName
End Function

Then it's easy

Dim filename = $"{DateTime.Today.ToString("yyyyMMdd")}_{SanitizeFileName(textbox1.Text)}.txt"

Upvotes: 3

Related Questions