tags:

views:

207

answers:

1

Hi,

I never used Settings class before but I found some articles on CodeProject which I'm currently reading (for example http://www.codeproject.com/KB/cs/PropertiesSettings.aspx) but so far I didn't see how to save string array for getting it after application is started next time.

For example my application has several FileSystemWatcher instances, with each instance several other directories are connected (for example one FSW instance is monitoring one directory for a change and when it happens it copies some file to several other directories), so I would have one string array with watched paths representing FSW instances, and string array for each of those paths, representing directories that are affected.

My question is, what should I use (Settings class or something else), and how should I use that for storing application configuration that is variable number of string arrays? Emphasize is on something I could use very soon as I don't have too much time to make custom class (but would have to if I cannot find solution) or dig into some obscure hacks. Any tutorial link, code snippet would be very helpful.

Thanks.

+1  A: 

Why not use a config file instead? I had a set of FileSystemWatchers like yours and just added a set of paths using custom config sections. Thought that requires you rolling a class to extending the Configuration classes, but I think that you can't beat that approach.

Though if you want a clear easy hack and don't want to get bothered with the Custom Config Sections/Elements/Collections. Just use a quick and easy AppSettings hack. XD

<appSettings>
    <add key="ConnectionInfo0" value="server=(local);database=Northwind;Integrated Security=SSPI" />
    <add key="ConnectionInfo1" value="server=(local);database=Northwind;Integrated Security=SSPI" />
    <add key="ConnectionInfo2" value="server=(local);database=Northwind;Integrated Security=SSPI" />
</appSettings>

Getting it from code.

const string CONNECTIONSTRING="";
int i = 0;
while(ConfigurationSettings.AppSettings["ConnectionInfo"] + i != null)
{
    // do stuff with connection info here
    // use
    ConfigurationSettings.AppSettings["ConnectionInfo" + i];
    // to get data.
    ++i;
}

Tad ugly, I know. Custom Config works best.

Jonn
Thanks for your answer Jonn. Thing is I need to add data programmatically also and since I simply don't have so much time now to spend on learning how to do this correctly (IMO they over-complicated it), I will use `System.Xml.Serialization.XmlSerializer` for now. It's cleaner and much easier to use/deploy with custom class which represents settings. Cheers.
Maks