vb.net My.Settings 不保存变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18984391/
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:13:22  来源:igfitidea点击:

My.Settings not saving variables

vb.netvisual-studio-2012

提问by

I have the following code:

我有以下代码:

Imports System.IO

Public Class Form2

Dim strInitialDirectory As String
Dim strInitialFile As String
Const strDefaultFileDirectory As String = "C:\Users\Sam\Desktop\Visual Basic\Test"
Const strDefaultFileName As String = "\test.txt"
Dim strFileJPath1 As String = strInitialDirectory + strInitialFile
Dim strFileJPath2 As String = strDefaultFileDirectory + strDefaultFileName

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    If My.Settings.strInitialDirectory = Nothing Or My.Settings.strInitialFile = Nothing Then
        My.Settings.strInitialDirectory = strDefaultFileDirectory
        My.Settings.strInitialFile = strDefaultFileName
    Else
        MsgBox("Else executed") ' debug 
        strInitialDirectory = strDefaultFileDirectory
        strInitialFile = strDefaultFileName
        strInitialDirectory = My.Settings.strInitialDirectory
        strInitialFile = My.Settings.strInitialFile
    End If
End Sub
End Class

Basically, when the form loads I'd like it to check if there is anything in My.Settings and if not, set the values to the defaults, however upon opening the program again, it does not execute the else statement, leaving me to believe I'm doing something wrong and the settings aren't saving. Any input into this? Thanks.

基本上,当表单加载时,我希望它检查 My.Settings 中是否有任何内容,如果没有,将值设置为默认值,但是再次打开程序时,它不会执行 else 语句,让我去相信我做错了什么并且设置没有保存。对此有何意见?谢谢。

采纳答案by char1es

You have to call the save function in My.Settings

您必须在 My.Settings 中调用保存功能

My.Settings.Save()

You should add this at the end of an update settings function. This will save all the settings for the next session.

您应该在更新设置功能的末尾添加它。这将保存下一个会话的所有设置。

Here us an example of an UpdateSettings function:

下面是一个 UpdateSettings 函数的例子:

Private Sub UpdateSettings()

    Console.WriteLine("Saving Settings")

    My.Settings.WO_imprevu = CheckBoxImprevu.Checked
    My.Settings.WO_prevu = CheckBoxPrevu.Checked
    My.Settings.Date_type = ComboBoxDateType.SelectedIndex
    My.Settings.Date_debut = DateTimePickerDebut.Value

    My.Settings.Save()

    Console.WriteLine("Settings Saved")
End Sub

回答by Alsace

Try using String.IsNullOrWhiteSpace()instead of = Nothing.

尝试使用String.IsNullOrWhiteSpace()而不是= Nothing.

Change your sub to this:

将您的子更改为:

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    If String.IsNullOrWhiteSpace(My.Settings.strInitialDirectory) OrElse String.IsNullOrWhiteSpace(My.Settings.strInitialFile) Then
        My.Settings.strInitialDirectory = strDefaultFileDirectory
        My.Settings.strInitialFile = strDefaultFileName
    Else
        MsgBox("Else executed") ' debug 
        strInitialDirectory = strDefaultFileDirectory
        strInitialFile = strDefaultFileName
        strInitialDirectory = My.Settings.strInitialDirectory
        strInitialFile = My.Settings.strInitialFile
    End If
End Sub