新建模块(建议不使用注册表) 命名为rwini
'ini文件在有回车换行符会出错,经过测试,汉字要小于86字节,英言文要小于143字节才能返回列表框。(这是我以前的code,是记录列表框内容的)
Option Explicit
Public iniFileName As String
Public Declare Function GetPrivateProfileInt Lib "kernel32 " Alias "GetPrivateProfileIntA " (ByVal lpApplicationName As String,ByVal lpKeyName As String,ByVal nDefault As Long,ByVal lpFileName As String) As Long
Public Declare Function GetPrivateProfileString Lib "kernel32 " Alias "GetPrivateProfileStringA " (ByVal lpApplicationName As String,ByVal lpKeyName As Any,ByVal lpDefault As String,ByVal lpReturnedString As String,ByVal nSize As Long,ByVal lpFileName As String) As Long
Public Declare Function WritePrivateProfileString Lib "kernel32 " Alias "WritePrivateProfileStringA " (ByVal lpApplicationName As String,ByVal lpString As Any,ByVal lpFileName As String) As Long
'****************************************获取Ini字符串值(Function)******************************************
Function GetIniS(ByVal SectionName As String,ByVal KeyWord As String,ByVal DefString As String) As String
Dim ResultString As String * 144,Temp As Integer
Dim s As String,i As Integer
Temp% = GetPrivateProfileString(SectionName,KeyWord," ",ResultString,144,AppProFileName(iniFileName))
'检索关键词的值
If Temp% > 0 Then '关键词的值不为空
s = " "
For i = 1 To 144
If Asc(Mid$(ResultString,i,1)) = 0 Then
Exit For
Else
s = s & Mid$(ResultString,1)
End If
Next
Else
Temp% = WritePrivateProfileString(SectionName,DefString,AppProFileName(iniFileName))
'将缺省值写入INI文件
s = DefString
End If
GetIniS = s
End Function
'**************************************获取Ini数值(Function)***************************************************
Function GetIniN(ByVal SectionName As String,ByVal DefValue As Long) As Integer
Dim d As Long,s As String
d = DefValue
GetIniN = GetPrivateProfileInt(SectionName,DefValue,AppProFileName(iniFileName))
If d <> DefValue Then
s = " " & d
d = WritePrivateProfileString(SectionName,s,AppProFileName(iniFileName))
End If
End Function
'***************************************写入字符串值(Sub)**************************************************
Sub SetIniS(ByVal SectionName As String,ByVal ValStr As String)
Dim res%
res% = WritePrivateProfileString(SectionName,ValStr,AppProFileName(iniFileName))
End Sub
'****************************************写入数值(Sub)******************************************************
Sub SetIniN(ByVal SectionName As String,ByVal ValInt As Long)
Dim res%,s$
s$ = Str$(ValInt)
res% = WritePrivateProfileString(SectionName,s$,AppProFileName(iniFileName))
End Sub
' '这是我自已不知道怎样清除一个键(keyword) 时
写的一个清除字符串值的过程,是有write函数写入一个空的值实现的, 'Sub DelIniS(ByVal SectionName As String,ByVal KeyWord As String)
'Dim retval As Integer
'retval = WritePrivateProfileString(SectionName,AppProFileName(iniFileName))
'End Sub
'其实0&表示前面的一个被清除,我多写了一个“”,如果是清除section就少写一个Key多一个“”。
'***************************************清除KeyWord "键 "(Sub)*************************************************
Sub DelIniKey(ByVal SectionName As String,ByVal KeyWord As String)
Dim RetVal As Integer
RetVal = WritePrivateProfileString(SectionName,0&,AppProFileName(iniFileName))
End Sub
'如果是清除section就少写一个Key多一个“”。
'**************************************清除 Section "段 "(Sub)***********************************************
Sub DelIniSec(ByVal SectionName As String) '清除section
Dim RetVal As Integer
RetVal = WritePrivateProfileString(SectionName,AppProFileName(iniFileName))
End Sub
'*************************************定义Ini文件名(Function)***************************************************
'定义ini文件名
Function AppProFileName(iniFileName)
AppProFileName = App.Path & "\ " & iniFileName & ".ini "
End Function
#######################################################################
'用法: 首先 定义iniFileName= "文件名 " 不需要 加ini后缀
'这就是说,你可以赋值给iniFileName就可以写入记录,而且你可以随时写入不同的ini文件(不管这个文件是否已存在),通过修改这个公用变量。
'然后 DelInikey(ByVal SectionName As String,ByVal KeyWord As String) 清除键
'DelIniSec(ByVal SectionName As String)) 清除部
'SetIniN(ByVal SectionName As String,ByVal ValInt As Long) 写入数
'GetIniN(ByVal SectionName As String,ByVal DefValue As Long)读取数
'SetIniS (ByVal SectionName As String,ByVal ValStr As String) 写入字符
'GetIniS(ByVal SectionName As String,ByVal ValStr As String) 读取字符
调用例子如下:
Sub RiniN()
Dim Initemp As String
Initemp = iniFileName '暂存原来的Ini文件名
iniFileName = App.EXEName '写入到另外一个Ini文件,App.EXEName是你的程序的名程
If GetIniN( "lstBackup ","backupnumber ",0) < lstBackUp.ListCount Then
'这里的第三个参数“0”表示在没有找到指定的键值时返回的缺省值为“0”
SetIniN "lstBackup ",lstBackUp.ListCount
'......
End If
iniFileName = Initemp '继续使用原来的Ini文件
End Sub
建立与读取.ini文件
作者: cww
虽然进入win95之後,一般读写ini文件被读写Registry所取代,但我们还是可以透过
win31的传统方式读写ini文件,以存程式目前的相关设定,而於下一次程式执行时再
读回来。目前建议使用GetSetting SaveSetting的方式存於Registry中,不用目前
的方式。 储存程式的设定
'请於form中放3个TextBox,一个CommandBox
Private Declare Function GetPrivateProfileString Lib "kernel32 " _
Alias "GetPrivateProfileStringA " (ByVal lpApplicationName As String,_
ByVal lpKeyName As Any,_
ByVal lpReturnedString As String,_
ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32 " _
Alias "WritePrivateProfileStringA " (ByVal lpApplicationName As String,_
ByVal lpFileName As String) As Long
Private Sub Command1_Click()
Dim success As Long
success = WritePrivateProfileString( "MyApp ","text1 ",Text1.Text,"c:\aa.ini ")
'叁数一 Section Name
'叁数二 於.ini中的项目
'叁数三 项目的内容
'叁数四 .ini文件的名称
success = WritePrivateProfileString( "MyApp ","text2 ",Text2.Text,"c:\aa.ini ")
success = WritePrivateProfileString( "MyApp2 ","text3 ",Text3.Text,"c:\aa.ini ")
End Sub
Private Sub Form_load()
Dim ret As Long
Dim buff As String
buff = String(255,0)
ret = GetPrivateProfileString( "Myapp ",buff,256,"c:\aa.ini ")
'若.ini MyApp中无text1,则采用叁数三的值
Text1.Text = buff
buff = String(255,"c:\aa.ini ")
Text2.Text = buff
buff = String(255,0)
ret = GetPrivateProfileString( "Myapp2 ","c:\aa.ini ")
Text3.Text = buff
End Sub
以前学习邹建的,我一直在用 方法:首先再模组里面写入: 1,'在d盤根目錄新建 aaa.ini文件 '模擬程序 '模組中 Option Explicit '------------------------- '聲明 Declare Function WritePrivateProfileString Lib "kernel32 " Alias "WritePrivateProfileStringA " (ByVal lpApplicationName As String,ByVal lpFileName As String) As Long Declare Function GetPrivateProfileString Lib "kernel32 " Alias "GetPrivateProfileStringA " (ByVal lpApplicationName As String,ByVal lpFileName As String) As Long '獲得設置 Public Function MyGetSetting(Section As String,KeyName As String,DefaultValue As String) As String Dim X As Long Dim Holder As String * 255 X = GetPrivateProfileString(Section,KeyName,DefaultValue,Holder,254,"d:\aaa.ini ") MyGetSetting = Left$(Holder,InStr(Holder,Chr$(0)) - 1) End Function '保存設置 Public Sub MySetSetting(Section As String,KeyValue As String) Dim X As Long X = WritePrivateProfileString(Section,KeyValue,"d:\aaa.ini ") End Sub 2,Form界面上: Private Sub Form_Load() Dim strServer As String Dim strUID As String Dim strPWD As String Dim strDBName As String Dim strConString As String strServer = MyGetSetting( "服務器名 ","服務器名 "," ") strUID = MyGetSetting( "用戶名 ","用戶名 "," ") strPWD = MyGetSetting( "密碼 ","密碼 "," ") strDBName = MyGetSetting( "數據庫名 ","數據庫名 "," ") strConString = "Driver={sql Server};Network Library=TCP/IP Sockets;SERVER= " & strServer & ";UID= " & strUID & ";PWD= " & strPWD & ";DATABASE= " & strDBName & " " conn.myconn.CommandTimeout = 30 conn.myconn.Open strConString End Sub 调用MyGetSetting函数,在From_load从ini档里面读出信息,然后把服务器名,用户名,密码,数据库名串到字符串里面,然后打开 3,执行sql语句: Private Sub Command1_Click() Dim rs As ADODB.Recordset Dim sql As String sql = "select * from student " Set rs = conn.myconn.Execute(sql) If rs.RecordCount > 0 Then Set TDBGrid2.DataSource = rs End If End Sub 4,验证代码: 写入ini 档 Private Sub Command3_Click() '保存變量 MySetSetting "服務器名 ","mis25 " MySetSetting "用戶名 "," " MySetSetting "密碼 "," " MySetSetting "數據庫名 ","hh " MsgBox "保存成功 " End Sub 5,读出ini档里面的内容 Private Sub Command4_Click() '取出變量 MsgBox MyGetSetting( "服務器名 "," ") MsgBox MyGetSetting( "用戶名 "," ") MsgBox MyGetSetting( "密碼 "," ") MsgBox MyGetSetting( "數據庫名 "," ") End Sub 6,如果想保存在当前路径下:则把上面的代码改为: '獲得設置 Public Function MyGetSetting(Section As String,DefaultValue As String) As String Dim X As Long Dim Holder As String * 255 ' X = GetPrivateProfileString(Section,"d:\aaa.ini ") X = GetPrivateProfileString(Section,App.Path & "\aaa.ini ") MyGetSetting = Left$(Holder,Chr$(0)) - 1) End Function同样读出当前路径下的内容时,也把上面的代码改为 '保存設置 Public Sub MySetSetting(Section As String,KeyValue As String) Dim X As Long ' X = WritePrivateProfileString(Section,"d:\aaa.ini ") X = WritePrivateProfileString(Section,App.Path & "\aaa.ini ") End Sub 1, 在sqlServer数据库里面设置访问数据库的用户名和密码: 打开sqlserver------选择数据库下的User ----右键----New Database User---new 输入用户名和密码(2次) 2,写ini档,在ini档里面纪录用户名和密码: 3,用户使用时候只要copy 和 ini档文件就Ok,因为,程序里面设置读的是当前路径下的ini文档
|