Solved

Read/write INI files

Posted on 2002-05-26
11
259 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now