Winform Application Settings: Unable to save application settings that were added during runtime

I am having trouble saving application settings for runtime ...

If I change the settings area for the user, it works fine, but nothing happens in the application area ...

I used:

Properties.Settings.Default.Save();

any ideas?

thank

+5
source share
3 answers

This is because setting the scope of the application makes it read-only.

See Using Settings in C #

.exe.config . , , . . , Settings.Save.

+10

, , :

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

([Serializable()]), , BinaryFormatter.

  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
+1

Mark this post . You simply reference application area settings, for example:

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

Worked for me.

0
source

All Articles