powered by simpleCommunicator - 2.0.60     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Visual Basic [игнор отключен] [закрыт для гостей] / Вопрос
5 сообщений из 5, страница 1 из 1
Вопрос
    #32451461
enter
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Подскажите как записывать и доставать функции в файл ini
...
Рейтинг: 0 / 0
Вопрос
    #32451487
johnnybo
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
попробуй (все в модуле):

Option Explicit


Public Declare Function GetPrivateProfileStringByKeyName& Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName$, ByVal lpszKey$, ByVal lpszDefault$, ByVal lpszReturnBuffer$, ByVal cchReturnBuffer&, ByVal lpszFile$)
Public Declare Function GetPrivateProfileStringKeys& Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName$, ByVal lpszKey&, ByVal lpszDefault$, ByVal lpszReturnBuffer$, ByVal cchReturnBuffer&, ByVal lpszFile$)
Public Declare Function GetPrivateProfileStringSections& Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName&, ByVal lpszKey&, ByVal lpszDefault$, ByVal lpszReturnBuffer$, ByVal cchReturnBuffer&, ByVal lpszFile$)
' This first line is the declaration from win32api.txt

Public Declare Function WritePrivateProfileStringByKeyName& Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lplFileName As String)
Public Declare Function WritePrivateProfileStringToDeleteKey& Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As Long, ByVal lplFileName As String)
Public Declare Function WritePrivateProfileStringToDeleteSection& Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Long, ByVal lpString As Long, ByVal lplFileName As String)
Public Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpfilename As String) As Long


Function VBGetPrivateProfileString(section$, key$, File$) As String
Dim KeyValue$
Dim characters As Long


KeyValue$ = String$(128, 0)
characters = GetPrivateProfileStringByKeyName(section$, key$, "", KeyValue$, 127, File$)

If characters > 1 Then
KeyValue$ = left$(KeyValue$, characters)
End If

VBGetPrivateProfileString = KeyValue$

End Function


Public Function GetSectionNames(FileName As String, SectionNames As Variant) As Integer
'GetSectionNames Return Number of Section in file
'SectionNames return all section names

Dim characters As Long
Dim SectionList As String
Dim ArrSection() As String
Dim i As Integer
Dim NullOffset%

SectionList = String$(128, 0)

' Retrieve the list of keys in the section
characters = GetPrivateProfileStringSections(0, 0, "", SectionList, 127, FileName)

' Load sections into Arrey
i = 0
Do
NullOffset% = InStr(SectionList, Chr$(0))
If NullOffset% > 1 Then
ReDim Preserve ArrSection(i)
ArrSection(i) = Mid$(SectionList, 1, NullOffset% - 1)
SectionList$ = Mid$(SectionList, NullOffset% + 1)
i = i + 1
End If
Loop While NullOffset% > 1
GetSectionNames = i - 1
SectionNames = ArrSection


End Function

Public Function GetKeyNames(SectionName As String, FileName As String, KeyNames As Variant) As Integer
'GetKeyNames Return Number of key in section
'KeyNames Return list of keyNames in section

Dim characters As Long
Dim KeyList As String
Dim ArrKey() As String
Dim i As Integer

KeyList = String$(128, 0)
' Retrieve the list of keys in the section

characters = GetPrivateProfileStringKeys(SectionName, 0, "", KeyList, 127, FileName)

' Load Keys into Arrey
Dim NullOffset%
i = 0
Do
NullOffset% = InStr(KeyList, Chr$(0))
If NullOffset% > 1 Then
ReDim Preserve ArrKey(i)
ArrKey(i) = Mid$(KeyList, 1, NullOffset% - 1)
KeyList$ = Mid$(KeyList, NullOffset% + 1)
i = i + 1
End If
Loop While NullOffset% > 1
GetKeyNames = i - 1
KeyNames = ArrKey
End Function

Public Function DeleteKey(KeyName As String, SectionName As String, FileName As String) As Long
'Return 0 if Deletion not sucsesful

' Delete the selected key
DeleteKey = WritePrivateProfileStringToDeleteKey(SectionName, KeyName, 0, FileName$)


End Function

Public Function WriteKey(SectionName As String, KeyName As String, KeyValue As String, FileName As String) As Long
If Len(KeyValue) = 0 Then KeyValue = " "
WriteKey = WritePrivateProfileStringByKeyName(SectionName, KeyName, KeyValue, FileName)
End Function

