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

G

Guest

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?
 
A

Angelos Karantzalis

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:]
 
U

UAError

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?

As it has already been stated; store user preferences in a
separate file under the user profile directories, somewhere
under:

%USERPROFILE%

for example

%USERPROFILE%\Application Data\MyCompany\MyApp

this is to ensure that the user has write permission to the
directory (which may not be true for the application
directory).

In the above example you should actually read
"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User
Shell Folders\AppData"
to determine whether the user has redirected the
"Application Data" folder


For an example of managing a custom XML application
configuration file look under

Working with Custom Application Files

on

Visual C# .NET Code Samples
http://www.microsoft.com/downloads/...eb-7152-454d-9936-495ffd79afd0&displaylang=en
Download 23 C# Code Samples
http://msdn.microsoft.com/vcsharp/downloads/samples/23samples/default.aspx


Not sure if its in here

101 Visual Basic and C# Code Samples
http://www.microsoft.com/downloads/...F8-033D-420B-A3B1-3074505C03F3&displaylang=en
 
G

Guest

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.
 
S

ShaneB

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
 
S

ShaneB

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top