我有以下代码:
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
基本上,当表单加载时我想检查My.Settings中是否有任何内容,如果没有,请将值设置为默认值,但是再次打开程序时,它不会执行else语句,让我相信我做错了什么,设置没有保存。对此有何输入?感谢。
答案 0 :(得分:2)
您必须在My.Settings
中调用保存功能My.Settings.Save()
您应该在更新设置功能的末尾添加它。这将保存下一个会话的所有设置。
这里有一个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
答案 1 :(得分:1)
尝试使用String.IsNullOrWhiteSpace()
代替= Nothing
。
将您的子更改为:
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