VB.NET 从 My.Settings 获取默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19869072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:45:29  来源:igfitidea点击:

VB.NET get default value from My.Settings

vb.netdefaultmy.settings

提问by sinDizzy

VB2010. I understand how to save settings and load them from the My.Settings namespace. What I am trying to figure out is how to get the default value of a settings and present it to the user. I don't want to save the setting I just want to show what the current value is and what the default value is. I tried this but I don't think it does what I think it's supposed to do:

VB2010。我了解如何保存设置并从 My.Settings 命名空间加载它们。我想弄清楚的是如何获取设置的默认值并将其呈现给用户。我不想保存设置我只想显示当前值是什么以及默认值是什么。我试过这个,但我认为它没有做我认为它应该做的事情:

 Msgbox "Current setting: " & My.Settings.CustLineWidth & vbcrlf & _
        "Default setting: " & My.MySettings.Default.CustLineWidth

The default as I have setup in the IDE is 10 but the user changed it to 25. If I run the above code I get

我在 IDE 中设置的默认值为 10,但用户将其更改为 25。如果我运行上面的代码,我会得到

 Current setting: 25
 Default setting: 25

What I would like is

我想要的是

 Current setting: 25
 Default setting: 10

Solution: I iterate through all the settings and print out the current value and the default value like this

解决方案:我遍历所有设置并像这样打印出当前值和默认值

        For Each prop As Configuration.SettingsPropertyValue In My.Settings.PropertyValues
            Debug.Print("Name={0}", prop.Name)
            Debug.Print("  Value  ={0}", prop.PropertyValue.ToString)
            Debug.Print("  Default={0}", prop.Property.DefaultValue.ToString)
        Next prop

采纳答案by LarsTech

This worked for me:

这对我有用:

My.Settings.PropertyValues("CustLineWidth").Property.DefaultValue

回答by habil chegeni

Use This Code only :

仅使用此代码:

MsgBox(CStr(My.Settings.Properties.Item("CustLineWidth").DefaultValue))

This code returns default value : with unhandled error

此代码返回默认值:有未处理的错误

My.Settings.PropertyValues("CustLineWidth").Property.DefaultValue

But this one only returns last value before calling "settings.save" method : : with unhandled error

但是这个只在调用“settings.save”方法之前返回最后一个值::有未处理的错误

My.MySettings.Default.CustLineWidth

回答by Kamaro Lambert

Let's say that you have added this to your settings file, you just need to do something like below

假设您已将此添加到您的设置文件中,您只需要执行以下操作

My.Settings.YOUR_SETTING_NAME