如何用VFP读写配置文件(.INI)?

如何用VFP读写配置文件(.INI)?

---------------------------------------------------------------

******读写(读取) INI 文件内容
--------------------------------

以下是一个读取INI文件的例子1*

  • Reading an arbitrary INI file into a cursor
  • 读一个 INI 文件到一个游标
    Local lcFileName
    lcFileName = getfile("ini")
    Create CURSOR csResult (section C(50), keyname C(50),keyvalue C(200))
    lcSections = getSections (lcFileName)
    = str2cursor (lcSections, "csSections")
    Select csSections
    Scan ALL
    Select csSections
    lcKeys = getKeys (lcFileName, csSections.strvalue)
    = str2cursor (lcKeys, "csKeys")
    Select csKeys
    Scan ALL
    lcValue = getKeyValue (lcFileName,csSections.strvalue, csKeys.strvalue)
    Insert INTO csResult VALUES (csSections.strvalue, csKeys.strvalue, lcValue)
    Endscan
    Endscan
    Select csResult
    Go TOP
    Browse NORMAL NOWAIT
    FUNCTION getSections (lcFileName)
    Declare INTEGER GetPrivateProfileString IN kernel32 INTEGER lpAppName,INTEGER lpKeyName,STRING lpDefault,STRING @ lpReturnedString,INTEGER nSize,STRING lpFileName
    Local lnSize, lpReturnedString, lnResult
    lnSize = 4096
    lpReturnedString = Repli (Chr(0), lnSize)
    lnResult = GetPrivateProfileString (0,0,"#empty#",@lpReturnedString,lnSize,lcFileName)
    Return Left (lpReturnedString, lnResult)
    ENDFUNC
    FUNCTION getKeys (lcFileName, lcSection)
    Declare INTEGER GetPrivateProfileString IN kernel32 STRING lpAppName,INTEGER lpKeyName,STRING lpDefault,STRING @ lpReturnedString,INTEGER nSize,STRING lpFileName
    Local lnSize, lpReturnedString, lnResult
    lnSize = 16384
    lpReturnedString = Repli (Chr(0),lnSize)
    lnResult = GetPrivateProfileString (lcSection,0,"#empty#",@lpReturnedString,lnSize,lcFileName)
    Return Left (lpReturnedString, lnResult)
    ENDFUNC
    FUNCTION getKeyValue (lcFileName, lcSection, lcKey)
    Declare INTEGER GetPrivateProfileString IN kernel32 STRING lpAppName,STRING lpKeyName,STRING lpDefault,STRING @ lpReturnedString,INTEGER nSize,STRING lpFileName
    Local lnSize, lpReturnedString, lnResult
    lnSize = 16384
    lpReturnedString = Repli (Chr(0), lnSize)
    lnResult = GetPrivateProfileString (lcSection,lcKey,"#empty#",@lpReturnedString,lnSize,lcFileName)
    Return Left (lpReturnedString, lnResult)
    endfunc
    FUNCTION str2cursor (lcSource, lcCursor)
    Create CURSOR (lcCursor) (strvalue C(250))
    Local ii, ch, ss, lnResult
    ss = ""
    lnResult = 0
    For ii=1 TO Len(lcSource)
    ch = SUBSTR (lcSource, ii,1)
    If ch = Chr(0)
    Insert INTO (lcCursor) VALUES (ss)
    lnResult = lnResult + 1
    ss = ""
    Else
    ss = ss + ch
    Endif
    Endfor
    Return lnResult
    ENDFUNC
    FUNCTION getWindowsDir
    Declare INTEGER GetWindowsDirectory IN kernel32 STRING @lpBuffer,INTEGER nSize
    lpBuffer = SPACE (250)
    nSizeRet = GetWindowsDirectory (@lpBuffer, Len(lpBuffer))
    Return SUBSTR (lpBuffer, 1, nSizeRet)
    ENDFUNC

以下是一个读写INI文件的例子2*

*-- 定义读应用程序 INI 文件的DLL函数
DECLARE INTEGER GetPrivateProfileString IN Win32API AS GetPrivStr ;
String cSection, String cKey, String cDefault, String @cBuffer, ;
Integer nBufferSize, String cINIFile
*-- 定义写应用程序 INI 文件的DLL函数
DECLARE INTEGER WritePrivateProfileString IN Win32API AS WritePrivStr ;
String cSection, String cKey, String cValue, String cINIFile
&& 调用
&& 以下程序将字符串abc=12,24写入当前目录中的formposi.ini文件中的WindowPositions段中:
LOCAL lcValue,lcEntry
lcEntry = "abc"
lcValue = '12,24'
=WritePrivStr("WindowPositions", lcEntry, lcValue, CURDIR() + "formposi.ini")
&& 以下程序从当前目录中的formposi.ini文件中的WindowPositions段中读取以前保存的字符串,并在wait window中显示出来
LOCAL lcBuffer,lcOldError,lnTop,lnLeft,llError,lnCommaPos,lcEntry
lcEntry = 'abc'
lcBuffer = SPACE(10) + CHR(0)
lcOldError = ON('ERROR')
*-- 在INI 文件中读取窗口位置
IF GetPrivStr("WindowPositions", lcEntry, "", @lcBuffer, LEN(lcBuffer), CURDIR() + "formposi.ini") > 0
*-- 如果分解参数时出现错误,
*-- 忽略该串并使用表单的默认值
ON ERROR llError = .T.
lnCommaPos = AT(",", lcBuffer)
lnTop = VAL(LEFT(lcBuffer, lnCommaPos - 1))
lnLeft = VAL(SUBSTR(lcBuffer, lnCommaPos + 1))
ON ERROR &lcOldError
IF !llError
wait window str(lnTop)+chr(13)+str(lnLeft)
ENDIF
ENDIF
CLEAR DLLS


