有多种方法可以做到这一点。例如,您可以将值存储在文本文件、二进制文件、XML 文件、ini 文件或配置文件中。最简单的选择是使用内置的设置框架。为此,请转到您的项目属性并单击设置选项卡。创建一个名为“DZien”的新设置并将类型设置为整数。然后,在代码中,您可以像这样访问和设置配置文件中的值:
Dim dzein As Integer = My.Settings.Dzien.ToString() ' Read value from config file
My.Settings.Dzien = 5 ' Write new value to config file
相反,如果你想在文本文件中存储一个数字,你可以这样做:
Dim dzein As Integer = Integer.Parse(File.ReadAllText("C:\Plik.txt")) ' Read value from text file
File.WriteAllText("C:\Plik.txt", dzein.ToString())
在您的情况下,您正在编写一个 Windows Phone 应用程序,您可以这样做:
Partial Public Class_1
Inherits PhoneApplicationPage
Private dzien As Integer
Public Sub New()
InitializeComponent()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
Try
dzien = CInt(IsolatedStorageSettings.ApplicationSettings("dzien"))
Catch ex As KeyNotFoundException
dzien = 0
End Try
If dzien = 0 Then
' ...
End If
End Sub
Private Sub Class_1_Unloaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Unloaded
IsolatedStorageSettings.ApplicationSettings("dzien") = dzien
End Sub
End Class