0

I have a couple of questions regarding the use of the Settings in VS projects. First my understanding from reading various resources on the web is that there are two types of settings. User settings which are read/write, and application settings which are read only. My first question is what makes application settings useful if they are read only and can't ever be changed at runtime? What would be the difference between using an Application scope setting and just hard coding the setting into your source code? Also is a User setting in a VS project tied to the specific windows user account on the computer? If my program is installed on a computer with User A and User B, if User A changes a setting will that new setting be used when the program is run under User B? If this is not the case how would one implement user independent settings?

Thanks

Alexander Van Atta
  • 870
  • 11
  • 34
  • Consider something like a SQL connection string or a path to a file share. The same program could be used in more than one environment with only changing the app settings. – paparazzo Dec 03 '12 at 16:46
  • How is this possible if application scoped settings are read only? How would the user change the SQL connection string to point to a new server? Here is a quote from MS API "The crucial distinction between application-scope and user-scope settings is that user-scope settings are read/write at run time, and their values can be changed and saved in code. Application-scope settings are read only at run time. While they can be read, they cannot be written to. Settings with application scope can only be changed at design time, or by altering the settings file manually." – Alexander Van Atta Dec 03 '12 at 16:54
  • @vanattab from MSDN (emphasis is mine): "... **In most cases**, the application-scoped settings are read-only; because they are program information, you will **typically not need** to overwrite them..." – Adriano Repetti Dec 03 '12 at 16:59
  • What part of my comment implied the end user was changing the app settings? "Used in more than one environment" clearly implies an admin type configuration. – paparazzo Dec 03 '12 at 16:59
  • @Blam "The same program could be used in more than one environment with only changing the app settings." How would to point to a new server – Alexander Van Atta Dec 03 '12 at 17:02
  • @Adriano I am reading more about the Application settings now through that like you sent me. However I am still confused I must be missing some major idea. – Alexander Van Atta Dec 03 '12 at 17:04
  • So you have your end users enter configuration stuff like SQL setting and file paths to run a program? You never have like and IT person deploy and configure? – paparazzo Dec 03 '12 at 17:05
  • Down vote, For??? @Blam My program .net program uses the octave.exe (the MatLab Clone) to run some .m scripts and get the results. When the program is installed I want the person installing to browse to the octave.exe file and then save that path in an Application setting. How would you do this. Would you have the IT person edit the XML settings by hand and add the path after installation? – Alexander Van Atta Dec 03 '12 at 17:12
  • 1
    @vanattab take a look to this post here on SO: http://stackoverflow.com/questions/453161/best-practice-to-save-application-settings-in-a-windows-forms-application – Adriano Repetti Dec 03 '12 at 17:59

1 Answers1

1

Application settings provide a single place to store values that might be used throughout the program.

Consider that if you have a number of message strings that are used to pass messages from one part of the program to the other, it would not be a good idea to replicate those strings in text form all over the program. Instead store them in an Application setting and use that. If ever in the future you need to change it, you can change it in one place, and you don't have to worry about spelling mistakes so long as you are using Intellisense and everything compiles correctly.

User settings are user dependent. I generally write my user settings to the registry however, as I do not want my users to get in there and mess with them easily.

Regardless, Usersettings are saved in the following location: C:\Documents and Settings\%username%\Local Settings\Application Data\%ApplicationName%

CodeWarrior
  • 7,388
  • 7
  • 51
  • 78
  • 1
    Well maybe the example for Application settings is not really exciting, it sounds you'd better use const string in a static class (or resources if they're messages for the user)... – Adriano Repetti Dec 03 '12 at 16:40
  • But whats the difference between using and application settings and storing the setting in a globally accessible variable in the source code? (i.e. I could have a application setting for an error msg or I could have a public settings module with a Public Const msg As String = "blah blah blah" – Alexander Van Atta Dec 03 '12 at 16:42
  • Settings are stored to an XML file, so they persist between each time the application is run. If you don't need to persist the data, yes, just use a variable to store the data in memory. Although, please, reconsider using a global :) – Steven Doggart Dec 03 '12 at 16:45
  • @vanattab if they're not inside source code then they can't be changed. Imagine the connection string to a database engine, installer or power users may have to change it so they can't be hard-coded. – Adriano Repetti Dec 03 '12 at 16:47
  • @Adriano I get that, and in fact that's part of want I want to do. (Want to user to input the path to a exe during the installation process.) However my understanding is that (Application scoped) settings can't be changed at runtime so how would one use that to store a exe path or server connection screen in the installer? – Alexander Van Atta Dec 03 '12 at 16:51
  • @vanattab no, they're not read-only. _Usually_ they are because they're stored in the same path of the executable but within the installer you can safely assume you can open them in r/w to update that values. – Adriano Repetti Dec 03 '12 at 16:55
  • so in order to change an application setting I would need to edit the XML setting file manually? – Alexander Van Atta Dec 03 '12 at 16:56
  • @vanattab no, you can do it programmatically. Start reading from here: http://msdn.microsoft.com/en-us/library/k4s6c3a0.aspx – Adriano Repetti Dec 03 '12 at 16:57