Public Function WriteSection(SectionName As String, FileName As String) As Long
WriteSection = WritePrivateProfileSection(SectionName, "", FileName)
End Function


Public Function DeleteSection(SectionName, FileName) As Long
DeleteSection = WritePrivateProfileStringToDeleteSection(SectionName, 0&, 0&, FileName)
End Function
...
Рейтинг: 0 / 0
Вопрос
    #32452019
мадама
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
А я по-простому делаю. Так я читаю.

Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
Dim sIniFileName As String
        
 sIniFileName = App.Path &  "\abc.ini" 
 
 Open sIniFileName For Input As # 1 
   Line Input # 1 , MyDir
   Line Input # 1 , MyFile
   Line Input # 1 , MySecuredFile
 Close # 1 
...
Рейтинг: 0 / 0
Вопрос
    #32452117
Фотография Magnus23
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Подерживаю johnnybo . Сам пользуюсь подобным модулем.
Уже вылизан. Если нужно - могу сбросить на мыло.

2 мадама
Просто и со вкусом :)
А если в файле сотни секций и тысячи ключей? Все перебирать? :)

Я уж лучше по-сложному :)




Magnus
...
Рейтинг: 0 / 0
Вопрос
    #32452374
marvan
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Есть такая библиотека осходников, под названием Source+
В ней чудный класс для работы с ini.
Гораздо удобнее чем модуль.

'*****************************************************************************************
'* Class : CIniFile
'* Description : Class for working with initialization (*.ini) file.
'* Notes : Use this class to give your application the capability to store and
'* retrieve its settings to and from an initialization file.
'* For advanced use, please see the Source+ Library online documentation
'* in the Source+ Explorer (the "Documentation" tab).
'*****************************************************************************************

Option Explicit

' Error handling definitions
Private Const E_ERR_BASE = 17680 + vbObjectError
Public Enum EErrIniFile
eErrIniFile_InvalidFileName = E_ERR_BASE + 1
eErrIniFile_FileNotFound
eErrIniFile_NotInitialized
eErrIniFile_InvalidSection
eErrIniFile_InvalidKey
eErrIniFile_ComponentFailure
End Enum
Private Const S_ERR_InvalidFileName = "Invalid file name"
Private Const S_ERR_FileNotFound = "File not found"
Private Const S_ERR_NotInitialized = "Object not properly initialized"
Private Const S_ERR_InvalidSection = "Invalid section"
Private Const S_ERR_InvalidKey = "Invalid key"
Private Const S_ERR_ComponentFailure = "CIniFile component failure"

' Private class constants
Private Const MAX_LENGTH = 1024
Private Const INVALID_HANDLE_VALUE = -1
Private Const MAX_LENGTH_EX = 512

' Private class type definitions
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_LENGTH_EX
cAlternate As String * 14
End Type

' Private class API function declarations
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As Any, ByVal lpKeyName As Any, ByVal lpDefault As Any, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private 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

' Private variables for internal class use
Private m_lError As Long
Private m_sKey As String
Private m_sSection As String
Private m_sDefault As String

' Private variables to hold property values
Private m_sFile As String


'*****************************************************************************************
'* Sub : Init
'* Notes : Use this routine for basic object initialization.
'*****************************************************************************************
Public Sub Init(FileName As String)
On Error GoTo hComponentFailure

If Len(FileName) = 0 Then
On Error GoTo 0
Err.Raise eErrIniFile_InvalidFileName, App.EXEName & ".CIniFile", S_ERR_InvalidFileName
End If

m_sFile = FileName

Exit Sub

hComponentFailure:
Err.Raise eErrIniFile_ComponentFailure, App.EXEName & ".CIniFile", S_ERR_ComponentFailure
End Sub


'*****************************************************************************************
'* Property : FileName
'* Notes : Returns or sets the name of the initialization (*.ini) file.
'*****************************************************************************************
Public Property Let FileName(Value As String)
On Error GoTo hComponentFailure

If Len(Value) = 0 Then
On Error GoTo 0
Err.Raise eErrIniFile_InvalidFileName, App.EXEName & ".CIniFile", S_ERR_InvalidFileName
End If

m_sFile = Value

Exit Property

hComponentFailure:
Err.Raise eErrIniFile_ComponentFailure, App.EXEName & ".CIniFile", S_ERR_ComponentFailure
End Property

Public Property Get FileName() As String
On Error GoTo hComponentFailure

FileName = m_sFile

Exit Property

