?
Solved

Application Settings in C# 8.0

Posted on 2006-03-28
10
Medium Priority
?
9,797 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.
0
Comment
Question by:AlexFM
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16312327
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
 
LVL 48

Author Comment

by:AlexFM
ID: 16313463
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
 
LVL 10

Accepted Solution

by:
heintalus earned 2000 total points
ID: 16314023
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:e1v
ID: 16314036
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16314302
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16314318
Cool B-)  I like it ;)

Application Name.Properties.Settings.Default.

Bob
0
 
LVL 10

Expert Comment

by:heintalus
ID: 16314358
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
 
LVL 11

Expert Comment

by:Expert1701
ID: 16314383
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
 
LVL 11

Expert Comment

by:Expert1701
ID: 16314398
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
 
LVL 48

Author Comment

by:AlexFM
ID: 16318787
Settings.Default.Save - this is what was missing. Thanks.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question