Solved

ini files in vb6

Posted on 2003-11-13
4
3,301 Views
Last Modified: 2008-09-05
I am currently working on a visual frontend for pisg, a statistics generator for irc.
I would very much like to include multi language support for this so i have decided to use ini files for this.

So my question would be, are there built in functions in vb6 that allow reading and/or modification of ini files? Or is there a FREE activex control/library that allows me to do this ?

I would very much appreciate an answer very quickly.
Thankx very much in advance ;)
0
Comment
Question by:Professor
4 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 9744288
You could use the API's for reading/writing to INI files.  There is an example at the bottom of this page:

http://www.mentalis.org/apilist/GetPrivateProfileString.shtml

Idle_Mind
0
 
LVL 1

Expert Comment

by:robdogg10
ID: 9745729
Create a new class, name it cIniClass and paste this into the class.  If you wish, leave a note here and I'll email you the entire class and the compiled DLL.

Usage is as follows:

dim oIni as cIniAccess
dim sValue as String

set oIni = new cIniClass
with oIni
    .File = "my.ini"
    .Section = "General"
   
    'read value from the Ini file
    sValue = .ReadSection("SomeSetting", "Default Value for this Setting")

    'write value to the Ini file
    .WriteSection "New Value", "SomeSetting"
end with
set oIni = nothing

'******START HERE*********
Option Explicit

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 Long
Private 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
Private mstrFile As String
Private mstrSection As String
Private mstrEntry As String
Private mstrValue As String
Private mstrDefault As String

Public Property Get Default() As String
   Default = mstrDefault
End Property

Public Property Let Default(ByVal strValue As String)
   mstrDefault = strValue
End Property

Public Property Get Value() As String
   Value = mstrValue
End Property

Public Property Let Value(ByVal strValue As String)
   mstrValue = strValue
End Property

Public Property Get Entry() As String
   Entry = mstrEntry
End Property

Public Property Let Entry(ByVal strEntry As String)
   mstrEntry = strEntry
End Property

Public Property Get Section() As String
   Section = mstrSection
End Property

Public Property Let Section(ByVal strSection As String)
   mstrSection = strSection
End Property

Public Property Get File() As String
   File = mstrFile
End Property

Public Property Let File(ByVal strFile As String)
   mstrFile = strFile
End Property
Public Function ReadSection(Optional Entry As Variant, Optional Default As Variant, Optional Section As Variant, Optional File As Variant) As String
    Dim ReturnString As String
    Dim NullPointer As Integer
   
    If Not IsMissing(Entry) Then mstrEntry = Entry
    If Not IsMissing(Section) Then mstrSection = Section
    If Not IsMissing(File) Then mstrFile = File
    If Not IsMissing(Default) Then mstrDefault = Default
   
    ReturnString = String$(255, Chr$(0))
    GetPrivateProfileString mstrSection, mstrEntry, mstrDefault, ReturnString, Len(ReturnString) + 1, mstrFile
    'If left$(ReturnString, InStr(ReturnString, Chr(0)) - 1) = "NotFound" Then
    '    WritePrivateProfileString section$, "SpySample.FileEventSpy", "0", "VB.INI"
    'End If
    NullPointer = InStr(ReturnString, Chr$(0))
    If NullPointer <= 1 Then
        mstrValue = ""
    Else
        mstrValue = Left$(ReturnString, NullPointer - 1)
    End If
    ReadSection = mstrValue
End Function

Public Sub WriteSection(Optional Value As Variant, Optional Entry As Variant, Optional Section As Variant, Optional File As Variant)
    If Not IsMissing(Entry) Then mstrEntry = Entry
    If Not IsMissing(Section) Then mstrSection = Section
    If Not IsMissing(File) Then mstrFile = File
    If Not IsMissing(Value) Then mstrValue = Value

    WritePrivateProfileString mstrSection, mstrEntry, mstrValue, mstrFile
End Sub

Sub FlushIni()
    'forces the memory contents of the ini writing to actually be written to the disk
    WritePrivateProfileString "x", "x", "x", mstrFile
End Sub

Public Sub DeleteSection()
    'Dim retcode As Long
    'retcode = WritePrivateProfileString(Section, 0&, 0&, filename)
    WritePrivateProfileString mstrSection, 0&, 0&, mstrFile
End Sub
'********END HERE********
0
 
LVL 5

Expert Comment

by:burningmace
ID: 9745834
Another thing you can do is add a reference to the Microsoft Scripting Runtime. Then use this kind of thing:


Dim fso as New FileSystemObject, mFile as file, stream as TextStream

Public Sub OpenFile(oFile as String, oReadWrite as IOMODE)
Set mFile = fso.GetFile(oFile)
Set stream = mFile.OpenAsTextStream(oReadWrite)
End Sub

Public Sub CloseFile()
stream.Close
End Sub

Public Sub ReadLinesFromFile() as Collection
Dim tmp as New Collection
start:
tmp.Add(stream.ReadLine)
if stream.AtEndOfStream Then Exit Sub
GoTo start
End Sub

Public Sub AddLineToFile(wText as String)
stream.WriteLine(wText)
End Sub

Private Sub Form_Load()
Dim lines as New Collection  'Used as "variable-size" array here
OpenFile("C:\file.ini", ForReading)
Set lines = ReadLinesFromFile
'You get the idea...
End Sub


Hope it's useful,
Burningmace
0
 
LVL 9

Expert Comment

by:Dang123
ID: 9747658
Professor,
    These are very good references for forking with ini files.

http://www.mvps.org/vbnet/code/file/pprofilebasic.htm
http://www.mvps.org/vbnet/code/file/pprofilesections.htm

Dang123
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sending a email via excel using vba 6 82
Exit a vb6 apps when a calling it apps closes 15 48
Visual Studio 2005 text editor 10 38
Help me. 3 54
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question