如题

解决方案 »

  1.   

    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
     
    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
      

  2.   

    我只是想得到项目名的名称(就是INI里面代中括号的字符串),事先我并不知道项目名的名称。
      

  3.   

    看看http://www.csdn.net/develop/Article/25/25860.shtm
      

  4.   

    [转]一个绝对经典的在VB中操作.ini文件的通用类源代码
    classIniFile.cls的内容:Option Explicit'--------classIniFile.cls  代码----------------
    '这里定义了一个classIniFile类
    '一个绝对经典的在VB中操作.ini文件的通用类源代码
    '程序编写:中国青岛·许家国
    '    2002.6.16
    'E-Mail: [email protected]
    'HomePage: http://www.gojclub.com
    ''Private  member  that  holds  a  reference  to
    'the  path  of  our  ini  filePrivate strINI As String'Windows  API  Declares
    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 LongPrivate 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 LongPrivate Function MakePath(ByVal strDrv As String, ByVal strDir As String) As String    '  Makes  an  INI  file:  Guarantees  a  sub  dir
        Do While Right$(strDrv, 1) = "\"
              strDrv = Left$(strDrv, Len(strDrv) - 1)
        Loop
        
        Do While Left$(strDir, 1) = "\"
              strDir = Mid$(strDir, 2)
        Loop
        
        '  Return  the  path
        MakePath = strDrv & "\" & strDir
    End FunctionPrivate Sub CreateIni(strDrv As String, strDir As String)
        '  Make  a  new  ini  file
        strINI = MakePath(strDrv, strDir)
    End SubPublic Sub WriteIniKey(strSection As String, strKey As String, strValue As String)
        '  Write  to  strINI
        WritePrivateProfileString strSection, strKey, strValue, strINI
    End SubPublic Function GetIniKey(strSection As String, strKey As String) As String
        Dim strTmp As String
        Dim lngRet As String
        Dim I As Integer
        Dim strTmp2 As String
        
        strTmp = String$(1024, Chr(32))
        lngRet = GetPrivateProfileString(strSection, strKey, "", strTmp, Len(strTmp), strINI)
        strTmp = Trim(strTmp)
        strTmp2 = ""
        For I = 1 To Len(strTmp)
            If Asc(Mid(strTmp, I, 1)) <> 0 Then
                strTmp2 = strTmp2 + Mid(strTmp, I, 1)
            End If
        Next I
        strTmp = strTmp2
        
        GetIniKey = strTmp
    End FunctionPublic Property Let INIFileName(ByVal New_IniPath As String)
        '  Sets  the  new  ini  path
        strINI = New_IniPath
    End PropertyPublic Property Get INIFileName() As String
        '  Returns  the  current  ini  path
        INIFileName = strINI
    End Property'***************************************清除KeyWord"键"(Sub)***********************************************
    Public Function DelIniKey(ByVal SectionName As String, ByVal KeyWord As String)
        Dim RetVal As Integer
        RetVal = WritePrivateProfileString(SectionName, KeyWord, 0&, strINI)
    End Function'如果是清除section就少写一个Key多一个""。
    '**************************************清除 Section"段"(Sub)***********************************************
    Public Function DelIniSec(ByVal SectionName As String)      '清除section
        Dim RetVal As Integer
        RetVal = WritePrivateProfileString(SectionName, 0&, "", strINI)
    End FunctionForm1中的内容:Option Explicit'一个绝对经典的在VB中操作.ini文件的通用类源代码示例程序
    '程序编写:中国青岛·许家国
    '    2002.6.16
    'E-Mail: [email protected]
    'HomePage: http://www.gojclub.com'定义一个.ini类型的变量
    Dim DemoIni As New classIniFilePrivate Sub Form_Load()
        '对控件进行初始化
        Text1.Text = "测试一下"
        List1.Clear
        
        '定义.ini文件名,并写入一些初始数据
        DemoIni.INIFileName = App.Path & "\demoini.ini"
        DemoIni.WriteIniKey "系统", "启动路径", App.Path
        DemoIni.WriteIniKey "系统", "可执行程序文件名", App.EXEName
        
        '显示保存到.ini文件中的数据
        Call CmdRead_Click
    End Sub'退出程序
    Private Sub CmdExit_Click()
        Unload Me
    End Sub'读取.ini文件中已经存在的数据并显示出来
    Private Sub CmdRead_Click()
        Dim TestStr As String
        
        List1.Clear
        TestStr = DemoIni.GetIniKey("系统", "启动路径")
        List1.AddItem "系统 - 启动路径: " & TestStr
        TestStr = DemoIni.GetIniKey("系统", "可执行程序文件名")
        List1.AddItem "系统 - 可执行程序文件名: " & TestStr
        TestStr = DemoIni.GetIniKey("用户自定义", "用户数据")
        List1.AddItem "用户自定义 - 用户数据: " & TestStr
    End Sub'保存用户自定义数据到.ini文件中
    Private Sub CmdSave_Click()
        DemoIni.WriteIniKey "用户自定义", "用户数据", Text1.Text
        
        '显示保存到.ini文件中的数据
        Call CmdRead_Click
    End Sub'清除用户自定义段和段中数据
    Private Sub CmdDelete_Click()
        DemoIni.DelIniKey "用户自定义", "用户数据"
        DemoIni.DelIniSec "用户自定义"
        
        '显示保存到.ini文件中的数据
        Call CmdRead_Click
    End Sub ------看满足你的要求吗
      

  5.   

    Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As LongPublic Function GetIniSection(ByVal sSection As String) As String
        Dim sFileName As String, lRet As Long, sBuf As String, lSize As Long
        sFileName = App.Path & INI_FILENAME
        lSize = 4096
        sBuf = String(lSize, Chr(7))
        lRet = GetPrivateProfileSection(sSection, sBuf, lSize, sFileName)
        sBuf = Left(sBuf, InStr(1, sBuf, Chr(7)) - 1)
        GetIniSection = sBuf
    End Function
      

  6.   

    一种替代的方法:
    在设计ini文件格式时,添加一个段,该段下面保存所有段名
    [SectionName]
    SectionNum=3
    Section1=Database
    Section2=UID
    Section3=PSD[UID]
    ...[PWD]
    ...
      

  7.   

    captainivy(Ivy):我用了一下你给的程序,出现以下错误:以下这行“用户定义类型未定义”Dim DemoIni As New classIniFile请问是什么原因?谢谢
      

  8.   

    用GetPrivateProfileString就可以了,只要指定lpApplicationName为vbNullString,就在lpReturnedString缓冲区内装载这个ini文件所有小节的列表:Option ExplicitPrivate 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 LongPublic Function GetIniSection(ByVal sFileName As String) As Variant
        Dim lRet As Long, sBuf As String, lSize As Long
        lSize = 4096
        sBuf = String(lSize, Chr(0))
        lRet = GetPrivateProfileString(vbNullString, vbNullString, vbNullString, sBuf, lSize, sFileName)
        sBuf = Left(sBuf, lRet)
        Dim s() As String
        s = Split(sBuf, Chr(0))
        Dim i As Long
        Dim n As Long
        n = 0
        Dim buff() As String
        ReDim buff(UBound(s) - 1)
        For i = 0 To UBound(s) - 1
            buff(i) = s(i)
        Next
        Erase s
        GetIniSection = buff
        Erase buff
    End FunctionPrivate Sub Command1_Click()
        Dim s() As String
        s = GetIniSection("c:\winnt\win.ini")
        Dim i As Long
        For i = 0 To UBound(s)
           Debug.Print s(i)
        Next
        Erase s
    End Sub