IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Coder
Search Coder Group:

Advanced
-Collapse +Expand Coder Study Test
PRESTWOODCERTIFIED
-Collapse +Expand Coder Store
PRESTWOODSTORE

Prestwood eMagazine

October Edition
Subscribe now! It's Free!
Enter your email:

   ► KBRole-Based T...Coding & OOGeneral .Net...   Print This     
  From the January 2016 Issue of Prestwood eMag
 
Coder General .Net Concepts:
Storing your .NET Application Settings
 
Posted 16 years ago on 2/26/2008 and updated 8/26/2008
Take Away: Storing settings and configuration information for your .NET application can be achieved in a number of ways including using the app.config file and the ConfigurationManager class, using the registry, and in your own custom configuration file.

KB100891

The ConfigurationManager.AppSettings mechanism for accessing your application settings is extremely convenient except that it is read-only and loads once at runtime. If you did a search on modifying app.config at runtime you would find many solutions that for example load in and rewrite the XML file with the updated setting, which (in my opinion) ends up feeling like you are writing a configuration file. Writing your own configuration file is definitely an acceptable solution, but in many cases a developer would like to use something that’s built in to their programming language of choice. Writing information to the registry is also acceptable but from doing some reading you will find that Microsoft (with Windows Vista and moving forward) is urging developers to make their applications less intrusive to the operating system, marking the “beginning of the end” of the Windows registry. Another option (which I came upon after wading through the results of the search I mention above), which I will describe here, is to use the Settings class.

In Visual Studio 2005 create a new project, I have named mine SettingsExample. Click on Project->SettingsExample Properties in the menu bar, click Settings, and you will be prompted to create a settings file. You will end up on a screen like:

I am going to use a string setting called myUser for this example. You will notice that this setting’s scope is User which allows it to be updated at runtime, if you select Application, it is read-only and can only be modified through the IDE.

In my program, I write the following code:

static void Main(string[] args)
{
string
myUser = (string)Properties.Settings.Default.myUser;
if
(myUser.Length <= 0)
{
Console
.WriteLine("Who will be using this program?");
string
newUser = Console.ReadLine();
Properties.Settings.Default.myUser = newUser;
Properties.Settings.Default.Save();
Console
.WriteLine("Thanks " + newUser +
", next time I'll know who
you are.\n");
}
else
{
Console
.WriteLine("Welcome back " + myUser + "!\n");
}
}

The first time I run the program and enter my name the output is:

Who will be using this program?
Adam
Thanks Adam, next time I’ll know who you are.

The second time I run the program the output is:

Welcome back Adam!

There you have it, a simple way to store your application’s settings. Hopefully this helps.


Comments

1 Comments.
Share a thought or comment...
Comment 1 of 3

I found this pretty interesting. I have one question, however. Where is the value stored?

I can various .config files which describe the property, but after running the program twice, I cannot find a file that contains the name (e.g. Adam).

added later

I am answering my own question.  I found it in:

C:\Documents and Settings\Ken\Local Settings\ . . .

Application Data\SettingsExample\ . . .

SettingsExample.exe_Url_u2cl4dyh3gvnojly3gn0ktpxtban10rv\1.0.0.0\user.config

Posted 16 years ago

Comment 2 of 3

I'm glad you found this KB post intersting inspiring you to ask (and answer) your own question.

-Adam

Posted 16 years ago

Comment 3 of 3

I have an windows application using a grid control.  I have set up user settings for each column of the grid to enable the user to determine various properties of those columns.  For example whether a particular user wants this column Visible or not.

Everything works fine for this. But I have had many companies request ing the ability to deliver a standard set of user settings (meaning, they want all users to have the same columns properties).  Application Settings will not work as each company may want to have different columns properties, so they need to be able to modify these settings.  And other companies/users want the ability to have each user determine what column properties they want.

So what would be nice is to tell them to set up one workstation with the column properties they want.  Then enable them to copy those user settings to different users.  Can this be done?

Posted 16 years ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P161A1
Enter key:
KB Post Contributed By Adam Lum:

Adam Lum is a part time developer for Prestwood Software and participates in this online community when time allows. His day-to-day work is C# coding but his current intrests (right now) are Ruby on Rails and iOS programming with Objective-C.  He has also coded several projects in Java, C++, ASP Classic, and PHP.  His personal website can be found at adamlum.com.

Visit Profile

 KB Article #100891 Counter
16714
Since 4/2/2008
Sales Website: www.prestwood.com Or visit our legacy sales site: 
legacy.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]