473,506 Members | 11,491 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3136
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2996
by: Jaime Rios | last post by:
Hi, I created a COM AddIn for Word that performs the functions that it needs to, but I needed to add the ability for the toolbar created by the COM AddIn to remember it's last position and...
7
908
by: Todd Beaulieu | last post by:
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...
3
1931
by: Bob | last post by:
I am a new java convert to asp.net. I'm trying to create an web application that will allow users to change their custom settings easily. Is there a class in .net that is similar to java's...
5
4489
by: Guadala Harry | last post by:
What are my options for *securely* storing/retrieving the ID and password used by an ASP.NET application for accessing a SQL Server (using SQL Server authentication)? Please note that this ID and...
4
2473
by: Marco | last post by:
Hi to All, I'm developing an application in VB .NET. I have a question: what is the best place to save personal settings of my users? I mean, when a user uses my application, it can create a...
4
4007
by: Piotrekk | last post by:
Hi everyone. I would like to ask about good habid, of keeping program data. I have written in C# Server and Client applications, and now i need to care about authorization. What i mean is that...
10
4234
by: Paul Cheetham | last post by:
Hi, I am developing an application that needs to store some machine-specific settings. The application is going to be published on the network in order to keep the clients on the latest version....
10
3648
by: Brett Romero | last post by:
I'd like to store something such as the following the my app.config file: <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="DEBUG" value="true"/> <add...
4
1786
by: Dave | last post by:
I have some data values that will will rarely change over the life of the program. I don't think it is wise to save it in a database. Is it ok to save them in Properties.Settings if I have many...
3
1687
by: Earl | last post by:
..Net Winforms app. I'm currently using the database to store user options. But I wonder if there is a better, faster technique. For a simple example, look at the Visual Studio 2005 Tools Options...
0
7308
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,...
1
7023
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
7479
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
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
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
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
410
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.