Link to home
Start Free TrialLog in
Avatar of AlexFM
AlexFM

asked on

Application Settings in C# 8.0

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.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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
Avatar of AlexFM
AlexFM

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of heintalus
heintalus

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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();
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
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
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();
  }
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();
  }
Avatar of AlexFM

ASKER

Settings.Default.Save - this is what was missing. Thanks.