This version uses static methods and ISO storage for your assembly settings.
Potentially more usefull and don't need to worry about file path locations
or user stepping on the file. Scope is assembly and user specific so
different user's can have different settings. New assembly versions will
also have their own settings and not conflict with each other (i.e. user
running parallel versions.)
Usage example:
-------------------------
MyAssemConfig mac = MyAssemConfig.G etAssemConfig() ;
mac.X = 20;
mac.Y = 40;
MyAssemConfig.S etAppConfig(mac );
MyAssemConfig mac2 = MyAssemConfig.G etAssemConfig() ;
Console.WriteLi ne("Name:"+mac2 .Name);
Console.WriteLi ne("X:"+mac2.X) ;
Console.WriteLi ne("Y:"+mac2.Y) ;
The Class:
---------------------
using System;
using System.Xml.Seri alization;
using System.IO;
using System.IO.Isola tedStorage;
using System.Reflecti on;
namespace MyNamespace
{
/// <summary>
/// Summary description for MyAppSettings.
/// </summary>
public sealed class MyAssemConfig
{
public string Name;
public int X;
public int Y;
// ... Add others...
//private static readonly string file;
private static readonly string isoFileName;
static MyAssemConfig()
{
//file = Assembly.GetExe cutingAssembly( ).Location + ".xml";
isoFileName = "AssemConfig.xm l";
}
public MyAssemConfig()
{
Name = Assembly.GetExe cutingAssembly( ).GetName().Nam e;
}
public static MyAssemConfig Reset()
{
MyAssemConfig mac = new MyAssemConfig() ;
MyAssemConfig.S etAppConfig(mac );
return mac;
}
public static MyAssemConfig GetAssemConfig( )
{
IsolatedStorage File isoStore =
IsolatedStorage File.GetStore(I solatedStorageS cope.Assembly |
IsolatedStorage Scope.User, null, null);
if ( ! MyAssemConfig.I SOFileExists(is oStore, isoFileName) )
return new MyAssemConfig() ;
string xml = MyAssemConfig.R eadFromISOFile( isoStore, isoFileName);
try
{
MyAssemConfig mac = MyAssemConfig.F romXmlString(xm l);
return mac;
}
catch
{
// Xml not valid - probably corrupted. Rewrite it with defaults.
return MyAssemConfig.R eset();
}
}
public static void SetAppConfig(My AssemConfig appConfig)
{
if ( appConfig == null )
throw new ArgumentNullExc eption("appConf ig");
string xml = appConfig.ToXml String();
IsolatedStorage File isoStore =
IsolatedStorage File.GetStore(I solatedStorageS cope.Assembly |
IsolatedStorage Scope.User, null, null);
MyAssemConfig.W riteToISOFile(i soStore, isoFileName, xml);
}
// public static MyAppConfig Load()
// {
// using(StreamRea der sr = new StreamReader(My AppConfig.file) )
// {
// string xml = sr.ReadToEnd();
// MyAppConfig mac = MyAppConfig.Fro mXmlString(xml) ;
// return mac;
// }
// }
// public void Save()
// {
// string myXml = this.ToXmlStrin g();
// using(StreamWri ter sw = new StreamWriter(My AppConfig.file) )
// {
// sw.Write(myXml) ;
// }
// }
public string ToXmlString()
{
string data = null;
XmlSerializer ser = new XmlSerializer(t ypeof(MyAssemCo nfig));
using(StringWri ter sw = new StringWriter())
{
ser.Serialize(s w, this);
sw.Flush();
data = sw.ToString();
return data;
}
}
public static MyAssemConfig FromXmlString(s tring xmlString)
{
if ( xmlString == null )
throw new ArgumentNullExc eption("xmlStri ng");
MyAssemConfig mac = null;
XmlSerializer ser = new XmlSerializer(t ypeof(MyAssemCo nfig));
using (StringReader sr = new StringReader(xm lString))
{
mac = (MyAssemConfig) ser.Deserialize (sr);
}
return mac;
}
private static bool ISOFileExists(I solatedStorageF ile isoStore, string
fileName)
{
if ( isoStore == null )
throw new ArgumentNullExc eption("isoStor e");
if ( fileName == null || fileName.Length == 0 )
return false;
string[] names = isoStore.GetFil eNames("*");
foreach(string name in names)
{
if ( string.Compare( name, fileName, true) == 0 )
return true;
}
return false;
}
private static void WriteToISOFile( IsolatedStorage File isoStore, string
fileName, string data)
{
// Assign the writer to the store and the file TestStore.
using(StreamWri ter writer = new StreamWriter(ne w
IsolatedStorage FileStream(file Name, FileMode.Create , isoStore)))
{
// Have the writer write "Hello Isolated Storage" to the store.
writer.Write(da ta);
}
}
private static string ReadFromISOFile (IsolatedStorag eFile isoStore, string
fileName)
{
string sb = null;
// This code opens the TestStore.txt file and reads the string.
using(StreamRea der reader = new StreamReader(ne w
IsolatedStorage FileStream(file Name, FileMode.Open, isoStore)))
{
sb = reader.ReadToEn d();
}
return sb.ToString();
}
}
}
--
William Stacey, MVP
http://mvp.support.microsoft.com
"William Stacey [MVP]" <st***********@ mvps.org> wrote in message
news:#j******** ******@TK2MSFTN GP15.phx.gbl...
Just create your own with strong types as you need. The following could
also me modified with encryption and/or ISO storage, etc.
MyAppConfig mac = new MyAppConfig();
mac.X = 20;
mac.Y = 40;
mac.Save();
MyAppConfig mac2 = MyAppConfig.Loa d();
Console.WriteLi ne("Name:"+mac2 .Name);
Console.WriteLi ne("X:"+mac2.X) ;
Console.WriteLi ne("Y:"+mac2.Y) ;
using System;
using System.Xml.Seri alization;
using System.IO;
using System.Reflecti on;
namespace MyNamespace
{
/// <summary>
/// Summary description for MyAppSettings.
/// </summary>
public sealed class MyAppConfig
{
public string Name;
public int X;
public int Y;
// ... Add others...
private static readonly string file;
static MyAppConfig()
{
file = Assembly.GetExe cutingAssembly( ).Location + ".xml";
}
public MyAppConfig()
{
Name = Assembly.GetExe cutingAssembly( ).GetName().Nam e;
}
public static MyAppConfig Load()
{
using(StreamRea der sr = new StreamReader(My AppConfig.file) )
{
string xml = sr.ReadToEnd();
MyAppConfig mac = MyAppConfig.Fro mXmlString(xml) ;
return mac;
}
}
public void Save()
{
string myXml = this.ToXmlStrin g();
using(StreamWri ter sw = new StreamWriter(My AppConfig.file) )
{
sw.Write(myXml) ;
}
}
public string ToXmlString()
{
string data = null;
XmlSerializer ser = new XmlSerializer(t ypeof(MyAppConf ig));
using(StringWri ter sw = new StringWriter())
{
ser.Serialize(s w, this);
sw.Flush();
data = sw.ToString();
return data;
}
}
public static MyAppConfig FromXmlString(s tring xmlString)
{
if ( xmlString == null )
throw new ArgumentNullExc eption("xmlStri ng");
MyAppConfig mac = null;
XmlSerializer ser = new XmlSerializer(t ypeof(MyAppConf ig));
using (StringReader sr = new StringReader(xm lString))
{
mac = (MyAppConfig)se r.Deserialize(s r);
}
return mac;
}
}
}
--
William Stacey, MVP
http://mvp.support.microsoft.com
"ALI-R" <ne****@microso ft.com> wrote in message
news:eG******** ******@TK2MSFTN GP14.phx.gbl... Hi,,
I have two questions :
1) Is it mandatory that config file of a desktop application must be
App.config
2) Is it possible to update config file in your code??
thanks for your help.
ALI