473,738 Members | 3,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to write to config file

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
Nov 16 '05 #1
9 8276
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


Nov 16 '05 #2
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


Nov 16 '05 #3
"ALI-R" wrote:
1) Is it mandatory that config file of a desktop application must be
App.config
This is a Visual Studio-ism. This is not what the configuration file ends
up getting called, this is just what it's called inside a Visual Studio .NET
project.

If you look in the build output directory (the bin\Release or bin\Debug
directory for C# projects, the bin directory for VB.NET projects) you'll see
that there is a file called MyApp.exe.confi g, where MyApp.exe is whatever
you've chosen to call your application executable.

This Whatever.exe.co nfig file is a copy of the App.Config file. VS.NET
copies it so that you don't need to maintain a seperate debug and release
version of this file. And they use the name App.config because that's the
magic name they've chosen for this behaviour. :-)
2) Is it possible to update config file in your code??


Sometimes yes, but not always, so you shouldn't rely on being able to do
this.

Why might you not be able to? Well remember that the config file ends up in
the same directory as your executable. If it's installed as a normal
Windows app, this will be something like c:\Program
Files\Yourcompa ny\Yourapp.

Normal users will not be able to write to this directory. (At least not if
it's on an NTFS volume.) Windows protects the Program Files directory so
that only Administrators and Power Users are allowed to write into it.

So if you are relying on being able to write to your config file, you just
made it impossible for anyone not running as an admin or power user to run
your application!

Also, if you were thinking of putting user settings in there, you also just
made life difficult for anyone who has multiple users using the machine.

And if you think multi-user machines are unusual, they're not *that*
unusual. It doesn't have to be multiple concurrent users... If different
logins exist, storing user settings in the app config file doesn't work
well.

You should look at the Environment.Get Folder API to see where you should be
putting settings.
--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/
Nov 16 '05 #4
Hi,
Thanks for your complete answer.

About my first question ,,you mean in deployment I should change its name
from app.config to myapp.exe.confi g?? right??
"Ian Griffiths [C# MVP]" <ia************ *@nospam.nospam > wrote in message
news:Os******** ******@TK2MSFTN GP15.phx.gbl...
"ALI-R" wrote:
1) Is it mandatory that config file of a desktop application must be
App.config
This is a Visual Studio-ism. This is not what the configuration file ends
up getting called, this is just what it's called inside a Visual Studio

..NET project.

If you look in the build output directory (the bin\Release or bin\Debug
directory for C# projects, the bin directory for VB.NET projects) you'll see that there is a file called MyApp.exe.confi g, where MyApp.exe is whatever
you've chosen to call your application executable.

This Whatever.exe.co nfig file is a copy of the App.Config file. VS.NET
copies it so that you don't need to maintain a seperate debug and release
version of this file. And they use the name App.config because that's the
magic name they've chosen for this behaviour. :-)
2) Is it possible to update config file in your code??
Sometimes yes, but not always, so you shouldn't rely on being able to do
this.

Why might you not be able to? Well remember that the config file ends up

in the same directory as your executable. If it's installed as a normal
Windows app, this will be something like c:\Program
Files\Yourcompa ny\Yourapp.

Normal users will not be able to write to this directory. (At least not if it's on an NTFS volume.) Windows protects the Program Files directory so
that only Administrators and Power Users are allowed to write into it.

So if you are relying on being able to write to your config file, you just
made it impossible for anyone not running as an admin or power user to run
your application!

Also, if you were thinking of putting user settings in there, you also just made life difficult for anyone who has multiple users using the machine.

And if you think multi-user machines are unusual, they're not *that*
unusual. It doesn't have to be multiple concurrent users... If different
logins exist, storing user settings in the app config file doesn't work
well.

You should look at the Environment.Get Folder API to see where you should be putting settings.
--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

Nov 16 '05 #5
thanks ,your answer is so complete and self-explanatory,
"William Stacey [MVP]" <st***********@ mvps.org> wrote in message
news:eS******** ******@TK2MSFTN GP15.phx.gbl...
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

Nov 16 '05 #6
Yes, when deployed you need to make sure that the App.config file is renamed
myapp.exe.confi g. I thought (but could be wrong) that a VS.NET Setup and
Deployment project would do this automatically. But if you're doing your
own installer you'll need to do this.

Of course, the simple thing is just to install what VS.NET builds into the
bin\release directory - it will already have copied and renamed the file for
you. And you usually need to install everything that's in that directory...
--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

"ALI-R" wrote:
Hi,
Thanks for your complete answer.

About my first question ,,you mean in deployment I should change its name
from app.config to myapp.exe.confi g?? right??
"Ian Griffiths [C# MVP]" <ia************ *@nospam.nospam > wrote in message
news:Os******** ******@TK2MSFTN GP15.phx.gbl...
"ALI-R" wrote:
> 1) Is it mandatory that config file of a desktop application must be
> App.config


