Solved

Read/write INI files

Posted on 2002-05-26
11
257 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

760 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

17 Experts available now in Live!

Get 1:1 Help Now