none
Save Application Settings 2017??? RRS feed

  • Question

  • About 8 years ago, I created my first VB.NET application and needed to store application settings that were configured by the user during install.  The user was unable to write to the application settings because they are read-only at runtime.  I had to take the old school ini file approach and store it in the Program Data folder.

    In the past 8 years, have there been any improvements for this?  I don't want to store the setting in a database.  I would be ok with the registry if a basic AD user would be able to write to it.

    I searched the forum and could only find very old posts showing how to save application settings with the My.settings.SettingName = etc... approach but this stopped working in .NET right around 2010.

    Thanks

     

     
    Friday, October 6, 2017 7:33 PM

All replies

  • Phill,

    You can always roll your own with a serializable class for persistence but doing that "because Application Settings don't work" isn't correct.

    As far as I know, they work fine up to and including the latest version.

    Do you have information showing otherwise? With so many others using it it's unimaginable that they "don't work".

    Show an example please?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, October 6, 2017 7:44 PM
  • I searched the forum and could only find very old posts showing how to save application settings with the My.settings.SettingName = etc... approach but this stopped working in .NET right around 2010.

    It didn't stop working.   What might have happened is that you are using application settings instead of user settings.  My.Settings.SettingName should be a user setting, not an application setting, and it still works just fine.   If you are having a problem using it then you can describe how you have set it up and what problem you are having.

    Friday, October 6, 2017 8:05 PM
  • I think you misunderstood my post.  Application settings work with the exception that a user can't set or modify them.

    Imagine I create an application scope setting called location.  When the user runs the application for the very first time, they are asked to select their location from a dropdown box.  The location setting needs to be stored somewhere locally on the PC and not per user.

    You can't do it!  When I enter the line of code

    My.Settings.Location = "Test"

    I get the error Property 'Location' is 'ReadOnly'

    It's the same problem I ran into 8 years ago.


    • Edited by Phill D Friday, October 6, 2017 8:09 PM
    Friday, October 6, 2017 8:08 PM
  • I think you misunderstood my post.  Application settings work with the exception that a user can't set or modify them.

    Imagine I create an application scope setting called location.  When the user runs the application for the very first time, they are asked to select their location from a dropdown box.  The location setting needs to be stored somewhere locally on the PC and not per user.

    You can't do it!  When I enter the line of code

    My.Settings.Location = "Test"

    I get the error Property 'Location' is 'ReadOnly'

    It's the same problem I ran into 8 years ago.


    Please make it obvious who you're talking to; we don't all see this forum the same way and I can't tell.

    *****

    That sounds like you have the wrong scope set. Look again and tell us what the scope is set for please?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, October 6, 2017 8:13 PM
  • I know it works for a user scope, but the setting needs to be saved for all users who login to the PC.  It's a domain based network and numerous users log in and out of the same PC.  If a user logged in and accidently set the wrong setting, it would cause numerous problems with the programs operation.

    Imagine if you had to enter the activation code for MS office every time a new user logged on to a PC.  It's the same concept, I just need to set the setting one time and it be configured for all users.

    Friday, October 6, 2017 8:14 PM
  • I know it works for a user scope, but the setting needs to be saved for all users who login to the PC.  It's a domain based network and numerous users log in and out of the same PC.  If a user logged in and accidently set the wrong setting, it would cause numerous problems with the programs operation.

    Imagine if you had to enter the activation code for MS office every time a new user logged on to a PC.  It's the same concept, I just need to set the setting one time and it be configured for all users.

    Then you probably should "roll your own" and save the data to the CommonApplicationData directory:

    https://msdn.microsoft.com/en-us/library/system.environment.specialfolder(v=vs.110).aspx

    You'll need to create that directory and/or test that it exists before persisting anything to it.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, October 6, 2017 8:17 PM
  • Yeah, that's very similar to what I had to do before.  I just thought some progress may have been made to simplify this problem in recent years.  I mean, application scope settings (configured by the user at install or runtime) aren't uncommon.  
    Friday, October 6, 2017 8:20 PM
  • Aren't user settings specific to the user who created them?  So if the user logs off and another user logs on, they would also have to configure the location?
    Friday, October 6, 2017 8:23 PM
  • Hi

    Did you create the setting first?

    I am wondering if you are confusing My.Settings with Application settings.

    Just in case, I will describe the use of a My.Setting variable.

    *

    1. Project Browser-> right click on Project name, select Properties.

    2, In the dialog that opens, select Setting on the left tab list

    3. In the first (blank) Name slot, enter the variable (Location?)

    4. Choose the typeof the variable you need from the dropdown list

        (if a screen location, the type would be System.Drawing.Point)

    5. Enter a default value for the variable (say) 100,100

    6. close the dialog (say yes if asked to save)

    *

    In your code. for an example: this will keep the form window location and restore it on restarting application.

    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            My.Settings.Location = Location
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Location = My.Settings.Location
        End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    Friday, October 6, 2017 8:26 PM
  • Aren't user settings specific to the user who created them?  So if the user logs off and another user logs on, they would also have to configure the location?

    I don't know who you're talking to, but that now makes no sense.

    If you want the "settings" per user, that's built in and works fine. If you want to save something that's "per computer" instead of per user, that's on you to do.

    Additional Information:

    https://docs.microsoft.com/en-us/dotnet/visual-basic/developing-apps/programming/app-settings/accessing-application-settings


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Friday, October 6, 2017 8:32 PM ... added link to MSDN documentation
    Friday, October 6, 2017 8:29 PM
  • Frank,

    Sorry, I clicked reply under your response, I thought MSDN used to nest responses.  I don't understand how it makes no sense.

    I need to store an application scoped setting so I am choosing application scope.  The location setting is not per user, it should be set once at the time of application install or first run and never be requested again regardless of new users logging in.

    Friday, October 6, 2017 8:35 PM
  • Frank,

    Sorry, I clicked reply under your response, I thought MSDN used to nest responses.  I don't understand how it makes no sense.

    I need to store an application scoped setting so I am choosing application scope.  The location setting is not per user, it should be set once at the time of application install or first run and never be requested again regardless of new users logging in.

    I don't view this forum nested -- I see messages serially, one after the other, so in the absence of you using my name or quoting, I'm guessing. ;-)

    *****

    It makes no sense because of what's showng here:

    https://docs.microsoft.com/en-us/dotnet/visual-basic/developing-apps/programming/app-settings/how-to-change-user-settings

    "Although you can change and save the values of user-scope settings at run time, application-scope settings are read-only and cannot be changed programmatically."


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, October 6, 2017 8:39 PM
  • @Everyone,

    I tested this scenario 8 years ago.  User scoped settings are only stored with current user logged on to the PC.  If a new user logs on, their settings are reset to default.

    In my scenario, the location setting is initially blank.  So using user settings won't work.

    Friday, October 6, 2017 8:39 PM
  • @Everyone,

    I tested this scenario 8 years ago.  User scoped settings are only stored with current user logged on to the PC.  If a new user logs on, their settings are reset to default.

    In my scenario, the location setting is initially blank.  So using user settings won't work.

    I quit using the built-in stuff years ago but for other reasons: With my own, I can persist my own type(s) and because mine is binary, it's a lot smaller than serialization to XML.

    I can also add in compression and encryption easily so it's hard to beat (but there are drawbacks too).

    Ultimately, you know how they work so it's up to you to maintain that - however you choose to.

    ;-)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, October 6, 2017 8:43 PM
  • Frank,

    Thanks for helping.  Looks like I'll be using a custom file to store the settings.  StreamWriter, here I come!!!

    :-(

    Friday, October 6, 2017 8:46 PM
  • StreamWriter, here I come!!!

    If it's not really unusual, have you considered XML?

    Not serializing to XML, but write your own using LINQ-To-XML for example? If you use XAttributes rather than XElements, it's fairly compact (for XML).


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, October 6, 2017 8:50 PM
  • In my scenario, the location setting is initially blank.  So using user settings won't work.

    That's what user settings are: a set of values that are suitable for that particular user.

    The problem with application settings is that if one user makes a change, all subsequent users see that change.  That is undesirable.  Application settings should be adjusted only be the administrator.

    If you are concerned about the settings that a user sees at first log on, then you need to provide suitable default values.

    Friday, October 6, 2017 9:20 PM