My.Settings for C#?

joe_pool_is

Contributor
Joined
Jan 18, 2004
Messages
507
Location
Longview, TX [USA]
Microsoft has a new Settings field in Visual Studio 2005 which can be accessed at design time via "Project | <Project Name> Properties..." item on the menu bar.

I have successfully created the Settings fields for my project, but I need to read/write values for the user.config file using these settings at run time. I found an article on how to do it here: http://msdn2.microsoft.com/en-us/library/shytyc55.aspx but it seems to only be accessible via the "My" keyword, which is not part of the C# language.

Does anyone know the proper way to access this information using C# on 2005?
 
as another alternative method, you can access the settings part ( as with My.Settings ) like this ...
Code:
[size=2][color=#0000ff]private [/color][/size][size=2][color=#0000ff]void[/color][/size][size=2] button1_Click([/size][size=2][color=#0000ff]object[/color][/size][size=2] sender, [/size][size=2][color=#008080]EventArgs[/color][/size][size=2] e)[/size]
[size=2]{
	WindowsApplication1.Properties.[/size][size=2][color=#008080]Settings[/color][/size][size=2] s = [/size][size=2][color=#0000ff]new[/color][/size][size=2] WindowsApplication1.Properties.[/size][size=2][color=#008080]Settings[/color][/size][size=2]();
 
[/size][size=2][color=#0000ff]foreach[/color][/size][size=2] (System.Configuration.[/size][size=2][color=#008080]SettingsProperty[/color][/size][size=2] prop [/size][size=2][color=#0000ff]in[/color][/size][size=2] s.Properties)[/size]
[size=2]	{
[/size][size=2][color=#008080]	Console[/color][/size][size=2].WriteLine(prop.DefaultValue.ToString());
	}
}[/size]
just for fun as another method :) , happy new year :D
 
Last edited:
PlausiblyDamp said:
Sorry PlausiblyDamp, but I just was not able to make that work. Maybe it did not explain enough to me. I already had those settings defined in the project's Properties segment, but I did not know how to access them via the program (i.e. read/write the data with C#).

dynamic_sysop said:
as another alternative method, you can access the settings part ( as with My.Settings ) like this ...
dynamic_sysop: I did get this to work. Thanks! I noticed that all of my Settings were stored under the "Default" tag. Does this mean that others can be created on the fly as well? I'm going to play around with that, but if you know any more secrets, I would appreciate them!
 
PlausiblyDamp said:
If you go through the properties dialog and create your settings there then you can access them, see the attached sample for an example.
That makes sense. Sorry for being so thick headed about this. Thanks for your patience.
 
Back
Top