VB several useful functions

xiaoxiao2021-03-06  80

Note: -------- (1) ------------ Note: Get all the key values ​​below in a section in the specified INI file, and you need the following API declaration notes: 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 Long Description: returns a string array NOTE: call: Notes: Dim Arrclass () AS STRING Note: Arrclass = GetInfosection ("Class", "D: /Type.ini")

Public Function GetInfoSection (strSection As String, strIniFile As String) As String () Dim strReturn As String * 32767Dim strTmp As StringDim nStart As Integer, nEnd As Integer, i As IntegerDim sArray () As String

Call getPrivateProfileSecion (StRSECTION, STRETURN, LEN (STRRETURN), STRINIFILE

strTmp = strReturni = 1Do While strTmp <> "" nStart = nEnd 1nEnd = InStr (nStart, strReturn, vbNullChar) strTmp = Mid $ (strReturn, nStart, nEnd - nStart) If Len (strTmp)> 0 ThenReDim Preserve sArray (1 TO I) SARRAY (i) = strmpi = i 1END IF

LoopGetInfosection = SarrayEnd Function

Note: -------- (2) ------------ Note: Role: Remove the first tail space in the string, all invalid character comments: Test cases: DIM STRRES AS STRING Note: DIM STRSOUR AS STRING Note: Note: strsour = "& VBnullchar & Vbnullcha": strres = zqtrim (strsour) Note: msgbox "Length =" & len (strsour) & "value = 111 "& strRes &" 222 "Public Function zqTrim (ByVal strSour As String) As StringDim strTmp As StringDim nLen As IntegerDim i As Integer, j As IntegerDim strNow As String, strValid () As String, strNew As String Note: strNow current character Notes : STRVALID Active Character Note: Strnew Last Generated New Character STRTMP = TRIM $ (STRSOUR) NLEN = LEN (STRTMP) if Nlen <1 Tenzqtrim = "" EXIT functionend ifj = 0for i = 1 to NLENSTRNOW = MID (strtmp, i, 1) Note: Each time you read: msgbox ASC (Strnow) if strnow <> vbnullchar and asc (strnow) <> 9 TEN Note: If it is valid, save the valid array Redim Preserve Strvalid (J) Strvalid (J) ) = strnowj = j 1END IF

Next I

Strnew = Join (Strvalid, "") Note: Connect all valid characters ZQTRIM = TRIM $ (STRNEW) Note: Remove the first tail space End Function in the string

Note: -------- (3) ------------ Note: Check if the file exists, there is returning true, otherwise returns falsepublic function checkfileexist (Strfile As String) as Boolean

IF DIR (Strfile, Vbdirectory <> "" "" "" "" ""

Note: -------- (4) ------------ Note: Get the key value of a subkey next to a section in the specified INI file, you need the following API declaration notes: Public Declare Function GetPrivateProfileString Lib "kernel32" Alias ​​_ comment: "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _ Notes: ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString _ Notes: As String, ByVal nSize As Long, ByVal lpFileName As String) AS Long Note: Returns a string Note: Calling example: Note: DIM STRUN AS STRING Note: Strrun = GetInivalue ("Windows", "Run", "C: /Windows/win.ini") Public Function GetInivalue BYVAL LPKEYNAME AS STRING, BYVAL STRNAME AS STRING, BYVAL STRINIFILE AS STRING AS STRINGDIM STRTMP AS STRING * 255

Call getPrivateProfileString (LpKeyName, Strname, ", _STRTMP, LEN (STRTMP), STRINIFILE) GetInetiValue = Left $ (Strtmp, Instr (strtmp, vbnullchar) - 1)

END FUNCTION

Note: -------- (5) ------------ Note: Get the Windows directory, you need the following API declaration Note: Private Declare Function getWindowsDirectory lib "kernel32" Alias ​​"getWindowsDirectorya" (BYVAL LPBUFFER AS STRING, BYVAL NSIZE AS LONG) AS Long Note: Return a string, such as "C: / Windows", "C: / Winnt" comment: call example: Note: DIM STRWINDIR AS STRING Note: Strwindir = getWindir () Private function getWindir () DIM WINDIR AS STRING * 100Call getWindowsDirectory (Windir, 100) getwindir = left $ (WINDIR, INSTR (WINDIR, VBNULLCHAR) - 1)

END FUNCTION

Note: -------- (6) ------------ Note: Get the Windows System Directory, you need the following API declaration Note: Private Declare function getSystemDirectory Lib "kernel32" Alias ​​"getSystemDirectorya (Byval LpBuffer AS String, Byval NSize As Long) AS Long Note: Return a string, such as "C: / Windows / System", "C: / Winnt / System32" comment: call example: Note: DIM strsysdir as string Notes: strSysDir = GetSystemDir () Private Function GetSystemDir () Dim strSysDir As String * 100Call GetSystemDirectory (strSysDir, 100) GetSystemDir = Left $ (strSysDir, InStr (strSysDir, vbNullChar) - 1) End Function

转载请注明原文地址:https://www.9cbs.com/read-121908.html

New Post(0)