Question xml or ini to save settings

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
Hi there,

I've been trying to create an app with vb 2010.
People are able to change settings (through the apps settings screen), then the changes are being saved to a file (the settings are being used in the whole app).

Now my question is, what is the best way to save the setting in vb 2010:

XML or ini?

Does anyone has a good sample for it?

Thanks
 
In VS there's a Settings page in project properties, this is the best place for application settings, they are also loaded and saved automatically. When you add a setting set it as scope User for settings users can change, scope Application for readonly settings common for all users. You can bind a setting to a control, for example to Text property of a TextBox and it will automatically get/set the value to settings cache. It is also possible to create a setting directly from a control binding, ie in Properties window select (ApplicationSettings) > (PropertyBinding) and for example select a new setting for Text property (just give the setting a name). No code is necessary for any of this, but if you for some reason need to access a setting in code use the My.Settings object, each setting has a strongly typed property. For this reason you can also use a PropertyGrid control in a form to display all setting by assigning My.Settings object to the grids SelectedObject property, the view is exactly the same like when you edit properties of a control in VS, all user settings can be edited by user directly. This may be sufficient for some, and really convenient to develop, but often one have a more extensive UI setting layout (and perhaps settings that is not displayed), so binding each setting to a specific UI control is probably more common. By the way, the underlying storage for these settings is a xml file, but that is really transparent for all usage.
MSDN/help has many articles about this if you need additional info, though if you look around and try the things I mentioned this is really intuitive and easy to set up.
 
Thanks JohnH for your reply,

I have thought of myself, but I want to be able to edit the settings file manually.

I have tried to create a file (app.config), which is xml based, but when I try to add several things I get the message that they are not declared, why is this?

Thanks
 
but I want to be able to edit the settings file manually.
In what context?
I have tried to create a file (app.config), which is xml based, but when I try to add several things I get the message that they are not declared, why is this?
Can you elaborate this?

Btw, if you're going outside the Configuration schema you're better off managing this data in your own file.
 
In what context?

For example.
In the file the fonts, fontsize, color, style, background, backcolor, etc. is being set. So I want to be able to edit these settings (if necessary) manually through notepad.


Can you elaborate this?

HTML:
<?xml version="1.0" encoding="utf-8" ?>
<settings>
  <General>    
    <usebgimage>0</usebgimage>
    <usebgcolor>1</usebgcolor>
    <bgimage>(link to image)</bgimage>
    <bgcolor>#FFFFFF</bgcolor>
  </General>
  <Beamer_Font>
    <font>Arial</font>
    <size>40</size>
    <style>Bold</style>
    <align>Left</align>
  </Beamer_Font>
</settings>

<settings> is underlined and says: The settings element is not declared.
 
is underlined and says: The settings element is not declared.
I don't see how that is related to the Configuration schema. For a user setting of type Font the projects app.config file will contain this kind of data:
HTML:
<userSettings>
    <WindowsApplication1.My.MySettings>
      <setting name="SomeFontSetting" serializeAs="String">
        <value>Verdana, 9.75pt, style=Bold</value>
      </setting>
    </WindowsApplication1.My.MySettings>
</userSettings>
You can also edit this file 'manually' in VS and the settings dialog page will update automatically. (projects app.config and the internal Settings.settings xml files both synchronize either way)

If the need is adding new settings at runtime (not defined at design time) it is possible to use the appSettings section, though I'd say that would be unusual and perhaps would be better done using any other data file, which you can handle using .Net Xml tools.
 
Back
Top