This is a Visual Studio-ism. This is not what the configuration file
ends
up getting called, this is just what it's called inside a Visual Studio

.NET
project.

If you look in the build output directory (the bin\Release or bin\Debug
directory for C# projects, the bin directory for VB.NET projects) you'll

see
that there is a file called MyApp.exe.confi g, where MyApp.exe is whatever
you've chosen to call your application executable.

This Whatever.exe.co nfig file is a copy of the App.Config file. VS.NET
copies it so that you don't need to maintain a seperate debug and release
version of this file. And they use the name App.config because that's
the
magic name they've chosen for this behaviour. :-)
> 2) Is it possible to update config file in your code??


Sometimes yes, but not always, so you shouldn't rely on being able to do
this.

Why might you not be able to? Well remember that the config file ends up

in
the same directory as your executable. If it's installed as a normal
Windows app, this will be something like c:\Program
Files\Yourcompa ny\Yourapp.

Normal users will not be able to write to this directory. (At least not

if
it's on an NTFS volume.) Windows protects the Program Files directory so
that only Administrators and Power Users are allowed to write into it.

So if you are relying on being able to write to your config file, you
just
made it impossible for anyone not running as an admin or power user to
run
your application!

Also, if you were thinking of putting user settings in there, you also

just
made life difficult for anyone who has multiple users using the machine.

And if you think multi-user machines are unusual, they're not *that*
unusual. It doesn't have to be multiple concurrent users... If
different
logins exist, storing user settings in the app config file doesn't work
well.

You should look at the Environment.Get Folder API to see where you should

be
putting settings.

Nov 16 '05 #7
Yes, when deployed you need to make sure that the App.config file is renamed
myapp.exe.confi g. I thought (but could be wrong) that a VS.NET Setup and
Deployment project would do this automatically. But if you're doing your
own installer you'll need to do this.

Of course, the simple thing is just to install what VS.NET builds into the
bin\release directory - it will already have copied and renamed the file for
you. And you usually need to install everything that's in that directory...
--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

"ALI-R" wrote:
Hi,
Thanks for your complete answer.

About my first question ,,you mean in deployment I should change its name
from app.config to myapp.exe.confi g?? right??
"Ian Griffiths [C# MVP]" <ia************ *@nospam.nospam > wrote in message
news:Os******** ******@TK2MSFTN GP15.phx.gbl...
"ALI-R" wrote:
> 1) Is it mandatory that config file of a desktop application must be
> App.config


This is a Visual Studio-ism. This is not what the configuration file
ends
up getting called, this is just what it's called inside a Visual Studio

.NET
project.

If you look in the build output directory (the bin\Release or bin\Debug
directory for C# projects, the bin directory for VB.NET projects) you'll

see
that there is a file called MyApp.exe.confi g, where MyApp.exe is whatever
you've chosen to call your application executable.

This Whatever.exe.co nfig file is a copy of the App.Config file. VS.NET
copies it so that you don't need to maintain a seperate debug and release
version of this file. And they use the name App.config because that's
the
magic name they've chosen for this behaviour. :-)
> 2) Is it possible to update config file in your code??


Sometimes yes, but not always, so you shouldn't rely on being able to do
this.

Why might you not be able to? Well remember that the config file ends up

in
the same directory as your executable. If it's installed as a normal
Windows app, this will be something like c:\Program
Files\Yourcompa ny\Yourapp.

