我有以下代碼:My.Settings不保存變量
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語句,讓我相信我做錯了什麼,並且設置沒有保存。對此有何意見?謝謝。
在加載事件的末尾添加My.Settings.Save – dbasnett
@dbasnett DERP,謝謝。 – SCGB