473,480 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Create 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 8261
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.Load();
Console.WriteLine("Name:"+mac2.Name);
Console.WriteLine("X:"+mac2.X);
Console.WriteLine("Y:"+mac2.Y);
using System;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;

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.GetExecutingAssembly().Location + ".xml";
}

public MyAppConfig()
{
Name = Assembly.GetExecutingAssembly().GetName().Name;
}

public static MyAppConfig Load()
{
using(StreamReader sr = new StreamReader(MyAppConfig.file))
{
string xml = sr.ReadToEnd();
MyAppConfig mac = MyAppConfig.FromXmlString(xml);
return mac;
}
}

public void Save()
{
string myXml = this.ToXmlString();
using(StreamWriter sw = new StreamWriter(MyAppConfig.file))
{
sw.Write(myXml);
}
}

public string ToXmlString()
{
string data = null;
XmlSerializer ser = new XmlSerializer(typeof(MyAppConfig));
using(StringWriter sw = new StringWriter())
{
ser.Serialize(sw, this);
sw.Flush();
data = sw.ToString();
return data;
}
}

public static MyAppConfig FromXmlString(string xmlString)
{
if ( xmlString == null )
throw new ArgumentNullException("xmlString");

MyAppConfig mac = null;
XmlSerializer ser = new XmlSerializer(typeof(MyAppConfig));
using (StringReader sr = new StringReader(xmlString))
{
mac = (MyAppConfig)ser.Deserialize(sr);
}
return mac;
}
}
}
--
William Stacey, MVP
http://mvp.support.microsoft.com

"ALI-R" <ne****@microsoft.com> wrote in message
news:eG**************@TK2MSFTNGP14.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.GetAssemConfig();
mac.X = 20;
mac.Y = 40;
MyAssemConfig.SetAppConfig(mac);

MyAssemConfig mac2 = MyAssemConfig.GetAssemConfig();
Console.WriteLine("Name:"+mac2.Name);
Console.WriteLine("X:"+mac2.X);
Console.WriteLine("Y:"+mac2.Y);

The Class:
---------------------
using System;
using System.Xml.Serialization;
using System.IO;
using System.IO.IsolatedStorage;
using System.Reflection;

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.GetExecutingAssembly().Location + ".xml";
isoFileName = "AssemConfig.xml";
}

public MyAssemConfig()
{
Name = Assembly.GetExecutingAssembly().GetName().Name;
}

public static MyAssemConfig Reset()
{
MyAssemConfig mac = new MyAssemConfig();
MyAssemConfig.SetAppConfig(mac);
return mac;
}

public static MyAssemConfig GetAssemConfig()
{
IsolatedStorageFile isoStore =
IsolatedStorageFile.GetStore(IsolatedStorageScope. Assembly |
IsolatedStorageScope.User, null, null);

if ( ! MyAssemConfig.ISOFileExists(isoStore, isoFileName) )
return new MyAssemConfig();

string xml = MyAssemConfig.ReadFromISOFile(isoStore, isoFileName);
try
{
MyAssemConfig mac = MyAssemConfig.FromXmlString(xml);
return mac;
}
catch
{
// Xml not valid - probably corrupted. Rewrite it with defaults.
return MyAssemConfig.Reset();
}
}

public static void SetAppConfig(MyAssemConfig appConfig)
{
if ( appConfig == null )
throw new ArgumentNullException("appConfig");
string xml = appConfig.ToXmlString();

IsolatedStorageFile isoStore =
IsolatedStorageFile.GetStore(IsolatedStorageScope. Assembly |
IsolatedStorageScope.User, null, null);
MyAssemConfig.WriteToISOFile(isoStore, isoFileName, xml);
}

// public static MyAppConfig Load()
// {
// using(StreamReader sr = new StreamReader(MyAppConfig.file))
// {
// string xml = sr.ReadToEnd();
// MyAppConfig mac = MyAppConfig.FromXmlString(xml);
// return mac;
// }
// }

