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 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 WritePrivateProfileString Lib "kernel32" _
      Alias "WritePrivateProfileStringA" _
      (ByVal lpApplicationName As String, ByVal lpKeyName As Any, _
      ByVal lpString As Any, ByVal lpFileName As String) As LongPublic Sub Main()
  gstrConnect = Space(200)
  gstrDataBase = Space(100)
  gstrServer = Space(100)
  Call GetPrivateProfileString("toeic", "server", TOEIC_SERVER, gstrServer, Len(gstrServer), App.Path & "\toeic.ini")
  gstrServer = Trim(gstrServer)
  gstrServer = Trim(Left(gstrServer, Len(gstrServer) - 1))
  Call GetPrivateProfileString("toeic", "database", TOEIC_DATABASE, gstrDataBase, Len(gstrDataBase), App.Path & "\toeic.ini")
  gstrDataBase = Trim(gstrDataBase)
  gstrDataBase = Trim(Left(gstrDataBase, Len(gstrDataBase) - 1))
  Call GetPrivateProfileString("toeic", "connect", TOEIC_CONNECT, gstrConnect, Len(gstrConnect), App.Path & "\toeic.ini")
  gstrConnect = Trim(gstrConnect)
  gstrConnect = Trim(Left(gstrConnect, Len(gstrConnect) - 1))
  gstrConnect = gstrConnect & ";Data Source=" & gstrServer & ";Initial Catalog=" & gstrDataBase
  glLeft = GetPrivateProfileInt("photo", "left", 0, App.Path & "\toeic.ini")
  glTop = GetPrivateProfileInt("photo", "top", 0, App.Path & "\toeic.ini")
  glWidth = GetPrivateProfileInt("photo", "width", 180, App.Path & "\toeic.ini")
  glHeight = GetPrivateProfileInt("photo", "height", 230, App.Path & "\toeic.ini")
  glJPEG = GetPrivateProfileInt("photo", "jpeg", 100, App.Path & "\toeic.ini")
  If glJPEG < 0 Or glJPEG > 100 Then glJPEG = 100
  gstrImagePath = Space(200)
  Call GetPrivateProfileString("photo", "imagepath", App.Path & "\photo", gstrImagePath, Len(gstrImagePath), App.Path & "\toeic.ini")
  gstrImagePath = Trim(gstrImagePath)
  gstrImagePath = Trim(Left(gstrImagePath, Len(gstrImagePath) - 1))
  If Right(gstrImagePath, 1) <> "\" Then gstrImagePath = gstrImagePath + "\"
  gstrDocPath = Space(200)
  Call GetPrivateProfileString("toeic", "docpath", App.Path & "\doc", gstrDocPath, Len(gstrDocPath), App.Path & "\toeic.ini")
  gstrDocPath = Trim(gstrDocPath)
  gstrDocPath = Trim(Left(gstrDocPath, Len(gstrDocPath) - 1))
  If Right(gstrDocPath, 1) <> "\" Then gstrDocPath = gstrDocPath + "\"
  gstrOperatorID = ""
  gstrPassWord = ""
  gintDocHeight = GetPrivateProfileInt("docimage", "height", 135, App.Path & "\toeic.ini")
  gintDocWidth = GetPrivateProfileInt("docimage", "width", 115, App.Path & "\toeic.ini")
  
End SubPublic Sub WritePara()
  Call WritePrivateProfileString("photo", "left", CStr(glLeft), App.Path & "\toeic.ini")
  Call WritePrivateProfileString("photo", "top", CStr(glTop), App.Path & "\toeic.ini")
  Call WritePrivateProfileString("photo", "height", CStr(glHeight), App.Path & "\toeic.ini")
  Call WritePrivateProfileString("photo", "width", CStr(glWidth), App.Path & "\toeic.ini")
  Call WritePrivateProfileString("photo", "jpeg", CStr(glJPEG), App.Path & "\toeic.ini")
  Call WritePrivateProfileString("photo", "imagepath", gstrImagePath, App.Path & "\toeic.ini")
  Call WritePrivateProfileString("toeic", "docpath", gstrDocPath, App.Path & "\toeic.ini")
End Sub

解决方案 »

  1.   

    我用如下语句可以新建一个TXT文本文件,但我想定义文本文件的具体内容,请问该如何写程序?Private Sub Form_Load()
        Dim FileNo As Long
        
        FileNo = FreeFile()
        Open "d:\aaa.ini" For Binary Access Write As #FileNo
        
    End Sub
      

  2.   

    Open "d:\aaa.ini" For Output As #FileNo    Print #FileNo, "ABC"
        Print #FileNo, "DEF"Close #FileNo则aaa.ini中的内容为:
    ABC
    DEF如果想追加写入,可以用 Append 方式打开
    不过如果你想写INI文件,最好还是用API,给你一个封装过的函数: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)
    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 LongPublic Function GetINI(FileName As String, SectionName As String, KeyName As String) As String
        Dim intSize As Integer, strValue As String, k As Integer
        intSize = 32767
        strValue = String(32767, " ")
        GetPrivateProfileString SectionName, KeyName, "", strValue, intSize, FileName
        k = InStr(1, strValue, Chr(0))
        strValue = Mid(strValue, 1, k - 1)
        GetINI = strValue
    End FunctionPublic Function SetINI(FileName As String, SectionName As String, KeyName As String, Value As String) As Boolean
        Dim rc As Integer
        rc = WritePrivateProfileString(SectionName, KeyName, Value, FileName)
        If rc = 0 Then SetINI = False Else SetINI = True
    End Function想写入时可以用 
    SetINI(FileName, SectionName, KeyName, Value)
    想读入时可以用
    xxx = GetINI(FileName, SectionName, KeyName)如运行
    SetINI "d:\aaa.ini", "ABCD", "MyKey", "123"
    则文件 d:\aaa.ini 中的内容为:[ABCD]
    MyKey=123若要读取以上内容,可以用:
    xxx = GetINI("d:\aaa.ini", "ABCD", "MyKey")
    则变量 xxx 中的内容为"123"