如何从用户控件访问默认设置?

时间:2015-09-27 03:12:30

标签: c# user-controls settings

如何从用户控制中访问默认设置?

当我使用此代码时

namespace SearchControl{
public partial class SearchControl : UserControl   
{
     string ConncStr = Properties.Settings["myConnectionString"].toString();
}}

没有工作

Properties.Settings["myConnectionString"].toString()

我想访问Properties.Settings [“myConnectionString”] 来自项目的usercontrol,其中包含usercontrol。

3 个答案:

答案 0 :(得分:0)

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var secGroup = config.SectionGroups["userSettings"]; // or applicationSettings

if (secGroup != null)
{
    var clientSettings = (ClientSettingsSection)secGroup.Sections[0];

    var settingElementCollection = clientSettings.Settings;

    var setting = settingElementCollection.Get(Name);

    var value = setting.Value.ValueXml.InnerText;
}

答案 1 :(得分:0)

string conStr = Properties.Settings.Default.myConnectionString;

答案 2 :(得分:-1)

大多数陈述以;而不是}结尾。

namespace SearchControl;

public partial class SearchControl : UserControl   
{
 string ConncStr = Properties.Settings.Default.myConnectionString;
}

请参阅https://msdn.microsoft.com/en-us/library/bb397759(v=vs.110).aspx