My program asks the user what their name is, but I need a way to find out how to only ask it on the first time running the program.
-
3You read and save information to a file or a database. – LarsTech Feb 20 '18 at 23:35
-
2...or in Settings. – Ňɏssa Pøngjǣrdenlarp Feb 20 '18 at 23:40
2 Answers
You can create a Boolean setting to keep track of whether or not the user has run the program before.
Right-click your project name in the Solution Explorer of Visual Studio. In the properties page, select the Settings tab on the left of the screen.
Here you can set the Name, Type, Score, and default Value of the setting. In your case, you could name the property "IsFirstTimeRun". For the type choose Boolean. For scope you want to pick User, not Application. The reason for this is that users can change the value of user-scoped settings can be changed by a user at run time whereas application-scoped settings cannot. Finally set the value to True.
At this point you should have something like this:
Now you have a Boolean property
My.Settings.IsFirstTimeRun
that you can use in your program.
If My.Settings.IsFirstTimeRun Then
System.Console.WriteLine("Hello, what is your name?")
' Update and save the value of the setting.
My.Settings.IsFirstTimeRun = False
My.Settings.Save()
Else
System.Console.WriteLine("Welcome back!")
End If
Further reading: Accessing application settings (Visual Basic)

- 61
- 4
You'll have to store the name somewhere. If you're app is connected to a database, then you naturally store it and get it back from the database, but it looks like your app is not connected to a database. You can store it in the settings app.config either the global file or the per-user file. Now, before the code where you ask the question, try to read the value from the setting file. If it exists, then don't ask the question and use the stored value. If it doesn't exist, then ask the question and save the answer in the settings file.

- 24,690
- 13
- 50
- 55