hComponentFailure:
Err.Raise eErrIniFile_ComponentFailure, App.EXEName & ".CIniFile", S_ERR_ComponentFailure
End Property


'*****************************************************************************************
'* Function : DeleteKey
'* Notes : Deletes a key setting from a specified section of the initialization file.
'* Returns true if the key was deleted and false otherwise.
'*****************************************************************************************
Public Function DeleteKey(ByVal Section As String, ByRef Key As String) As Boolean
On Error GoTo hComponentFailure

Dim iRet As Long

If Len(m_sFile) = 0 Then
On Error GoTo 0
Err.Raise eErrIniFile_NotInitialized, App.EXEName & ".CIniFile", S_ERR_NotInitialized
End If

If Not FileExists(m_sFile) Then
On Error GoTo 0
Err.Raise eErrIniFile_FileNotFound, App.EXEName & ".CIniFile", S_ERR_FileNotFound
End If

If Len(Section) = 0 Then
On Error GoTo 0
Err.Raise eErrIniFile_InvalidSection, App.EXEName & ".CIniFile", S_ERR_InvalidSection
End If

If Len(Key) = 0 Then
On Error GoTo 0
Err.Raise eErrIniFile_InvalidKey, App.EXEName & ".CIniFile", S_ERR_InvalidKey
End If

iRet = WritePrivateProfileString(Section, Key, 0&, m_sFile)
WritePrivateProfileString vbNullString, vbNullString, vbNullString, m_sFile
DeleteKey = (iRet <> 0)

Exit Function

hComponentFailure:
Err.Raise eErrIniFile_ComponentFailure, App.EXEName & ".CIniFile", S_ERR_ComponentFailure
End Function


'*****************************************************************************************
'* Function : DeleteSection
'* Notes : Deletes a section of the initialization file.
'* Returns true if the section was deleted and false otherwise.
'*****************************************************************************************
Public Function DeleteSection(ByVal Section As String) As Boolean
On Error GoTo hComponentFailure

Dim iRet As Long

If Len(m_sFile) = 0 Then
On Error GoTo 0
Err.Raise eErrIniFile_NotInitialized, App.EXEName & ".CIniFile", S_ERR_NotInitialized
End If

If Not FileExists(m_sFile) Then
On Error GoTo 0
Err.Raise eErrIniFile_FileNotFound, App.EXEName & ".CIniFile", S_ERR_FileNotFound
End If

If Len(Section) = 0 Then
On Error GoTo 0
Err.Raise eErrIniFile_InvalidSection, App.EXEName & ".CIniFile", S_ERR_InvalidSection
End If

iRet = WritePrivateProfileString(Section, 0&, 0&, m_sFile)
DeleteSection = (iRet <> 0)
WritePrivateProfileString vbNullString, vbNullString, vbNullString, m_sFile

Exit Function

hComponentFailure:
Err.Raise eErrIniFile_ComponentFailure, App.EXEName & ".CIniFile", S_ERR_ComponentFailure
End Function


'*****************************************************************************************
'* Function : EnumerateAllKeys
'* Notes : Retrieves all the keys (in a an array of strings) belonging to a section
'* of an initialization file.
'* Returns the number of retrieved keys.
'*****************************************************************************************
Public Function EnumerateAllKeys(Section As String, ByRef Key() As String) As Long
On Error GoTo hComponentFailure

Dim lApi_Ret As Long
Dim lCount As Long
Dim lNextPos As Long
Dim lPos As Long
Dim lSize As Long
Dim buffer As String
Dim sCur As String
Dim sSections As String

If Len(m_sFile) = 0 Then
On Error GoTo 0
Err.Raise eErrIniFile_NotInitialized, App.EXEName & ".CIniFile", S_ERR_NotInitialized
End If

If Not FileExists(m_sFile) Then
On Error GoTo 0
Err.Raise eErrIniFile_FileNotFound, App.EXEName & ".CIniFile", S_ERR_FileNotFound
End If

If Len(Section) = 0 Then
On Error GoTo 0
Err.Raise eErrIniFile_InvalidSection, App.EXEName & ".CIniFile", S_ERR_InvalidSection
End If

lCount = 0
Erase Key

If (Len(Section) > 0) Then

buffer = Space$(8192)
lSize = Len(buffer)
lApi_Ret = GetPrivateProfileString(Section, 0&, m_sDefault, buffer, lSize, m_sFile)
If (lSize > 0) Then
sSections = Left$(buffer, lApi_Ret)
Else
sSections = ""
End If

