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