不管是什么好用就行啊^*^,分不多60,来的就给点,呵呵,希望大家帮个忙

解决方案 »

  1.   

    VB6中一个非常好用的读写Ini文件的模块        网上读写Ini文件的例子只有几篇相同的文章,而并不好用,奇怪的是各网站都是同样的例程,高手也就罢了,三下五除二就搞定,初学者会被搞得一头雾水,看着一个好好的模块就是不能用。
            所以我整理了一下(最早是在腾讯答一个贴子时写的),这个也就是修改了一下,不是我自已的发明(至于这个代码起先不知是谁写的),不过非常的好用 新建模块(建议不使用注册表) 命名为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 lpKeyName As Any, 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, i, 1) 
    End If 
    Next 
    Else 
    Temp% = WritePrivateProfileString(SectionName, KeyWord, DefString, AppProFileName(iniFileName)) 
    '将缺省值写入INI文件 
    s = DefString 
    End If 
    GetIniS = s 
    End Function '**************************************获取Ini数值(Function)*************************************************** 
    Function GetIniN(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefValue As Long) As Integer 
    Dim d As Long, s As String 
    d = DefValue 
    GetIniN = GetPrivateProfileInt(SectionName, KeyWord, DefValue, AppProFileName(iniFileName)) 
    If d <> DefValue Then 
    s = "" & d 
    d = WritePrivateProfileString(SectionName, KeyWord, s, AppProFileName(iniFileName)) 
    End If 
    End Function '***************************************写入字符串值(Sub)************************************************** 
    Sub SetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValStr As String) 
    Dim res% 
    res% = WritePrivateProfileString(SectionName, KeyWord, ValStr, AppProFileName(iniFileName)) 
    End Sub 
    '****************************************写入数值(Sub)****************************************************** 
    Sub SetIniN(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValInt As Long) 
    Dim res%, s$ 
    s$ = Str$(ValInt) 
    res% = WritePrivateProfileString(SectionName, KeyWord, s$, AppProFileName(iniFileName)) 
    End Sub 
    ''这是我自已不知道怎样清除一个键(keyword) 时
    写的一个清除字符串值的过程,是有write函数写入一个空的值实现的,'Sub DelIniS(ByVal SectionName As String, ByVal KeyWord As String) 
    'Dim retval As Integer 
    'retval = WritePrivateProfileString(SectionName, KeyWord, "", 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, KeyWord, 0&, AppProFileName(iniFileName)) 
    End Sub '如果是清除section就少写一个Key多一个“”。 
    '**************************************清除 Section"段"(Sub)*********************************************** 
    Sub DelIniSec(ByVal SectionName As String) '清除section 
    Dim RetVal As Integer 
    RetVal = WritePrivateProfileString(SectionName, 0&, "", 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 KeyWord As String, ByVal ValInt As Long) 写入数 
              'GetIniN(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefValue As Long)读取数 
              'SetIniS (ByVal SectionName As String, ByVal KeyWord As String, ByVal ValStr As String) 写入字符 
              'GetIniS(ByVal SectionName As String, ByVal KeyWord 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", "backupnumber", lstBackUp.ListCount
            '......
        End If
        iniFileName = Initemp '继续使用原来的Ini文件
    End Sub
     
    '你在你的电脑子上搜索*.ini就看一下,最上面是部,每一个key后面是一个对应的值 再次重申,这个Ini读写的例子是网上找来改写的,我只是为方便大家使用。算不得我的发明。(我本来不想写VB6的东西---我本来就不曾认真学过VB6的了,实在不明白为什么要传播一个不完整的例程这么久),如果有任何不当的地方,就“权且”吧。
      

  2.   

    几个操作INI文件的函数   [ 2005-04-08 | 作者:Mndsoft | 来自:本站原创]
      Function GetKeyVal(ByVal INIFileLoc As String, ByVal Section As String, ByVal Key As String)
        If Dir(INIFileLoc) = "" Then MsgBox "文件没有找到: " & INIFileLoc & vbCrLf & "无法提取所需的值", vbExclamation, "INI 文件没有找到": Exit Function
        Dim RetVal As String, Worked As Integer
        RetVal = String$(255, 0)
        Worked = GetPrivateProfileString(Section, Key, "", RetVal, Len(RetVal), INIFileLoc)    If Worked = 0 Then
            GetINI = ""
        Else
            GetINI = Left(RetVal, InStr(RetVal, Chr(0)) - 1)
        End If
    End Function
    Function AddToINI(ByVal INIFileLoc As String, ByVal Section As String, ByVal Key As String, ByVal Value As String)
    '其实应该自动创建文件???
        If Dir(INIFileLoc) = "" Then MsgBox "文件没有找到: " & INIFileLoc & vbCrLf & "无法增加所需的值", vbExclamation, "INI 文件没有找到": Exit Function
        WritePrivateProfileString Section, Key, Value, INIFileLoc
    End Function
    Function DeleteSection(ByVal INIFileLoc As String, ByVal Section As String)
        If Dir(INIFileLoc) = "" Then MsgBox "文件没有找到: " & INIFileLoc & vbCrLf & "无法删除所需的值", vbExclamation, "INI 文件没有找到": Exit Function
        WritePrivateProfileString Section, vbNullString, vbNullString, INIFileLoc
    End Function
    Function DeleteKey(ByVal INIFileLoc As String, ByVal Section As String, ByVal Key As String)
        If Dir(INIFileLoc) = "" Then MsgBox "文件没有找到: " & INIFileLoc & vbCrLf & "无法删除所需的值", vbExclamation, "INI 文件没有找到": Exit Function
        WritePrivateProfileString Section, Key, vbNullString, INIFileLoc
    End Function
    Function DeleteKeyValue(ByVal INIFileLoc As String, ByVal Section As String, ByVal Key As String)
        If Dir(INIFileLoc) = "" Then MsgBox "文件没有找到: " & INIFileLoc & vbCrLf & "Please refer To code in Function 'DeleteKeyValue'", vbExclamation, "INI 文件没有找到": Exit Function
        WritePrivateProfileString Section, Key, "", INIFileLoc
    End Function'以下功能自己做喽,呵呵
    Function RenameSection()
    End Function
    Function RenameKey()
    End Function
     
      

  3.   

    '之前先申明GetPrivateProfileString
    '读取INI文件
    Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long'读取INI文件内容;
    Public Function gReadINI(ByVal strFileName As String, ByVal strApplication As String, strKeyValue As String) As String
        
        Dim lngFlag As Long
        Dim strTemp As String
        
        strTemp = Space(255)    lngFlag = GetPrivateProfileString(strApplication, strKeyValue, "", strTemp, 255, strFileName)
        
        gReadINI = Trim(strTemp)End Function
      

  4.   

    写INI文件无非运用API模块没有必要去用别人的,自己组织个人认为连模块都没必要装个API_GUID,参考里面的示范就行了
    Private 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
    Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    Private Sub Form_Load()
        'KPD-Team 1999
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        Dim Ret As String, NC As Long
        'Write the setting to the file (c:\test.ini) under
        '   Project1 -> Keyname
        WritePrivateProfileString App.Title, "KeyName", "This is the value", "c:\test.ini"
        'Create a buffer
        Ret = String(255, 0)
        'Retrieve the string
        NC = GetPrivateProfileString(App.Title, "KeyName", "Default", Ret, 255, "C:\test.ini")
        'NC is the number of characters copied to the buffer
        If NC <> 0 Then Ret = Left$(Ret, NC)
        'Show our string
        MsgBox Ret
        'Delete the file
        Kill "c:\test.ini"
    End Sub