Ready class for accessing configuration file

  • Thread starter Thread starter Serge calderara
  • Start date Start date
S

Serge calderara

Dear all,

Does any one have a ready class that is able to access a
configuration file and make different type of querry,
read, write operation?

Why re invanting the real if something already exist and
as we share things in usegroup in general...

thanks for your help
 
Hi,
Does any one have a ready class that is able to access a
configuration file and make different type of querry,
read, write operation?

If you mean a custom user setting file for your application, you can find
plenty of them on the web. Just a few examples (look through the article to
find the section related to the setting file):
http://www.msdnaa.net/Resources/display.aspx?ResID=1929 (very easy to
understand and reuse, using the IsolatedStorage class)
http://www.msdnaa.net/Resources/display.aspx?ResID=1975 (idem but without
using IsolatedStorage)
or http://www.codeproject.com/useritems/configmanager.asp#xx385470xx
also : http://www.codeproject.com/csharp/app_config.asp


You may also want to use the MS application configuration block :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/cmab.asp

bye
mehdi
 
Thnaks for your links

By the way what do u suggest to use ?
- configuration management block
- XML documents function

Thnaks for your answer
 
serge calderara said:
Thnaks for your links

By the way what do u suggest to use ?
- configuration management block
- XML documents function

i've still not implemented the setting management in my application (i'm
gonna do that today actually) but i think that i will go for the solution
exposed in the first link i gave you. It looks easy and clear enough for me
(and the setting file is automaticaly stored in a safe place thanks to the
IsolatedStorage system). I had a quick look at the MS configuration
management block but it looked more heavy to understand and use and it
requires 1.1 which i don't use now.

good luck.
 
Hi,

But this StoreIolate seems a bit different no?
it seems that it collect information of the current user,like
environement settings ...

But where this file is located when it creates it, is it pure memory or
what ? could not foînd it

SOund more complex that andling pure aaplication.config file with
XMLDocuments no?

Or maybe I did not really catch the purpopse of this Isolate stuff, in
my case I need just to create, even manually, a file with different
parameters in that my application will use

thnaks for your commnents
 
calderara serge said:
Hi,
Hi,

But this StoreIolate seems a bit different no?
it seems that it collect information of the current user,like
environement settings ...

But where this file is located when it creates it, is it pure memory or
what ? could not foînd it

i've implemented my configuration management yesterday and it's working well
so far. I used the first link i gave you in my first message. I had to
modify a bit the code to make it actually work. I use IsolatedStorage to get
the path where i can store my setting file and an XML serializer to actually
create the file. It gives me an XML file which contains all my user
settings.

IsolatedStorage is only a way to get a path where you can store your setting
file (or whatever else related to your application). If you use that,
instead of manually indicate a path on the hard drive, you ensure that:
- the user running the application has read/write access to that path, you
don't have to bother of which operating system he is running and/or which
access rights he has
- the path given to you by IsolatedStorage is unique to your assembly and/or
domain, which means that even if an other application installed on the same
machine uses the same name than yours for its setting file, you won't
overwrite its setting file by mistake.

For example, for my application, the setting file is stored in :
C:\Documents and Settings\USER\Local Settings\Application
Data\IsolatedStorage\biif0gen.lgz\4iywbas3.5tz\Url.etsikv0grd1xsubxhp2n5bfdz
aioaiuu\Url.etsikv0grd1xsubxhp2n5bfdzaioaiuu\Files
SOund more complex that andling pure aaplication.config file with
XMLDocuments no?

this is actually pure XML application config file. And simply generated by
an XML serializer.

Here is how i do it (it is a modified version of the code found in the link
i gave you, in c#, sorry if you are using VB) :
- first you need to create a small class which will contain all your user
settings. This class is going to be serialized into the XML file :
//class containing all the settings and serialized into a file

[Serializable()]

public class ConfigurationData

{

//all user settings, you may want to make those fields private and use
public properties to get/set their values

public string m_userName = null;

public string m_pet = null;

//constructors

public ConfigurationData() {}

public ConfigurationData(string userName, string pet)

{

m_userName = userName;

m_pet = pet;

}

}



- and here is how you can store your settings into an XML file:
private static void saveSettings(string userName, string pet)

{

//name of the setting file

string settingFileName = "setting.xml";

//the actual user settings

ConfigurationData settings = new ConfigurationData(userName, pet);

try

{

//folder containing the setting file, unique for that application

IsolatedStorageFile isolatedStore =
IsolatedStorageFile.GetUserStoreForDomain();

XmlSerializer serializer = new XmlSerializer(typeof(ConfigurationData));

IsolatedStorageFileStream stream = new
IsolatedStorageFileStream(settingFileName ,

System.IO.FileMode.Create,

System.IO.FileAccess.ReadWrite,

isolatedStore);

//we write the file

serializer.Serialize(stream, settings);

stream.Close();

}

catch (Exception error)

{

MessageBox.Show("Error while saving settings : " + error.Message);

}

}


- and now, here is how you can read that setting file

private static ConfiguarationData loadSettings()

{

//name of the setting file

string settingFileName = "setting.xml";

//user settings

ConfigurationData settings = null;

//folder containing the setting file

IsolatedStorageFile isolatedStore;


isolatedStore = IsolatedStorageFile.GetUserStoreForDomain();

//we check if a setting file is already there

if (isolatedStore.GetFileNames(m_settingFileName).Length > 0)

{

try

{

//we retrieve the values from this setting file

XmlSerializer serializer = new XmlSerializer(typeof(ConfigurationData));

IsolatedStorageFileStream stream = new IsolatedStorageFileStream

(settingFileName,

System.IO.FileMode.Open,

System.IO.FileAccess.Read,

isolatedStore);

settings = (ConfigurationData)(serializer.Deserialize(stream));

stream.Close();

}

catch (Exception error)

{

MessageBox.Show("Error while reading setting file : " + error.Message);

//let's create a default set of user settings

settings = new ConfigurationData("default name", "default pet");

}

}

else

{

//no setting file is present, create a default set of user settings

settings = new ConfigurationData("default name", "default pet");

}

return settings;

}
 
Thnaks for your example.

Sounds to be a bit different that a normal application configuration
file because as far as I understand you generate the configuration file
manually by specifying your own section and settings for your
application and then read or write into that file for corresponding
value.

In your sample you define trough code your all configuration needs and
then create the configuration file base on that right?

What SERIALIZE keyword means at the begining of your class?

Does you final xmlk file will be with the form App.exe.config ? this is
the name of the file I reada it has to be

regards
serge
 
Back
Top