Solved

Read/write INI files

Posted on 2002-05-26
11
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…

724 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