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.
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you add a setting called InitialDirectory, you can access it like this
string mySetting = Properties.Settings.Defaul t.InitialD irectory;
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.Defaul t.InitialD irectory= .....new Value;
Properties.Settings.Defaul t.Save();
string mySetting = Properties.Settings.Defaul
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.Defaul
Properties.Settings.Defaul
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="C Test.Prope rties" GeneratedClassName="Settin gs">
<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,4 80}</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
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)"
<Profiles />
<Settings>
<Setting Name="MainFormLocation" Type="System.String" Scope="User">
<Value Profile="(Default)">{0,0}<
</Setting>
<Setting Name="MainFormSize" Type="System.String" Scope="User">
<Value Profile="(Default)">{640,4
</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.D efault.
Bob
Application Name.Properties.Settings.D
Bob
As e1v pointed out after my post you dont even need the Application Name i.e. you can just use
Properties.Settings.Defaul t.WhatEver
Andy
Properties.Settings.Defaul
Andy
Create a property:
Name: OpenFileDialogDirectory
Type: string
Scope: User
Value: C:\
And then you can use this code:
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDire ctory = ((string)Properties.Settin gs.Default ["OpenFile DialogDire ctory"]);
if (openFileDialog.ShowDialog () == DialogResult.OK)
{
Properties.Settings.Defaul t["OpenFil eDialogDir ectory"] = System.IO.Path.GetDirector yName(open FileDialog .FileName) ;
Properties.Settings.Defaul t.Save();
}
Name: OpenFileDialogDirectory
Type: string
Scope: User
Value: C:\
And then you can use this code:
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDire
if (openFileDialog.ShowDialog
{
Properties.Settings.Defaul
Properties.Settings.Defaul
}
Sorry, late post... and the code should be:
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDire ctory = Properties.Settings.Defaul t.OpenFile DialogDire ctory;
if (openFileDialog.ShowDialog () == DialogResult.OK)
{
Properties.Settings.Defaul t.OpenFile DialogDire ctory = System.IO.Path.GetDirector yName(open FileDialog .FileName) ;
Properties.Settings.Defaul t.Save();
}
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDire
if (openFileDialog.ShowDialog
{
Properties.Settings.Defaul
Properties.Settings.Defaul
}
ASKER
Settings.Default.Save - this is what was missing. Thanks.
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