Read and Write Settings in Vb.net

MSDN Definition:

Settings that are application-scoped are read-only, and can only be changed at design time or by altering the .config file in between application sessions. Settings that are user-scoped, however, can be written at run time just as you would change any property value. The new value persists for the duration of the application session. You can persist the changes to the settings between application sessions by calling the Save method.

Hi,
In few years back i used to save user settings in text file, during runtime i have write a method to recall and overwrite the value, but Settings in .net make this process much easier.

Following method you can implement to use of settings

1

You can find the settings file under MyProject Folder, Double click the file and define the keys, type and value

My.Settings.PrinterName = prtDialog.PrinterSettings.PrinterName

using this we can access PrinterName settings over all application, if you want to persist the setting then you can use save method.

My.Settings.Save()

Sample Code

Private Sub SavePrinterSettings()
Dim prtDialog As New System.Windows.Forms.PrintDialog()
Dim PrinterName As String = String.Empty


PrinterName = My.Settings.PrinterName.ToString()


If Not (PrinterName = Nothing) Then
prtDialog.PrinterSettings.PrinterName = PrinterName


If Not (prtDialog.PrinterSettings.IsValid) Then
prtDialog.ShowDialog()
PrinterName = prtDialog.PrinterSettings.PrinterName.ToString()
My.Settings.PrinterName = PrinterName
End If
Else
prtDialog.ShowDialog()
PrinterName = prtDialog.PrinterSettings.PrinterName.ToString()
My.Settings.PrinterName = PrinterName
End If
My.Settings.Save()
End Sub

In this example i have used to save printer name. If printer name is not already exists then open new printer dialog and save printer name.

If you forget to call My.Settings.Save() method then settings only persist still the application is alive, after closing the application it will rollback the settings to the default.

Any queries, Leave a  comment

You can get sample from this link
http://jmp.sh/IBxWX46

Cheers..