Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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!

Posted
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:

 

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:

SettingsManager settings = new SettingsManager();
settings.Name = "My Name";
settings.Save();

Exit the application and then restart. Check the settings value with:

SettingsManager settings = new SettingsManager();
MessageBox.Show(settings.Name);

.NET handles all the saving/reading for you.

Posted
If you enter a name for the setting in the first column' date=' 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.[/quote']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:

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...

Posted

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:

[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?

Posted

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:

 

[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:

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;
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...