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.
LVL 48
AlexFMAsked:
Who is Participating?
 
heintalusCommented:
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
0
 
Bob LearnedCommented:
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
0
 
AlexFMAuthor 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.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
e1vCommented:
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();
0
 
Bob LearnedCommented:
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
0
 
Bob LearnedCommented:
Cool B-)  I like it ;)

Application Name.Properties.Settings.Default.

Bob
0
 
heintalusCommented:
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
0
 
Expert1701Commented:
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();
  }
0
 
Expert1701Commented:
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();
  }
0
 
AlexFMAuthor Commented:
Settings.Default.Save - this is what was missing. Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.