细看了,原并不是内嵌!数据只是存在Documents and Settings\LzmTW\Local Settings\Application Data下头.
Author:水如烟先说说一般数据类型的贮存。
建一个TestSettings工程,输出类型为Windows Application.
Form1界面如图:
显示信息用的是三个Label,分别是Label1、Label2、Label3,其Text如图示。
下面定义三个变量来贮存数据,分别是Logon、Logoff、Count。
使Soltution Explorer的视图Show Files,定位到My Project下的Settings,双击(或右键Open),界面如图:
如是就定义了三个变量。注意的是Scope选择User。它还有一个选择,是Application,而选它如同在Resource,是不可更改的。
回到Form1,点View Code,添加如下代码:
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Label1.Text = String.Format(Me.Label1.Text, My.Settings.Logon)
Me.Label2.Text = String.Format(Me.Label2.Text, My.Settings.Logoff)
Me.Label3.Text = String.Format(Me.Label3.Text, My.Settings.count)
End Sub
End Class
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Label1.Text = String.Format(Me.Label1.Text, My.Settings.Logon)
Me.Label2.Text = String.Format(Me.Label2.Text, My.Settings.Logoff)
Me.Label3.Text = String.Format(Me.Label3.Text, My.Settings.count)
End Sub
End Class
上面只是完成了定义和显示。下面是对三个变量的赋值。
新建一个MyApplication.Class文件,添加如下代码:
Namespace My
Friend Class MyApplication
Private Sub MyApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
My.Settings.Logoff = Now.ToString
End Sub
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
My.Settings.Logon = Now.ToString
My.Settings.count += 1
End Sub
End Class
End Namespace
Friend Class MyApplication
Private Sub MyApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
My.Settings.Logoff = Now.ToString
End Sub
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
My.Settings.Logon = Now.ToString
My.Settings.count += 1
End Sub
End Class
End Namespace
编译后运行程序,退出再运行,结果如你所愿。
如是涉及数据集合的,如Collection,DataSet等,因为需要New,所以细节上有些技巧。图片多,太长了,另文写吧。