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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Todd Beaulieu

    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?
  • ShaneB

    #2
    Re: How to store user settings in custom/user XML file - not app.confi

    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 Beaulieu@discus sions.microsoft .com> wrote in message
    news:C334EEF3-062E-48E4-AA6B-0DF5D73E660F@mi crosoft.com...[color=blue]
    > 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?[/color]


    Comment

    • Angelos Karantzalis

      #3
      Re: How to store user settings in custom/user XML file - not app.confi

      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 Beaulieu@discus sions.microsoft .com> wrote in message
      news:C334EEF3-062E-48E4-AA6B-0DF5D73E660F@mi crosoft.com...[color=blue]
      > 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[/color]
      trying[color=blue]
      > 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?[/color]


      Comment

      • UAError

        #4
        Re: How to store user settings in custom/user XML file - not app.confi

        On Tue, 2 Nov 2004 14:24:04 -0800, you wrote:
        [color=blue]
        >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?[/color]

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

        %USERPROFILE%

        for example

        %USERPROFILE%\A pplication 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\Windo ws\CurrentVersi on\Explorer\Use r
        Shell Folders\AppData "
        to determine whether the user has redirected the
        "Applicatio n 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

        Download 23 C# Code Samples



        Not sure if its in here

        101 Visual Basic and C# Code Samples


        Comment

        • UAError

          #5
          Re: How to store user settings in custom/user XML file - not app.confi

          "ShaneB" <stormfire1@yah oo.com> wrote:
          [color=blue]
          >
          >public bool SaveUserSetting s()
          >{
          > string fileName = Application.Use rAppDataPath + @"\usersettings .xml";
          >[/color]

          Hmmm... Application.Use rAppDataPath that's much better!



          Learn something new every day.

          Comment

          • Todd Beaulieu

            #6
            Re: How to store user settings in custom/user XML file - not app.c

            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.

            Comment

            • ShaneB

              #7
              Re: How to store user settings in custom/user XML file - not app.confi

              so do I :)

              ShaneB
              [color=blue]
              >
              > Learn something new every day.[/color]


              Comment

              • ShaneB

                #8
                Re: How to store user settings in custom/user XML file - not app.c

                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" <ToddBeaulieu@d iscussions.micr osoft.com> wrote in message
                news:34948B07-4D23-41ED-BEBA-A7AD7EA31EE3@mi crosoft.com...[color=blue]
                > 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.[/color]


                Comment

                Working...