// public void Save()
// {
// string myXml = this.ToXmlString();
// using(StreamWriter sw = new StreamWriter(MyAppConfig.file))
// {
// sw.Write(myXml);
// }
// }

public string ToXmlString()
{
string data = null;
XmlSerializer ser = new XmlSerializer(typeof(MyAssemConfig));
using(StringWriter sw = new StringWriter())
{
ser.Serialize(sw, this);
sw.Flush();
data = sw.ToString();
return data;
}
}

public static MyAssemConfig FromXmlString(string xmlString)
{
if ( xmlString == null )
throw new ArgumentNullException("xmlString");

MyAssemConfig mac = null;
XmlSerializer ser = new XmlSerializer(typeof(MyAssemConfig));
using (StringReader sr = new StringReader(xmlString))
{
mac = (MyAssemConfig)ser.Deserialize(sr);
}
return mac;
}

private static bool ISOFileExists(IsolatedStorageFile isoStore, string
fileName)
{
if ( isoStore == null )
throw new ArgumentNullException("isoStore");
if ( fileName == null || fileName.Length == 0 )
return false;

string[] names = isoStore.GetFileNames("*");
foreach(string name in names)
{
if ( string.Compare(name, fileName, true) == 0 )
return true;
}
return false;
}

private static void WriteToISOFile(IsolatedStorageFile isoStore, string
fileName, string data)
{
// Assign the writer to the store and the file TestStore.
using(StreamWriter writer = new StreamWriter(new
IsolatedStorageFileStream(fileName, FileMode.Create, isoStore)))
{
// Have the writer write "Hello Isolated Storage" to the store.
writer.Write(data);
}
}

private static string ReadFromISOFile(IsolatedStorageFile isoStore, string
fileName)
{
string sb = null;
// This code opens the TestStore.txt file and reads the string.
using(StreamReader reader = new StreamReader(new
IsolatedStorageFileStream(fileName, FileMode.Open, isoStore)))
{
sb = reader.ReadToEnd();
}
return sb.ToString();
}
}
}

--
William Stacey, MVP
http://mvp.support.microsoft.com

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:#j**************@TK2MSFTNGP15.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.Load();
Console.WriteLine("Name:"+mac2.Name);
Console.WriteLine("X:"+mac2.X);
Console.WriteLine("Y:"+mac2.Y);
using System;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;

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.GetExecutingAssembly().Location + ".xml";
}

public MyAppConfig()
{
Name = Assembly.GetExecutingAssembly().GetName().Name;
}

public static MyAppConfig Load()
{
using(StreamReader sr = new StreamReader(MyAppConfig.file))
{
string xml = sr.ReadToEnd();
MyAppConfig mac = MyAppConfig.FromXmlString(xml);
return mac;
}
}

public void Save()
{
string myXml = this.ToXmlString();
using(StreamWriter sw = new StreamWriter(MyAppConfig.file))
{
sw.Write(myXml);
}
}

public string ToXmlString()
{
string data = null;
XmlSerializer ser = new XmlSerializer(typeof(MyAppConfig));
using(StringWriter sw = new StringWriter())
{
ser.Serialize(sw, this);
sw.Flush();
data = sw.ToString();
return data;
}
}

public static MyAppConfig FromXmlString(string xmlString)
{
if ( xmlString == null )
throw new ArgumentNullException("xmlString");

MyAppConfig mac = null;
XmlSerializer ser = new XmlSerializer(typeof(MyAppConfig));
using (StringReader sr = new StringReader(xmlString))
{
mac = (MyAppConfig)ser.Deserialize(sr);
}
return mac;
}
}
}
--
William Stacey, MVP
http://mvp.support.microsoft.com

"ALI-R" <ne****@microsoft.com> wrote in message
news:eG**************@TK2MSFTNGP14.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.config, where MyApp.exe is whatever
you've chosen to call your application executable.

This Whatever.exe.config 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\Yourcompany\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.GetFolder 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.config?? right??
"Ian Griffiths [C# MVP]" <ia*************@nospam.nospam> wrote in message
news:Os**************@TK2MSFTNGP15.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.config, where MyApp.exe is whatever
you've chosen to call your application executable.

