473,890 Members | 1,990 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 3162
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 ShowSplashScree n; // make sure we're public or add a
public property get/set
public string Name;

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

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

private User _MyUser;

.....

public bool LoadUserSetting s()
{
string fileName = Application.Use rAppDataPath + @"\usersettings .xml";
// UserAppDataPath ensures that it uses the logged on user's ApplicationData
folder.
if (File.Exists(fi leName))
{
XmlSerializer ser = new XmlSerializer(t ypeof(User));
FileStream fs = new FileStream(file Name, 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 SaveUserSetting s()
{
string fileName = Application.Use rAppDataPath + @"\usersettings .xml";

XmlSerializer ser = new XmlSerializer(t ypeof(User));
FileStream fs = new FileStream(file Name, FileMode.Create );
try
{
ser.Serialize(f s, CurrentUser);
return true;
}
finally
{
fs.Close();
}
return false;
}

HTH,
ShaneB

"Todd Beaulieu" <Todd Be******@discus sions.microsoft .com> wrote in message
news:C3******** *************** ***********@mic rosoft.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******@discus sions.microsoft .com> wrote in message
news:C3******** *************** ***********@mic rosoft.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**********@d iscussions.micr osoft.com> wrote in message
news:34******** *************** ***********@mic rosoft.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
3044
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 whether it was docked or not. I added the following code to my "OnConnection" function but it fails with an error, "Run-time exception thrown : System.IO.IOException - Bad file name or number." With applicationObject.CommandBars("SampleToolbar")
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 file. I want each user to read/write to a file in their user directory. Does anyone know how to do this?
3
1947
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 Property class that can deal with user's custom settings?
5
4519
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 password would be different than the one the user enters for ASP.NET forms authentication. The ID/password in question is used by the application, itself, for accessing the SQL Server. Thanks in advance.
4
2504
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 personal profile (e.g. last files opened, custom settings, etc...). What is the best place to save them? At first, I thought to save them in the registry, in the CURRENT_USER section, but I'm not sure every user can have read/write access to it. ...
4
4020
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 Server should allow login only authorized users - Admin of the server should have good and comfortable acces to "permitted users list", with opportunity to add or remove entries in a good way. Program should also keep info. where to store...
10
4290
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. Because of this, I am unable to store these settings in the App.Config file, as this gets updated every time the application does, and there doesn't appear to be a way of preventing this. Most of my application settings are kept in the...
10
3678
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 key="DEBUGCleanFile" value="true"/> </appSettings>
4
1803
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 different values? Example I want to save information about the user of the software, eg, name, address, telephone #. -- L. A. Jones
3
1705
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 screens ... are all of these choices being saved XML, a database or where? Is the OK button a "Save" button or are the changes committed at the time you check/uncheck? I'm doing everything Winforms and while my system works well, I'm always looking...
0
9972
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
11210
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...
1
10898
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10444
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
7154
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5832
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...
0
6032
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4653
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
4255
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.