Search code examples
c#linqvisual-studio-2012application-settings

How to order application settings


Whenever there is a need for a new application setting in my C# project, I add it via PROJECT -> Properties -> Settings. Currently I have about 20 application settings in my C# project, but they are out of order.

To be able to change the settings during runtime I created a simple settings panel by iterating over the settings.

foreach (System.Configuration.SettingsProperty prop in Properties.Settings.Default.Properties)
{
    Label caption = new Label();
    caption.Text = prop.Name;
    caption.Location = new Point(10, this.Height - 70);
    caption.Size = new Size(100, 13);
    caption.Anchor = AnchorStyles.Left | AnchorStyles.Top;

    TextBox textbox = new TextBox();
    textbox.Name = prop.Name;
    textbox.Text = Properties.Settings.Default[prop.Name].ToString();
    textbox.Location = new Point(120, this.Height - 70);
    textbox.Size = new Size(this.Width - 140, 23);
    textbox.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
    if (prop.IsReadOnly)
        textbox.ReadOnly = true;

    this.Height += 30;
    this.Controls.Add(caption);
    this.Controls.Add(textbox);
}

It works fine. But the labels are in the same unlogical order as I entered them in the Visual Studio UI.

Is there a way to either rearrange the order of the settings in Visual Studio or sort the System.Configuration.SettingsPropertyCollection during runtime?

Because SettingsPropertyCollection is an IEnumerable I was trying to use LINQ like this:

Properties.Settings.Default.Properties.OrderBy(s => s.Name)

But it didn't compile complaining about missing OrderBy extension for SettingsPropertyCollection.


Solution

  • You can try:

    Properties.Settings.Default.Properties.OfType<SettingsProperty>().OrderBy(s => s.Name)