469,648 Members | 1,172 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,648 developers. It's quick & easy.

How to store user settings in custom/user XML file - not app.confi

Hello,

I'm using the configuration block to store user settings in the app.config
file. As this exe will reside on a network drive, I can't have users trying
to update the master app.config file. I want each user to read/write to a
file in their user directory.

Does anyone know how to do this?
Nov 22 '05 #1
5 2863
I use xml serialization for that sort of thing. There are tons of articles
all over the net and on msdn.microsoft.com that explain the ins and outs of
it. To briefly illustrate, here is some code samples (not production code
of course...but illustrative code).

// A simple user class to illustrate the idea
// "User.cs".
public class User
{
public bool ShowSplashScreen; // make sure we're public or add a
public property get/set
public string Name;

public User(string name)
{
ShowSplashScreen = true;
Name = name;
}
}

// Use something like this to load/save the user settings.
// You will need to add references to System.IO, System.Xml.Serialization,
and System.Windows.Forms
// "someclass.cs"

private User _MyUser;

.....

public bool LoadUserSettings()
{
string fileName = Application.UserAppDataPath + @"\usersettings.xml";
// UserAppDataPath ensures that it uses the logged on user's ApplicationData
folder.
if (File.Exists(fileName))
{
XmlSerializer ser = new XmlSerializer(typeof(User));
FileStream fs = new FileStream(fileName, FileMode.Open);
try
{
_MyUser = (User) ser.Deserialize(fs); // Note that you do not
need to create a new User first before trying to deserialize one.
return true;
}
finally
{
fs.Close();
}
return false;
}

public bool SaveUserSettings()
{
string fileName = Application.UserAppDataPath + @"\usersettings.xml";

XmlSerializer ser = new XmlSerializer(typeof(User));
FileStream fs = new FileStream(fileName, FileMode.Create);
try
{
ser.Serialize(fs, CurrentUser);
return true;
}
finally
{
fs.Close();
}
return false;
}

HTH,
ShaneB

"Todd Beaulieu" <Todd Be******@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
Hello,

I'm using the configuration block to store user settings in the app.config
file. As this exe will reside on a network drive, I can't have users
trying
to update the master app.config file. I want each user to read/write to a
file in their user directory.

Does anyone know how to do this?

Nov 22 '05 #2
There is a way to get the current user home directory from the .NET
Environment, although I can't rememeber it off the top of my head. i think
you shouldn't save that data in the App.Config file altogether but rather
save it in a user-specific file, or have an App.Config per user, stored in
their user directory.

Angel
O:]
"Todd Beaulieu" <Todd Be******@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
Hello,

I'm using the configuration block to store user settings in the app.config
file. As this exe will reside on a network drive, I can't have users trying to update the master app.config file. I want each user to read/write to a
file in their user directory.

Does anyone know how to do this?

Nov 22 '05 #3
Thanks, Shane.

I was hoping to be able to use the configuration block, and assumed there is
some way to have it support the use of additional, external files.

Your sample should help me if give up on the block. I guess I just wanted to
try using the blocks, since I'm new to .net, with the assumption that I'm
gaining stability and features that would otherwise take me quite a while to
evolve my own code to.
Nov 22 '05 #4
so do I :)

ShaneB

Learn something new every day.

Nov 22 '05 #5
The approach I posted is pretty common but as with anything in the
programming world, there are a million ways to skin a cat. App.config files
weren't designed to help with loading/saving user-specific settings.
Honestly, I haven't come across a real use for them...yet.

Good luck!
ShaneB

"Todd Beaulieu" <To**********@discussions.microsoft.com> wrote in message
news:34**********************************@microsof t.com...
Thanks, Shane.

I was hoping to be able to use the configuration block, and assumed there
is
some way to have it support the use of additional, external files.

Your sample should help me if give up on the block. I guess I just wanted
to
try using the blocks, since I'm new to .net, with the assumption that I'm
gaining stability and features that would otherwise take me quite a while
to
evolve my own code to.

Nov 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Bob | last post: by
5 posts views Thread by Guadala Harry | last post: by
4 posts views Thread by Piotrekk | last post: by
10 posts views Thread by Paul Cheetham | last post: by
10 posts views Thread by Brett Romero | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.