VB.net 读写ini的问题,希望好心人贡献下时间,帮忙
解决下~~ Imports System.Text ''声明 Inherits System.Windows.Forms.Form ''命名空间 Declare Function GetPrivateProfileInt Lib "kernel32 " Alias "GetPrivateProfileIntA " ( _ ByVal lpApplicationName As String,_ ByVal lpKeyName As String,_ ByVal nDefault As Integer,_ ByVal lpFileName As String) As Integer Declare Function GetPrivateProfileString Lib "kernel32 " Alias "GetPrivateProfileStringA " ( _ ByVal lpApplicationName As String,_ ByVal lpDefault As String,_ ByVal lpReturnedString As StringBuilder,_ ByVal nSize As Integer,_ ByVal lpFileName As String) As Integer Declare Function WritePrivateProfileString Lib "kernel32 " Alias "WritePrivateProfileStringA " ( _ ByVal lpApplicationName As String,_ ByVal lpString As String,_ ByVal lpFileName As String) As Integer Declare Function GetPrivateProfileSection Lib "kernel32 " Alias "GetPrivateProfileSectionA " ( _ ByVal lpAppName As String,_ ByVal lpFileName As String) As Integer Declare Function WritePrivateProfileSection Lib "kernel32 " Alias "WritePrivateProfileSectionA " ( _ ByVal lpAppName As String,_ ByVal lpFileName As String) As Integer Public Const FILE_NAME = "./test.ini " ''设置路径 Private Sub Form1_Load(ByVal sender As System.Object,_ ByVal e As System.EventArgs) Handles MyBase.Load If (System.IO.File.Exists(FILE_NAME)) Then Dim strCaption As New StringBuilder(256) GetPrivateProfileString( "Form ","Caption ","Default Caption ",_ strCaption,strCaption.Capacity,FILE_NAME) Me.Text = strCaption.ToString() Me.Width = GetPrivateProfileInt( "Form ","Width ",Me.Width,FILE_NAME) Me.Height = GetPrivateProfileInt( "Form ","Height ",Me.Height,FILE_NAME) Me.Left = GetPrivateProfileInt( "Form ","Left ",Me.Left,FILE_NAME) Me.Top = GetPrivateProfileInt( "Form ","Top ",Me.Top,FILE_NAME) End If End Sub Protected Overrides Sub OnClosing( _ ByVal e As System.ComponentModel.CancelEventArgs) Dim strCaption As String = Me.Text WritePrivateProfileString( "Form ",strCaption,FILE_NAME) WritePrivateProfileString( "Form ",Me.Width.ToString(),Me.Height.ToString(),Me.Left.ToString(),Me.Top.ToString(),FILE_NAME) End Sub -------------------------------------- 上面主要用来记录Form的caption,还有大小位置 麻烦懂的好心人帮忙注释下,上面那5段的作用,还有下面的作用 如果我新加了一个text
Box1,这个text
Box1要能保存住里面的值,该怎么
加上去,我不会哦 以后不懂读写ini的人看到这题应该都会了吧~~~希望好心人贡献下~~~~ __________________________________________________________________________ 你的这段
代码应该是来自VB6.0的,源码应该是日本人写的,我猜 GetPrivateProfileInt ''从ini
文件中取得数值 GetPrivateProfileString ’从ini
文件中取得
文字列 WritePrivateProfileString ‘向ini
文件中写入
文字列 GetPrivateProfileSection ‘从被指定的SECTION中取得KEY ,值 WritePrivateProfileSection‘向初始化的SECTION中写入KEY ,值 __________________________________________________________________________ 这个建议直接使用IDE提供的本地资源设置
功能. 它的说明就是: 使用 Visual Stu
dio,可以创建新的应用程序设置,并将它们绑定到窗体或窗体的控件上的
属性,这样这些设置就会在运行时
自动加载和保存。 关键词:应用程序设置,创建 __________________________________________________________________________ 把Text
Box1的值写入ini
文件,然后载入程序的时候再读取ini
文件的对应的值到Text
Box1就可以了 在OnClosing()子程序中
添加: Dim s As String=Text
Box1.Text ''写入ini ''形如:Text
Box1_Value=... WritePrivateProfileString( "Form ","Text
Box1_Value ",s,FILE_NAME) __________________________________________________________________________ GetPrivateProfileString( "Form ","text
Box2 ",Nothing,FILE_NAME) Text
Box2.Text = strCaption.ToString() 解释下,在句子后面
加上这一句,,,Nothing 可以设置为""也可以设置为vbnullstring strcaption.capacity自己看下注释是字节长度的意思,这句是GET,下面的是Write ---- Private Sub Text
Box2_TextChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Text
Box2.TextChanged WritePrivateProfileString( "Form ",Text
Box2.Text.ToString(),FILE_NAME) End Sub __________________________________________________________________________ 没人给出如果读写Text
Box的,,感谢第一位给的注释,汗~~最主要的是得~~Get~ __________________________________________________________________________ VB2005中有个My.Settings命名空间,可以将设置保存在xml形式的config
文件中,很方便的啊 __________________________________________________________________________ 你去看看我的
博客吧,里面有很详细的说明 http://blog.csdn.net/zzy1254/archive/2007/05/22/1620308.aspx __________________________________________________________________________
原文链接:https://www.f2er.com/vb/263820.html