This Whatever.exe.config 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\Yourcompany\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.GetFolder 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**************@TK2MSFTNGP15.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.GetAssemConfig();
mac.X = 20;
mac.Y = 40;
MyAssemConfig.SetAppConfig(mac);

MyAssemConfig mac2 = MyAssemConfig.GetAssemConfig();
Console.WriteLine("Name:"+mac2.Name);
Console.WriteLine("X:"+mac2.X);
Console.WriteLine("Y:"+mac2.Y);

The Class:
---------------------
using System;
using System.Xml.Serialization;
using System.IO;
using System.IO.IsolatedStorage;
using System.Reflection;

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.GetExecutingAssembly().Location + ".xml";
isoFileName = "AssemConfig.xml";
}

public MyAssemConfig()
{
Name = Assembly.GetExecutingAssembly().GetName().Name;
}

public static MyAssemConfig Reset()
{
MyAssemConfig mac = new MyAssemConfig();
MyAssemConfig.SetAppConfig(mac);
return mac;
}

public static MyAssemConfig GetAssemConfig()
{
IsolatedStorageFile isoStore =
IsolatedStorageFile.GetStore(IsolatedStorageScope. Assembly |
IsolatedStorageScope.User, null, null);

if ( ! MyAssemConfig.ISOFileExists(isoStore, isoFileName) )
return new MyAssemConfig();

string xml = MyAssemConfig.ReadFromISOFile(isoStore, isoFileName);
try
{
MyAssemConfig mac = MyAssemConfig.FromXmlString(xml);
return mac;
}
catch
{
// Xml not valid - probably corrupted. Rewrite it with defaults.
return MyAssemConfig.Reset();
}
}

public static void SetAppConfig(MyAssemConfig appConfig)
{
if ( appConfig == null )
throw new ArgumentNullException("appConfig");
string xml = appConfig.ToXmlString();

IsolatedStorageFile isoStore =
IsolatedStorageFile.GetStore(IsolatedStorageScope. Assembly |
IsolatedStorageScope.User, null, null);
MyAssemConfig.WriteToISOFile(isoStore, isoFileName, xml);
}

// public static MyAppConfig Load()
// {
// using(StreamReader sr = new StreamReader(MyAppConfig.file))
// {
// string xml = sr.ReadToEnd();
// MyAppConfig mac = MyAppConfig.FromXmlString(xml);
// return mac;
// }
// }

// public void Save()
// {
// string myXml = this.ToXmlString();
// using(StreamWriter sw = new StreamWriter(MyAppConfig.file))
// {
// sw.Write(myXml);
// }
// }

public string ToXmlString()
{
string data = null;
XmlSerializer ser = new XmlSerializer(typeof(MyAssemConfig));
using(StringWriter sw = new StringWriter())
{
ser.Serialize(sw, this);
sw.Flush();
data = sw.ToString();
return data;
}
}

public static MyAssemConfig FromXmlString(string xmlString)
{
if ( xmlString == null )
throw new ArgumentNullException("xmlString");

MyAssemConfig mac = null;
XmlSerializer ser = new XmlSerializer(typeof(MyAssemConfig));
using (StringReader sr = new StringReader(xmlString))
{
mac = (MyAssemConfig)ser.Deserialize(sr);
}
return mac;
}

private static bool ISOFileExists(IsolatedStorageFile isoStore, string
fileName)
{
if ( isoStore == null )
throw new ArgumentNullException("isoStore");
if ( fileName == null || fileName.Length == 0 )
return false;

string[] names = isoStore.GetFileNames("*");
foreach(string name in names)
{
if ( string.Compare(name, fileName, true) == 0 )
return true;
}
return false;
}

private static void WriteToISOFile(IsolatedStorageFile isoStore, string
fileName, string data)
{
// Assign the writer to the store and the file TestStore.
using(StreamWriter writer = new StreamWriter(new
IsolatedStorageFileStream(fileName, FileMode.Create, isoStore)))
{
// Have the writer write "Hello Isolated Storage" to the store.
writer.Write(data);
}
}