以下是一个读取Win.ini文件的例子3*

  • Reading keys in the specified section of the Win.ini file
  • 从 Win.ini 文件的指定段读取信息
    lcSection = "Mail" && section name
    *lcSection = [DbcsMsgMode]
    *lcSection = [embedding]
    *lcSection = [Extensions]
    lcKeys = getKeysString (lcSection)
    Create CURSOR csWinIni (keyname C(30), keyvalue C(200))
    lcKey = ""
    For ii=1 TO Len(lcKeys)
    ch = SUBSTR (lcKeys, ii,1)
    If ch = Chr(0)
    lcValue = GetKeyValue(lcSection, lcKey)
    Insert INTO csWinIni VALUES (lcKey, lcValue)
    lcKey = ""
    Else
    lcKey = lcKey + ch
    Endif
    Endfor
    Select csWinIni
    Go TOP
    Brow NORMAL NOWAIT
    Function GetKeysString (lcSection)
    Declare INTEGER GetProfileString IN kernel32 STRING lpAppName,INTEGER lpKeyName,STRING lpDefault,STRING @ lpReturnedString,INTEGER nSize
    nSize = 1024
    lpReturnedString = Repli (Chr(0), nSize)
    lnResult = GetProfileString (lcSection, 0,"#<未知>#",@lpReturnedString, nSize)
    Return Left(lpReturnedString, lnResult)
    Endfunc
    Function GetKeyValue (lcSection, lcKey)
    Declare INTEGER GetProfileString IN kernel32 STRING lpAppName,STRING lpKeyName,STRING lpDefault,STRING @ lpReturnedString,INTEGER nSize
    nSize = 4096
    lpReturnedString = Repli (Chr(0), nSize)
    lnResult = GetProfileString (lcSection, lcKey,"#<未知>#",@lpReturnedString, nSize)
    Return Left(lpReturnedString, lnResult)
    Endfunc

--------------------------------------------------------------------

  • Creating INI file and adding strings to it
  • 创建、写 ini 文件
    *-----------------------------------------------------

Do decl

Local lcFilename
lcFilename = "C:\111\测试.ini"
= createFile (lcFilename)

*** Technique 1

  • adding empty sections
    = WritePrivateProfileSection("General","",lcFilename)
    = WritePrivateProfileSection("Language","",lcFilename)
    = WritePrivateProfileSection("Devices","",lcFilename)
    = WritePrivateProfileSection("Uninstall","",lcFilename)
    = WritePrivateProfileSection("Old Brown Shoe","",lcFilename)

*** Technique 2

  • adding key names and associated values to existing sections
    = WritePrivateProfileSection("General","startdir=C:"+Chr(0)+"resolution=high"+Chr(0)+"delay=500"+Chr(0)+"security=default"+Chr(0),lcFilename)
    = WritePrivateProfileSection("Language","Active=English"+Chr(0),lcFilename)
    = WritePrivateProfileSection("Devices","Default=Fork"+Chr(0)+"Active=Spoon"+Chr(0)+"Emergency=Hand"+Chr(0),lcFilename)

*** Technique 3

  • adding new section and a key in one step
    = WritePrivateProfileSection("Environment","Active=Testing"+Chr(0),lcFilename)

*** Technique 4

  • adding new key to a section which exists
    = WritePrivateProfileString("General","datapath","C:\App\Data",lcFilename)

*** Technique 5

  • replacing existing key
    = WritePrivateProfileString("General","startdir","C:\App",lcFilename)

*** Technique 6

  • adding new key to the section, which does not exist
    = WritePrivateProfileString("Very Important Section","Urgent action","Do not care",lcFilename)

PROCEDURE createFile (lcFilename)
IF FILE (lcFilename)
DELETE FILE (lcFilename)
ENDIF
hFile = FCREATE (lcFilename)
= FCLOSE (hFile)
ENDPROC

PROCEDURE decl
DECLARE WritePrivateProfileSection IN kernel32 STRING lpAppName,STRING lpString,STRING lpFileName
Declare WritePrivateProfileString IN kernel32 STRING lpAppName,STRING lpKeyName,STRING lpString,STRING lpFileName
ENDPROC

Published At
Categories with 数据库类
Tagged with
comments powered by Disqus