Normal users will not be able to write to this directory. (At least not

if
it's on an NTFS volume.) Windows protects the Program Files directory so
that only Administrators and Power Users are allowed to write into it.

So if you are relying on being able to write to your config file, you
just
made it impossible for anyone not running as an admin or power user to
run
your application!

Also, if you were thinking of putting user settings in there, you also

just
made life difficult for anyone who has multiple users using the machine.

And if you think multi-user machines are unusual, they're not *that*
unusual. It doesn't have to be multiple concurrent users... If
different
logins exist, storing user settings in the app config file doesn't work
well.

You should look at the Environment.Get Folder API to see where you should

be
putting settings.

Nov 16 '05 #8
No,it installs the file as it is.I mean it dosn't change the name of the
file.it keeps app.config and that was why mt application couldn't get the
settings from that in runtime.

"Ian Griffiths [C# MVP]" <ia************ *@nospam.nospam > wrote in message
news:Oh******** ******@TK2MSFTN GP10.phx.gbl...
Yes, when deployed you need to make sure that the App.config file is renamed myapp.exe.confi g. I thought (but could be wrong) that a VS.NET Setup and
Deployment project would do this automatically. But if you're doing your
own installer you'll need to do this.

Of course, the simple thing is just to install what VS.NET builds into the
bin\release directory - it will already have copied and renamed the file for you. And you usually need to install everything that's in that directory...

--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

"ALI-R" wrote:
Hi,
Thanks for your complete answer.

About my first question ,,you mean in deployment I should change its name from app.config to myapp.exe.confi g?? right??
"Ian Griffiths [C# MVP]" <ia************ *@nospam.nospam > wrote in message news:Os******** ******@TK2MSFTN GP15.phx.gbl...
"ALI-R" wrote:
> 1) Is it mandatory that config file of a desktop application must be
> App.config

This is a Visual Studio-ism. This is not what the configuration file
ends
up getting called, this is just what it's called inside a Visual Studio

.NET
project.

If you look in the build output directory (the bin\Release or bin\Debug
directory for C# projects, the bin directory for VB.NET projects) you'll
see
that there is a file called MyApp.exe.confi g, where MyApp.exe is
whatever you've chosen to call your application executable.

This Whatever.exe.co nfig file is a copy of the App.Config file. VS.NET
copies it so that you don't need to maintain a seperate debug and release version of this file. And they use the name App.config because that's
the
magic name they've chosen for this behaviour. :-)

> 2) Is it possible to update config file in your code??

Sometimes yes, but not always, so you shouldn't rely on being able to do this.

Why might you not be able to? Well remember that the config file ends up in
the same directory as your executable. If it's installed as a normal
Windows app, this will be something like c:\Program
Files\Yourcompa ny\Yourapp.

Normal users will not be able to write to this directory. (At least
not if
it's on an NTFS volume.) Windows protects the Program Files directory
so that only Administrators and Power Users are allowed to write into it.

So if you are relying on being able to write to your config file, you
just
made it impossible for anyone not running as an admin or power user to
run
your application!

Also, if you were thinking of putting user settings in there, you also

just
made life difficult for anyone who has multiple users using the machine.
And if you think multi-user machines are unusual, they're not *that*
unusual. It doesn't have to be multiple concurrent users... If
different
logins exist, storing user settings in the app config file doesn't work
well.

You should look at the Environment.Get Folder API to see where you

should be
putting settings.


Nov 16 '05 #9
No,it installs the file as it is.I mean it dosn't change the name of the
file.it keeps app.config and that was why mt application couldn't get the
settings from that in runtime.

"Ian Griffiths [C# MVP]" <ia************ *@nospam.nospam > wrote in message
news:Oh******** ******@TK2MSFTN GP10.phx.gbl...
Yes, when deployed you need to make sure that the App.config file is renamed myapp.exe.confi g. I thought (but could be wrong) that a VS.NET Setup and
Deployment project would do this automatically. But if you're doing your
own installer you'll need to do this.

Of course, the simple thing is just to install what VS.NET builds into the
bin\release directory - it will already have copied and renamed the file for you. And you usually need to install everything that's in that directory...

--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

"ALI-R" wrote:
Hi,
Thanks for your complete answer.

About my first question ,,you mean in deployment I should change its name from app.config to myapp.exe.confi g?? right??
"Ian Griffiths [C# MVP]" <ia************ *@nospam.nospam > wrote in message news:Os******** ******@TK2MSFTN GP15.phx.gbl...
"ALI-R" wrote:
> 1) Is it mandatory that config file of a desktop application must be
> App.config

