Solved

Read/write INI files

Posted on 2002-05-26
11
262 Views
Last Modified: 2006-11-17
Hello,

I have an INI file named "Settings.ini" It looks like this:

[Section1]
Key1=dflgkj
Key3=werw
[Section2]
Key1=45
Key2=twrtw
Key3=sdn
Key5=opirwt

I want to read keys from this file, for example: Section2 --> Key3 will return sdn. How can I do this?
I also want to write keys...

Thanks in advance.
0
Comment
Question by:slavikn
11 Comments
 
LVL 4

Expert Comment

by:Monchanger
ID: 7035645
This contains a class which does pretty much everything you could imagine. Nicely documented too.

http://www.freevbcode.com/ShowCode.Asp?ID=306

I've seen a lot of other INI-file code out there on the various free code sites.
0
 
LVL 1

Expert Comment

by:Freekeko
ID: 7036523
You can use the API (GetPrivateProfileString)
if u want code plz tell me.
0
 
LVL 2

Expert Comment

by:priya_pbk
ID: 7036552
hi slavikn,

You can try this link
--How do I read/write INI Files? ..
http://www.visualbasic.org/vbfaq/common.htm#C1

-priya
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 1

Author Comment

by:slavikn
ID: 7037660
Freekeko,
Please give me code. I am new in VB... sorry...

priya_pbk,
Sorry, this is not like working with INI files.

0
 
LVL 1

Expert Comment

by:Freekeko
ID: 7038332
Hi, here it is:

Put this into the form declaration section
---------------------------------------------------
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 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
---------------------------------------------------

and this into the main form code

Private Sub Form_Load()
    'Write the setting to the file (c:\test.ini) under
    '   Project1 -> Keyname
    WritePrivateProfileString App.Title, "KeyName", "This is the value", "c:\test.ini"

    'Create a buffer
    Ret = String(255, 0)
    'Retrieve the string
    NC = GetPrivateProfileString(App.Title, "KeyName", "Default", Ret, 255, "C:\test.ini")

    'NC is the number of characters copied to the buffer
    If NC <> 0 Then Ret = Left$(Ret, NC)
    'Show our string
    MsgBox Ret
    'Delete the file
    Kill "c:\test.ini"
End Sub

hope u find it easy :) plz reply if u have a comment
0
 
LVL 1

Author Comment

by:slavikn
ID: 7040106
Monchanger,
I AM SORRY. I DIDN'T NOTICE YOUR COMMENT YESTERDAY. So I didn't reply to it. Anyway, it is not exactly what I need.

Freekeko,
Sorry, the INI file is written correctly, but when I try to read from it (your example), "Ret" returns an empty string although NC=Len(the value).
Try the code you've given me and see.
Any suggestions?
0
 
LVL 1

Author Comment

by:slavikn
ID: 7040113
Also, how can I delete key/section?
0
 
LVL 1

Author Comment

by:slavikn
ID: 7044446
I INCREASE POINTS TO 120 !!! PLEASE ANSWER !!!
0
 
LVL 1

Author Comment

by:slavikn
ID: 7046742
Nobody answers.
0
 
LVL 27

Expert Comment

by:Asta Cu
ID: 7143847
This question has not cleared, is pending deletion, so you'll need to request a deletion in Community Support to close this.
Asta
0
 
LVL 6

Accepted Solution

by:
Mindphaser earned 0 total points
ID: 7144071
Points refunded and moved to PAQ

** Mindphaser - Community Support Moderator **
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

726 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