473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 3123
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
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...

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.