lPos = 1
lNextPos = InStr(lPos, sSections, Chr$(0))

Do While lNextPos <> 0
sCur = Mid$(sSections, lPos, (lNextPos - lPos))

If (sCur <> Chr$(0)) Then
lCount = lCount + 1

ReDim Preserve Key(1 To lCount) As String
Key(lCount) = Mid$(sSections, lPos, (lNextPos - lPos))

lPos = lNextPos + 1
lNextPos = InStr(lPos, sSections, Chr$(0))
End If

Loop

End If

EnumerateAllKeys = lCount

Exit Function

hComponentFailure:
Err.Raise eErrIniFile_ComponentFailure, App.EXEName & ".CIniFile", S_ERR_ComponentFailure
End Function


'*****************************************************************************************
'* Sub : EnumerateAllSections
'* Notes : Retrieves all the sections belonging to an initialization file.
'* Returns the number of retirved sections.
'*****************************************************************************************
Public Function EnumerateAllSections(ByRef Sections() As String) As Long
On Error GoTo hComponentFailure

Dim lApi_Ret As Long
Dim lCount As Long
Dim lNextPos As Long
Dim lPos As Long
Dim lSize As Long
Dim sCur As String
Dim buffer As String
Dim sSections As String

If Len(m_sFile) = 0 Then
On Error GoTo 0
Err.Raise eErrIniFile_NotInitialized, App.EXEName & ".CIniFile", S_ERR_NotInitialized
End If

If Not FileExists(m_sFile) Then
On Error GoTo 0
Err.Raise eErrIniFile_FileNotFound, App.EXEName & ".CIniFile", S_ERR_FileNotFound
End If

lCount = 0
Erase Sections

If (Len(m_sFile) > 0) Then

buffer = Space$(8192)
lSize = Len(buffer)
lApi_Ret = GetPrivateProfileString(0&, 0&, m_sDefault, buffer, lSize, m_sFile)
If (lSize > 0) Then
sSections = Left$(buffer, lApi_Ret)
Else
sSections = ""
End If

lPos = 1
lNextPos = InStr(lPos, sSections, Chr$(0))

Do While lNextPos <> 0

If (lNextPos <> lPos) Then
sCur = Mid$(sSections, lPos, (lNextPos - lPos))
lCount = lCount + 1

ReDim Preserve Sections(1 To lCount) As String
Sections(lCount) = sCur
End If

lPos = lNextPos + 1
lNextPos = InStr(lPos, sSections, Chr$(0))

Loop

End If

EnumerateAllSections = lCount

Exit Function

hComponentFailure:
Err.Raise eErrIniFile_ComponentFailure, App.EXEName & ".CIniFile", S_ERR_ComponentFailure
End Function


'*****************************************************************************************
'* Function : GetNumericValue
'* Notes : Returns an integer value containing the key setting stored in the
'* specified section of an initialization file.
'*****************************************************************************************
Public Function GetNumericValue(Section As String, Key As String) As Integer
On Error GoTo hComponentFailure

If Len(m_sFile) = 0 Then
On Error GoTo 0
Err.Raise eErrIniFile_NotInitialized, App.EXEName & ".CIniFile", S_ERR_NotInitialized
End If

If Not FileExists(m_sFile) Then
On Error GoTo 0
Err.Raise eErrIniFile_FileNotFound, App.EXEName & ".CIniFile", S_ERR_FileNotFound
End If

GetNumericValue = GetPrivateProfileInt(Section, Key, -1, m_sFile)

Exit Function

hComponentFailure:
Err.Raise eErrIniFile_ComponentFailure, App.EXEName & ".CIniFile", S_ERR_ComponentFailure
End Function


'*****************************************************************************************
'* Function : GetStringValue
'* Notes : Returns a string value containing the key setting stored in the specified
'* section of an initialization file.
'*****************************************************************************************
Public Function GetStringValue(Section As String, Key As String) As String
On Error GoTo hComponentFailure

Dim lResult As Long
Dim sText As String

If Len(m_sFile) = 0 Then
On Error GoTo 0
Err.Raise eErrIniFile_NotInitialized, App.EXEName & ".CIniFile", S_ERR_NotInitialized
End If

If Not FileExists(m_sFile) Then
On Error GoTo 0
Err.Raise eErrIniFile_FileNotFound, App.EXEName & ".CIniFile", S_ERR_FileNotFound
End If

