每当我的C#项目需要一个新的应用设置时,我会通过 PROJECT -> Properties -> Settings 添加它。目前我有大约20个应用设置在我的C#项目中,但是它们是杂乱的。
为了能够在运行时更改设置,我通过迭代设置创建了一个简单的设置面板。
为了能够在运行时更改设置,我通过迭代设置创建了一个简单的设置面板。
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);
}
它运作良好。但是标签的顺序与我在Visual Studio UI中输入它们的顺序相同,这是不合逻辑的。
是否有一种方法可以在Visual Studio中重新排列设置的顺序或在运行时对System.Configuration.SettingsPropertyCollection进行排序?
因为SettingsPropertyCollection是一个IEnumerable,所以我尝试使用LINQ,像这样:
Properties.Settings.Default.Properties.OrderBy(s => s.Name)
但它编译不通过,抱怨缺少SettingsPropertyCollection的OrderBy扩展。