Long time no see :) Today I’ll give You a simple example on how to hide Your application configuration file using IsolatedStorage and a little help of XmlSerializer.

Sometimes You have to write some configuration file with common setup of Your application, but You don’t want somebody to find and edit it outside the application. .NET Framework comes with help named IsolatedStorage. When using it, application stores data in unique place, which depends on user, assembly, domain etc.(parameters can be mixed). This place is transparent even for developers, .NET Framework manages it all. Using it is very simple and helps to prevent users to make changes in Your files on their own.

Below You have a simple example of using IsolatedStorage and XmlSerializer, which I use to serialize one of my classes to xml.

Serialized class:

[Serializable]
public class DBConnectionData
{
public string Server { get; set; }
public string DataBase { get; set; }
public string User { get; set; }
public string Password { get; set; }
public DBConnectionData()
{ }
public DBConnectionData(string server, string dataBase, string user, string password)
{
Server = server;
DataBase = dataBase;
User = user;
Password = password;
}
}

Isolated Storage Example:


static class ConfiguationManager
 {

private static string configName = "Configuration.xml";

public static void SaveConfiguration(DBConnectionData data)
 {

XmlSerializer s = new XmlSerializer(data.GetType());

using (StringWriter sw = new StringWriter())
 {
 s.Serialize(sw, data);

using (IsolatedStorageFile isf = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))
 {

using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(configName, FileMode.Create, FileAccess.Write, isf))
 {

using (StreamWriter sww = new StreamWriter(isfs, Encoding.Default))
 {

sww.Write(sw.ToString());

}
 }
 }
 }
 }

public static DBConnectionData GetConfiguration()
 {

using (IsolatedStorageFile isf = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))
 {
 string[] files = isf.GetFileNames(configName);

if (files != null && files.Length > 0)
 {

using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(configName, FileMode.Open, FileAccess.Read, isf))
 {

isfs.Position = 0;

StreamReader sr = new StreamReader(isfs, Encoding.Default);

DBConnectionData cData = new DBConnectionData();

XmlSerializer s = new XmlSerializer(cData.GetType());

cData = (DBConnectionData)s.Deserialize(sr);

return cData;

}

}

return null;
 }
 }
 }