Author Topic: Playtime with the user.config  (Read 2542 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Playtime with the user.config
« on: December 28, 2008, 08:45:11 AM »

I've been having a play .. just trying to get my head around the user.config.
Can be used in Console as well as Win Forms.

excuse the vulgarity of this :)

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/// add a reference to System.Configuration.dll in Solution Explorer->References->Net
using System.Configuration;

namespace ConsoleApplication216
{
    class Program
    {
        static void Main(string[] args)
        {
            Configuration config =
                        ConfigurationManager.OpenExeConfiguration(
                        ConfigurationUserLevel.PerUserRoamingAndLocal);
            Console.WriteLine("Local user config path: {0}", config.FilePath);

            string UserNameAlias = Properties.Settings.Default.UserNameAlias;

            Console.WriteLine("\nThe program is meant to be used by {0}", UserNameAlias);
            Console.WriteLine("\nIf you aren't {0}, please type your userName [ Enter to Contunue]", UserNameAlias);
         
            string response = Console.ReadLine().Trim();
           
            if ( !String.IsNullOrEmpty(response) )
            {
                Console.WriteLine("\nWelcome {0}.", response);
                Properties.Settings.Default.UserNameAlias = response;   
            } else
            {
                Console.WriteLine("\nThanks {0}, Welcome back.", UserNameAlias);
            }
            DateTime currentTime = DateTime.Now;
            DateTime LastUsed = Properties.Settings.Default.LastTimeUsed;
           
            if (LastUsed.Year < 5)
            {
                Console.WriteLine("This is the initial use of this program.");
            }
            else
            {
                Console.WriteLine("It's been {0} seconds since this program was last used",
                   currentTime.Subtract(LastUsed).Seconds ) ;
            }

            Properties.Settings.Default.LastTimeUsed = currentTime;
            Properties.Settings.Default.Save(); 

            Console.ReadKey();
        }
    }
}





kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

tjr

  • Guest
Re: Playtime with the user.config
« Reply #1 on: December 29, 2008, 01:06:13 AM »
Neat stuff. Never tried using any of these *.config things for .NET as the need has never reared it's head. Although if it did I would be looking for something a little more simple. One bad habit .NET inherited from the Java world is XML files. XML is garbage for anything, you name it there is a better alternative. 



Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Playtime with the user.config
« Reply #2 on: December 29, 2008, 01:47:19 AM »
perception is a personal thing .. I rather appreciate XML.

user.config and app.config are pretty usefull. I'm investigating using the user.config for saving things like comboBox selections,  textBox values, selected path to files, and other dynamic info that it may be 'professional' to offer as 'last used'.

.... and then there is the fact that the values can be DataBound :)
 
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

TonyT

  • Guest
Re: Playtime with the user.config
« Reply #3 on: December 29, 2008, 02:30:38 AM »
The System.Configuration namespace in .NET 2 is quite powerful,
and it can't be any simpler to use, provided one knows how.

With just a few clicks and without having to write any code, You
can do things like databind the font or size/location of your form
to persistent settings that are saved in user.config, and the whole
thing is completely automatic, not even requiring you to write code
to restore the settings when your app loads.

I use Settings extensively, although I have yet to work out how
to adapt the related classes to have settings that have AutoCAD
profile scope, rather than user scope.

perception is a personal thing .. I rather appreciate XML.

user.config and app.config are pretty usefull. I'm investigating using the user.config for saving things like comboBox selections,  textBox values, selected path to files, and other dynamic info that it may be 'professional' to offer as 'last used'.

.... and then there is the fact that the values can be DataBound :)
 

Ken Alexander

  • Newt
  • Posts: 61
Re: Playtime with the user.config
« Reply #4 on: December 29, 2008, 11:45:56 AM »
, and the whole thing is completely automatic, not even requiring you to write code
to restore the settings when your app loads.


I believe you need to force a save when saving settings from a referenced dll.
Ken Alexander