winform's Application Settings : cannot save application settings which has been added during the run time
Asked Answered
D

3

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

Diphenylhydantoin answered 8/3, 2012 at 11:52 Comment(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.Rubbish
A
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.

Absorption answered 8/3, 2012 at 13:16 Comment(0)
S
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
Seawright answered 8/3, 2012 at 12:8 Comment(3)
maybe i didn't explained myself properly, i'm not using Registry, but Properties.Settings.Default.Save();Diphenylhydantoin
No, i know that Properties.Settings is pain so i give 2 others choices to save you app setting : Registry or Binary fileSeawright
oh, i see :) but this i kind of problem... the application was already implemented by using Properties.Settings so i cannot change it nowDiphenylhydantoin
G
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.

Guildsman answered 6/3, 2014 at 1:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.