Save Settings in Vb.Net or C#

How to save settings made in VB?

Yes you have multiple options, you can create a file and load it on startup, or you can use the built in settings object. Just go to your Project Property's --> settings set your variable there, the you can access it through My.Settings

Sample Image

Then load it like this:

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim greeting As String = My.Settings.mySetting
End Sub

you would change the value and save it like this

My.Settings.mySetting = "GoodBye"
My.Settings.Save()

What is the easiest way to save settings in VB.net

Use Visual Studio's Settings Designer, Right click on your project then properties, navigate to the settings section, from there, you can add anything you want.
then you can access your settings from code like so

My.Settings.nameOfSettingsEntry = value
My.Settings.Save()

you'll find more details and screenshots here,

How can I save application settings in a VB .NET application?

In VB, you use My.Settings rather than Properties.Settings.Default.

Also, you don't necessarily need to call Save explicitly as it will happen automatically, by default, when the application exits. It won't happen if the application crashes but, if you have done the right thing and handled the UnhandledException event, the application will not crash, even if an unhandled exception is thrown.

How to save an application setting item

Right click on the name of your program in Solution Explorer and select Properties. Click the Settings tab.

Sample Image

Enter MMABooksConnectionString as the Name, select (Connection string) in the Type dropdown and Scope as Appliction. You can scope to application since I don't think this will be changing.

How to use settings in Visual C#

Settings are stored under the Application folder and as such, use that as their namespace.

int myInt = Properties.Settings.Default.myVariable;
Properties.Settings.Default.myVariable = 12;
Properties.Settings.Default.Save();

Using Settings in C#

how to save settings in vb.net

Here you go: http://www.xtremevbtalk.com/showthread.php?t=131671

Where are application settings saved?

.NET has to do something special, it has to give you a guarantee that another program that also happens to have a "LastRuntime" setting doesn't overwrite the value that's stored for your program. To do that, it stores a user.config file in a directory that's hard to find back. It has a weirdo name, like

C:\Users\username\AppData\Local\WindowsFormsApplication1\WindowsFormsApplication1._Url_twchbbo4atpsvjpauzkgkvesu5bh2aul\1.0.0.0\user.config

Note how the project name is part of the path, that's one way to find it back. The unspeakable part of the name is a hash, created from various properties of your project that make your app unique enough to not collide with another .NET program, even if the name matches. Like your product name, company name, exe name, etcetera.

Note how the converse is true as well, changing such a property makes you lose your user.config file. So if "LastRuntime" is some kind of license metering value then using a setting isn't the best idea.

How can I save application settings in a Windows Forms application?

If you work with Visual Studio then it is pretty easy to get persistable settings. Right click on the project in Solution Explorer and choose Properties. Select the Settings tab and click on the hyperlink if settings doesn't exist.

Use the Settings tab to create application settings. Visual Studio creates the files Settings.settings and Settings.Designer.settings that contain the singleton class Settings inherited from ApplicationSettingsBase. You can access this class from your code to read/write application settings:

Properties.Settings.Default["SomeProperty"] = "Some Value";
Properties.Settings.Default.Save(); // Saves settings in application configuration file

This technique is applicable both for console, Windows Forms, and other project types.

Note that you need to set the scope property of your settings. If you select Application scope then Settings.Default.<your property> will be read-only.

Reference: How To: Write User Settings at Run Time with C# - Microsoft Docs

VB.NET How to save Program settings inside class module

You can make your own settings file using a binary serializer.

This method can be used to store an instance of your settings class to a file, which is not very human-readable. If human readability and editability is required, you could use an xml serializer instead. The settings file will reside in the application directory. You can control this with the variable settingsFileName.

Create a new console application and paste the code below. Run it a couple of times and note that the "connection string" is persisted through application close and open.

Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary

Module Module1

Private settingsFileName As String = "Settings.bin"
Private mySettingsClass As SettingsClass

Private Sub loadSettings()
Dim formatter As New BinaryFormatter()
If File.Exists(settingsFileName) Then
Using stream As New FileStream(settingsFileName, FileMode.Open)
mySettingsClass = CType(formatter.Deserialize(stream), SettingsClass)
End Using
Else
Using Stream As New FileStream(settingsFileName, FileMode.CreateNew)
mySettingsClass = New SettingsClass()
formatter.Serialize(Stream, mySettingsClass)
End Using
End If
End Sub

Private Sub saveSettings()
Dim formatter As New BinaryFormatter()
If File.Exists(settingsFileName) Then
Using stream As New FileStream(settingsFileName, FileMode.Truncate)
formatter.Serialize(stream, mySettingsClass)
End Using
Else
Using stream As New FileStream(settingsFileName, FileMode.CreateNew)
formatter.Serialize(stream, mySettingsClass)
End Using
End If
End Sub

<Serializable>
Public Class SettingsClass
Public Property ConnectionString As String = ""
End Class

Sub Main()
Console.WriteLine("Loading settings...")
loadSettings()
Dim connectionString = mySettingsClass.ConnectionString
Console.WriteLine("Connection string: ""{0}""", connectionString)
Console.WriteLine("Enter new connection string...")
mySettingsClass.ConnectionString = Console.ReadLine()
Console.WriteLine("Saving settings...")
saveSettings()
Console.WriteLine("Done!")
Console.ReadLine()
End Sub

End Module

Add additional properties to SettingsClass which can be used elsewhere in your application.



Related Topics



Leave a reply



Submit