private static string ReadFromISOFile(IsolatedStorageFile isoStore, string fileName)
{
string sb = null;
// This code opens the TestStore.txt file and reads the string.
using(StreamReader reader = new StreamReader(new
IsolatedStorageFileStream(fileName, FileMode.Open, isoStore)))
{
sb = reader.ReadToEnd();
}
return sb.ToString();
}
}
}

--
William Stacey, MVP
http://mvp.support.microsoft.com

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:#j**************@TK2MSFTNGP15.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.Load();
Console.WriteLine("Name:"+mac2.Name);
Console.WriteLine("X:"+mac2.X);
Console.WriteLine("Y:"+mac2.Y);
using System;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;

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.GetExecutingAssembly().Location + ".xml";
}

public MyAppConfig()
{
Name = Assembly.GetExecutingAssembly().GetName().Name;
}

public static MyAppConfig Load()
{
using(StreamReader sr = new StreamReader(MyAppConfig.file))
{
string xml = sr.ReadToEnd();
MyAppConfig mac = MyAppConfig.FromXmlString(xml);
return mac;
}
}

public void Save()
{
string myXml = this.ToXmlString();
using(StreamWriter sw = new StreamWriter(MyAppConfig.file))
{
sw.Write(myXml);
}
}

public string ToXmlString()
{
string data = null;
XmlSerializer ser = new XmlSerializer(typeof(MyAppConfig));
using(StringWriter sw = new StringWriter())
{
ser.Serialize(sw, this);
sw.Flush();
data = sw.ToString();
return data;
}
}

public static MyAppConfig FromXmlString(string xmlString)
{
if ( xmlString == null )
throw new ArgumentNullException("xmlString");

MyAppConfig mac = null;
XmlSerializer ser = new XmlSerializer(typeof(MyAppConfig));
using (StringReader sr = new StringReader(xmlString))
{
mac = (MyAppConfig)ser.Deserialize(sr);
}
return mac;
}
}
}
--
William Stacey, MVP
http://mvp.support.microsoft.com

"ALI-R" <ne****@microsoft.com> wrote in message
news:eG**************@TK2MSFTNGP14.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.config. 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.config?? right??
"Ian Griffiths [C# MVP]" <ia*************@nospam.nospam> wrote in message
news:Os**************@TK2MSFTNGP15.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.config, where MyApp.exe is whatever
you've chosen to call your application executable.

This Whatever.exe.config 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\Yourcompany\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.GetFolder 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.config. 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.config?? right??
"Ian Griffiths [C# MVP]" <ia*************@nospam.nospam> wrote in message
news:Os**************@TK2MSFTNGP15.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.config, where MyApp.exe is whatever
you've chosen to call your application executable.

This Whatever.exe.config 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\Yourcompany\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.GetFolder 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**************@TK2MSFTNGP10.phx.gbl...
Yes, when deployed you need to make sure that the App.config file is renamed myapp.exe.config. 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.config?? right??
"Ian Griffiths [C# MVP]" <ia*************@nospam.nospam> wrote in message news:Os**************@TK2MSFTNGP15.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.config, where MyApp.exe is
whatever you've chosen to call your application executable.

This Whatever.exe.config 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\Yourcompany\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.GetFolder 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**************@TK2MSFTNGP10.phx.gbl...
Yes, when deployed you need to make sure that the App.config file is renamed myapp.exe.config. 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.config?? right??
"Ian Griffiths [C# MVP]" <ia*************@nospam.nospam> wrote in message news:Os**************@TK2MSFTNGP15.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.config, where MyApp.exe is
whatever you've chosen to call your application executable.

This Whatever.exe.config 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\Yourcompany\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.GetFolder 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
3033
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...
33
3442
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...
0
1366
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...
2
4549
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">...
4
1661
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...
3
5289
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...
4
1676
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...
1
1722
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...
4
1828
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 ...
0
6912
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...
0
7092
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...
1
6744
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6981
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...
0
3000
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...
0
2989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1304
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 ...
1
565
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
188
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...

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.