Saving Project Settings (in C# 2005)

joe_pool_is

Contributor
Joined
Jan 18, 2004
Messages
507
Location
Longview, TX [USA]
I've been trying to find a way to save the settings of projects that clients' create with my C# projects. They can set page orientations, set paths, windows positions, etc. All these values are stored in my running application using a class called ThisProject that encapsulates the settings:

ThisProject objProj = new ThisProject();

What are good, accepted methods of saving information like this?

* I have found information on Serializing, but this seems to be set up for writing text files.

* I noticed the Project > Properties has a Settings tab, but I can't find much info on what this is meant to save, where it is saved, how to write to it or retrieve it during code.

I'm trying to familiarize myself with C# during my time off (until Jan 3rd). I've used only VB.NET (2002) and C++ in the past, so learning C# seems like a great idea!
 
If you enter a name for the setting in the first column, data type in the second and scope in the third then it will generate a settings object for you that can be accessed under the Properties namespace of your application.
 
joe_pool_is said:
I've been trying to find a way to save the settings of projects that clients' create with my C# projects. They can set page orientations, set paths, windows positions, etc. All these values are stored in my running application using a class called ThisProject that encapsulates the settings:

ThisProject objProj = new ThisProject();

What are good, accepted methods of saving information like this?

* I have found information on Serializing, but this seems to be set up for writing text files.

* I noticed the Project > Properties has a Settings tab, but I can't find much info on what this is meant to save, where it is saved, how to write to it or retrieve it during code.

I'm trying to familiarize myself with C# during my time off (until Jan 3rd). I've used only VB.NET (2002) and C++ in the past, so learning C# seems like a great idea!

For a more extensible approach, consider the following:

1. Create a class that inherits from System.Configuration.ApplicationSettingsBase

2. Create a property in your class

3. Assign the property with either the System.Configuration.UserScopedSettingAttribute or System.Configuration.ApplicationScopedSettingAttribute.

Example:

Code:
class SettingsManager :   ApplicationSettingsBase  
    {
        private Hashtable ht = new Hashtable();
        
        [UserScopedSettingAttribute()]
        public string Name
        {
            get { return (string)this["Name"]; }
            set { this["Name"] = value; }
        }
    }
Create an instance and set the property's value:
Code:
SettingsManager settings = new SettingsManager();
settings.Name = "My Name";
settings.Save();
Exit the application and then restart. Check the settings value with:
Code:
SettingsManager settings = new SettingsManager();
MessageBox.Show(settings.Name);
.NET handles all the saving/reading for you.
 
PlausiblyDamp said:
If you enter a name for the setting in the first column, data type in the second and scope in the third then it will generate a settings object for you that can be accessed under the Properties namespace of your application.
Ok, I entered/updated the values listed under the Project > Properties... Settings tab for 16 different values.

Over on the right in the Properties viewer, I tried to enter descriptions of my 16 different values in the "Description" box, but every time I try this I get this error:
Code:
An error occurred while saving values to the app.config file. The file might be corrupted or contain invalid XML.
Should I delete the existing app.config file? Will the program generate a new one? Why does it keep erroring like this on me? I have tried closing the program and restarting (a well documented trick), but this didn't fix anything.

I am about to give Mister E's solution a go now...
 
Mister E,

For a simple lesson in learning C# features, I decided to create a simple calendar making program. It has 16 textboxes which determine the paths to photos for 12 months, plus 2 previous months and 2 months for the next year. When I save someone's project, I want to be able to save these paths as well.

When I created my calendar class, I gave it a private string[16] path declaration along with a couple of public members to get/set values: Index and Path. To save the settings, I first have to set the Index, then set the Path:
Visual Basic:
[System.Configuration.UserScopedSetting()]
public string Path {
  get { return m_path[m_index]; }
  set { s_path[m_index] = value; }
}
Is there a way to save all of my 16 strings, or will this technique only save the last string that I accessed?
 
Well, when you inherit from ApplicationSettingsBase you can see that the base class supplies an indexer. You are essentially dealing with a hash table.

So, your property might look like:

Code:
[UserScopedSettingAttribute()]
public string[] Paths
{
    get
    {
        if (this["Paths"] == null)
            this["Paths"] = new string[16];

        return (string[])this["Paths"];
    }
    set
    {

        this["Paths"] = value;
    }
}
You could have a function to set the values of the path array:
Code:
public void SetPath(int index, string val)
{
    string[] paths;

    if (this["Paths"] == null)
        paths = new string[16];
    else
        paths = (string[])this["Paths"];

    paths[index] = val;

    this["Paths"] = paths;
}
 
Back
Top