哇,你的逻辑很难理解。怎么样的东西:
Private mVariable1 = Nothing
Private mVariable2 As String = "Something"
Private mFilePath As String = System.Environment.GetEnvironmentVariable("APPDATA")
Private mFileSpec As String = System.IO.Path.Combine(mFilePath, "textfile.txt")
' dgp rev 3/13/2012
Private ReadOnly Property Variable1 As String
Get
If mVariable1 Is Nothing Then
If System.IO.File.Exists(mFileSpec) Then
Try
Dim sr = New StreamReader(mFileSpec)
mVariable1 = sr.ReadToEnd
sr.Close()
Catch ex As Exception
mVariable1 = "error"
End Try
Else
mVariable1 = mVariable2
End If
End If
Return mVariable1
End Get
End Property
如果文件不存在,变量将在第一次引用时使用文本文件或硬编码值初始化。当然,一旦程序关闭,没有什么是持久的。这就是我对您的要求有点模糊的地方。