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
Solved

Read/write INI files

Posted on 2002-05-26
11
261 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

856 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