This is a Visual Studio-ism. This is not what the configuration file
ends
up getting called, this is just what it's called inside a Visual Studio

.NET
project.

If you look in the build output directory (the bin\Release or bin\Debug
directory for C# projects, the bin directory for VB.NET projects) you'll
see
that there is a file called MyApp.exe.confi g, where MyApp.exe is
whatever you've chosen to call your application executable.

This Whatever.exe.co nfig file is a copy of the App.Config file. VS.NET
copies it so that you don't need to maintain a seperate debug and release version of this file. And they use the name App.config because that's
the
magic name they've chosen for this behaviour. :-)

> 2) Is it possible to update config file in your code??

Sometimes yes, but not always, so you shouldn't rely on being able to do this.

Why might you not be able to? Well remember that the config file ends up in
the same directory as your executable. If it's installed as a normal
Windows app, this will be something like c:\Program
Files\Yourcompa ny\Yourapp.

Normal users will not be able to write to this directory. (At least
not if
it's on an NTFS volume.) Windows protects the Program Files directory
so that only Administrators and Power Users are allowed to write into it.

So if you are relying on being able to write to your config file, you
just
made it impossible for anyone not running as an admin or power user to
run
your application!

Also, if you were thinking of putting user settings in there, you also

just
made life difficult for anyone who has multiple users using the machine.
And if you think multi-user machines are unusual, they're not *that*
unusual. It doesn't have to be multiple concurrent users... If
different
logins exist, storing user settings in the app config file doesn't work
well.

You should look at the Environment.Get Folder API to see where you

should be
putting settings.


Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3056
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers to have an easier time understanding what I do. Therefore this weekend I'm going to spend 3 days just writing comments. Before I do it, I thought I'd ask other programmers what information they find useful. Below is a typical class I've...
33
3486
by: Nick Evans | last post by:
Hello there, I have been on and off learning to code (with python being the second language I have worked on after a bit of BASIC). What I really want to know is, if you are going to actually write a program or a project of some sort, how do you actually start. Picture this, you know what you want the program to do (its features), you have a possably rough idea of how the program will work. You have opened an empty text file and saved...
0
1390
by: Joan Bos | last post by:
Hi, Is there somewhere on the Internet a description of what a .NET application config file should contain? For our application, I have to write passwords encrypted in a config file. The Enterprise Library does have an encryption method, but that method doesn't just return a value - no - it wants to write the value immediately to a config file. Except, it always gives an error, because no config file contents are ever good enough for...
2
4574
by: Shaun Ram | last post by:
Hi I have this constraint. A help would be greatly apprecitated. I have this Config file. <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="ITASCA"> <section name="ConnectionString" type="System.NamevaluesectionHandler,System" /> </sectionGroup>
4
1668
by: feng | last post by:
..Net's .config file is supposed to replace the .ini files. That's all fine if all I need is to read from it. But what if I need both read and write? With .ini file, I can do that very easily with standard API. But is there API for writing the .config files? I know we can treat .config file as a regular XML file and using XML API to write to it, but is there API for writing to .config file just like we read from it? Thanks.
3
5298
by: Agnes | last post by:
Can I write a file like config.ini which store the server-name, userid & password, So, my application can read the config.ini file and get the server - name, Does my concept is correct ? If yes, do any web site can provide the sample of such file or provide tutorial ?? For my current stage, i got a class object and assign the server-name to a property, even form will call this class object. Please give some advice.
4
1693
by: Jeff smith | last post by:
I am reading the value from config file using the below statement. System.Configuration.ConfigurationSettings.AppSettings("MY_DATA") How to write data back to the same location? I am using .Net Framework 1.1. Thanks,
1
1737
by: Lialie | last post by:
Hello,all I found it easy to read configures from a config file. But how can I set a special value to an item or write it into the original file? I have tried this: import ConfigParser config = ConfigParser.ConfigParser() config.read('a.conf') config.get('Main', 'Something') # That is OK.
4
1847
by: apriebe47 | last post by:
Alright, I realize this is probably very basic to be posted on this newsgroup but I cannot figure out what is causing my problem. Here is the code I am using below: from getpass import getpass configfile = file('config.txt', 'w') serverPassword = configfile.readline() if serverPassword == '': print "Server warning: No password has been set!"
0
8968
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8787
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.