sText = String$(255, 0)
lResult = GetPrivateProfileString(Section, Key, "", sText, Len(sText), m_sFile)

If lResult = 0 Then
GetStringValue = ""
Else
GetStringValue = Left$(sText, InStr(sText, Chr$(0)) - 1)
End If

Exit Function

hComponentFailure:
Err.Raise eErrIniFile_ComponentFailure, App.EXEName & ".CIniFile", S_ERR_ComponentFailure
End Function


'*****************************************************************************************
'* Sub : SaveNumericValue
'* Notes : Saves a key with a numeric value in the specified section of an
'* initialization file.
'*****************************************************************************************
Public Sub SaveNumericValue(Section As String, Key As String, iData As Integer)
On Error GoTo hComponentFailure

Dim sData As String

If Len(m_sFile) = 0 Then
On Error GoTo 0
Err.Raise eErrIniFile_NotInitialized, App.EXEName & ".CIniFile", S_ERR_NotInitialized
End If

If Len(Section) = 0 Then
On Error GoTo 0
Err.Raise eErrIniFile_InvalidSection, App.EXEName & ".CIniFile", S_ERR_InvalidSection
End If

If Len(Key) = 0 Then
On Error GoTo 0
Err.Raise eErrIniFile_InvalidKey, App.EXEName & ".CIniFile", S_ERR_InvalidKey
End If

sData = iData
WritePrivateProfileString Section, Key, sData, m_sFile
WritePrivateProfileString vbNullString, vbNullString, vbNullString, m_sFile

Exit Sub

hComponentFailure:
Err.Raise eErrIniFile_ComponentFailure, App.EXEName & ".CIniFile", S_ERR_ComponentFailure
End Sub


'*****************************************************************************************
'* Sub : SaveStringValue
'* Notes : Saves a key containing a string value in the specified section of an
'* initialization file.
'*****************************************************************************************
Public Sub SaveStringValue(Section As String, Key As String, sText As String)
On Error GoTo hComponentFailure

If Len(m_sFile) = 0 Then
On Error GoTo 0
Err.Raise eErrIniFile_NotInitialized, App.EXEName & ".CIniFile", S_ERR_NotInitialized
End If

If Len(Section) = 0 Then
On Error GoTo 0
Err.Raise eErrIniFile_InvalidSection, App.EXEName & ".CIniFile", S_ERR_InvalidSection
End If

If Len(Key) = 0 Then
On Error GoTo 0
Err.Raise eErrIniFile_InvalidKey, App.EXEName & ".CIniFile", S_ERR_InvalidKey
End If

WritePrivateProfileString Section, Key, sText, m_sFile
WritePrivateProfileString vbNullString, vbNullString, vbNullString, m_sFile

Exit Sub

hComponentFailure:
Err.Raise eErrIniFile_ComponentFailure, App.EXEName & ".CIniFile", S_ERR_ComponentFailure
End Sub


'*****************************************************************************************
'* Function : FileExists
'* Notes : API-based routine to check for a file's existence.
'* Returns True if the specified file exists, False otherwise.
'*****************************************************************************************
Private Function FileExists(FileName As String) As Boolean
On Error GoTo hComponentFailure

Dim wfd As WIN32_FIND_DATA
Dim hFile As Long

FileExists = False

hFile = FindFirstFile(FileName, wfd)
If hFile <> INVALID_HANDLE_VALUE Then
FileExists = True

If FindClose(hFile) = 0 Then
On Error GoTo 0
Err.Raise eErrIniFile_ComponentFailure, App.EXEName & ".CIniFile", S_ERR_ComponentFailure
End If
End If

Exit Function

hComponentFailure:
Err.Raise eErrIniFile_ComponentFailure, App.EXEName & ".CIniFile", S_ERR_ComponentFailure
End Function


'*****************************************************************************************
'* Sub : Class_Initialize
'* Notes : Class data space initialization.
'*****************************************************************************************
Private Sub Class_Initialize()
On Error GoTo hComponentFailure

m_sFile = App.Path & "\MyFileSettings.ini"

Exit Sub

hComponentFailure:
Err.Raise eErrIniFile_ComponentFailure, App.EXEName & ".CIniFile", S_ERR_ComponentFailure
End Sub
...
Рейтинг: 0 / 0
5 сообщений из 5, страница 1 из 1
Форумы / Visual Basic [игнор отключен] [закрыт для гостей] / Вопрос
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]