5

I having some issues with saving Application settings during the run time...

If i change the setting scope to user, it works fine, but in application scope, nothing being happened...

I've used :

Properties.Settings.Default.Save();

any ideas ?

thanks

Igal
  • 4,603
  • 14
  • 41
  • 66
  • 1
    By design, only settings with User scope can be modified by your code. You change Application scope settings by editing the .config file. Which normally requires admin privileges to get write access to the file. – Hans Passant Mar 08 '12 at 13:15

3 Answers3

11

That's because setting the scope to Application makes it read-only.

See Using Settings in C#

Application-scope settings are read only, and can only be changed at design time or by altering the .exe.config file in between application sessions. User-scope settings, 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 changes to user settings between application sessions by calling the Settings.Save method.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
1

You can save and read setting like all advanced programs in Registry, and that is how to do it:

Public Function GetRegistryValue(ByVal KeyName As String, Optional ByVal DefaultValue As Object = Nothing) As Object
        Dim res As Object = Nothing
        Try
            Dim k = My.Computer.Registry.CurrentUser.OpenSubKey("Software\YourAppName", True)
            If k IsNot Nothing Then
                res = k.GetValue(KeyName, DefaultValue)
            Else
                k = My.Computer.Registry.CurrentUser.CreateSubKey("Software\YourAppName")
            End If
            If k IsNot Nothing Then k.Close()
        Catch ' ex As Exception
            'PromptMsg(ex)
        End Try
        Return res
    End Function

    Public Sub SetRegistryValue(ByVal KeyName As String, ByVal _Value As Object)
        Try
            Dim k = My.Computer.Registry.CurrentUser.OpenSubKey("Software\YourAppName", True)
            If k IsNot Nothing Then
                k.SetValue(KeyName, _Value)
            Else
                k = My.Computer.Registry.CurrentUser.CreateSubKey("Software\YourAppName")
                k.SetValue(KeyName, _Value)
            End If
            If k IsNot Nothing Then k.Close()
        Catch ' ex As Exception
            'PromptMsg(ex)
        End Try
    End Sub

Or even more you can make a serializable class ([Serializable()] attrib) that contains all of your settings as properties, then save it in your app directory, with the BinaryFormatter class.

  Public Sub saveBinary(ByVal c As Object, ByVal filepath As String)
        Try
            Using sr As Stream = File.Open(filepath, FileMode.Create)
                Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
                bf.Serialize(sr, c)
                sr.Close()
            End Using
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Function loadBinary(ByVal path As String) As Object
        Try
            If File.Exists(path) Then
                Using sr As Stream = File.Open(path, FileMode.Open)
                    Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
                    Dim c = bf.Deserialize(sr)
                    sr.Close()
                    Return c
                End Using
            Else
                Throw New Exception("File not found")
            End If
        Catch ex As Exception
            Throw ex
        End Try
        Return Nothing
    End Function
Amen Ayach
  • 4,288
  • 1
  • 23
  • 23
  • maybe i didn't explained myself properly, i'm not using Registry, but Properties.Settings.Default.Save(); – Igal Mar 08 '12 at 12:40
  • No, i know that `Properties.Settings` is pain so i give 2 others choices to save you app setting : Registry or Binary file – Amen Ayach Mar 08 '12 at 12:43
  • oh, i see :) but this i kind of problem... the application was already implemented by using Properties.Settings so i cannot change it now – Igal Mar 08 '12 at 12:45
0

Check this post out. You just refer to the application scoped settings like this:

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

Worked for me.

Community
  • 1
  • 1
Andreas
  • 705
  • 10
  • 22