We help IT Professionals succeed at work.

Application Settings in C# 8.0

AlexFM
AlexFM asked
on
Medium Priority
9,952 Views
Last Modified: 2013-06-16
Project - Properties window in C# 8.0 contains "Application Settings" tab. How can I use them in code? For example, I have some Open File dialog in the program and I add string InitialiDirectory setting with default empty value.
How can I use it in the code to set Open File dialog initial directory, and keep new directory when dialog is closed?
Any other sample will be OK, basically I want to do two things: read current value and change it.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Alex, have you seen this?

Using My.Settings in Visual Basic 2005
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/vbmysettings.asp

I realize that this is a VB.NET reference, but the concepts are very similar.

Bob

Author

Commented:
Yes, this is direct link from the Settings page in Visual Studio. I didn't understand it and this is why I asked this question.
Having had a play with it you can do it like this, assuming that MyApp is the name of your program & MySetting is defined as one of your settings.

MessageBox.Show(MyApp.Properties.Settings.Default.MySetting);

to change it you can just do
MyApp.Properties.Settings.Default.MySetting = "I've Changed";

but to persist the change you need to call
MyApp.Properties.Settings.Default.Save();

I must admit that the help available was severly lacking, I had to use the designer to bind a control to the property & then look through the forms Designer.cs code to find out how they were doing it.

HTH
Andy

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
e1v

Commented:
If you add a setting called InitialDirectory, you can access it like this
string mySetting = Properties.Settings.Default.InitialDirectory;

If you will change it at runtime, it must be a user setting, not app setting, then you can change and save it with

Properties.Settings.Default.InitialDirectory= .....new Value;
Properties.Settings.Default.Save();
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
1) The Settings.settings file + Settings.Designer is under the Properties folder.
2) The default file looks like this:

<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)">
  <Profiles>
    <Profile Name="(Default)" />
  </Profiles>
  <Settings />
</SettingsFile>

3) To add a setting, right-click on the project, and select 'Properties...'
4) Enter a name, type, scope, and cvalue.

Example:
MainFormLocation, string, User, {0,0}
MainFormSize, string, User, {640, 480}

5) The resulting file looks like this:

<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="CTest.Properties" GeneratedClassName="Settings">
  <Profiles />
  <Settings>
    <Setting Name="MainFormLocation" Type="System.String" Scope="User">
      <Value Profile="(Default)">{0,0}</Value>
    </Setting>
    <Setting Name="MainFormSize" Type="System.String" Scope="User">
      <Value Profile="(Default)">{640,480}</Value>
    </Setting>
  </Settings>
</SettingsFile>

6) C# does not, sadly, implement the My.Settings namespace.  It is generated code for VB.NET.  I tried to find a way to recreate the same thing in C# from a VB.NET class, but it is deep.  I can show you what I tried, if you want.

Bob
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Cool B-)  I like it ;)

Application Name.Properties.Settings.Default.

Bob
As e1v pointed out after my post you dont even need the Application Name i.e. you can just use

Properties.Settings.Default.WhatEver

Andy
Top Expert 2006

Commented:
Create a property:

  Name: OpenFileDialogDirectory
  Type: string
  Scope: User
  Value: C:\

And then you can use this code:

  OpenFileDialog openFileDialog = new OpenFileDialog();
  openFileDialog.InitialDirectory = ((string)Properties.Settings.Default["OpenFileDialogDirectory"]);
  if (openFileDialog.ShowDialog() == DialogResult.OK)
  {
    Properties.Settings.Default["OpenFileDialogDirectory"] = System.IO.Path.GetDirectoryName(openFileDialog.FileName);
    Properties.Settings.Default.Save();
  }
Top Expert 2006

Commented:
Sorry, late post... and the code should be:

  OpenFileDialog openFileDialog = new OpenFileDialog();
  openFileDialog.InitialDirectory = Properties.Settings.Default.OpenFileDialogDirectory;
  if (openFileDialog.ShowDialog() == DialogResult.OK)
  {
    Properties.Settings.Default.OpenFileDialogDirectory = System.IO.Path.GetDirectoryName(openFileDialog.FileName);
    Properties.Settings.Default.Save();
  }

Author

Commented:
Settings.Default.Save - this is what was missing. Thanks.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.