?
Solved

INI"S

Posted on 1999-07-06
15
Medium Priority
?
416 Views
Last Modified: 2010-05-02
is there anyway i can make a visual basic prog edit one entry in an ini file
eg
wavdir=C:\wav\
avidir=c:\sumthin\
and not change any other settings like this

wavdir=c:\wav\
avidir=c:\sumthinelse\
0
Comment
Question by:gthgb
[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
  • 5
  • 5
  • 5
15 Comments
 
LVL 1

Expert Comment

by:Jaysin28
ID: 1522255
Yes you can change only one setting in an INI file.  I have a ready made module that will do this for you if you want.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1522256
You can achieve this with a few API calls. The ones that are used most are:

'To retrieve a value from an .INI file:
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

'To write a value to an .INI file:
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
0
 

Author Comment

by:gthgb
ID: 1522257
yeah ill take the module

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:caraf_g
ID: 1522258
You'll have to reject my answer then to allow Jaysin28 to lock the question.
0
 
LVL 1

Expert Comment

by:Jaysin28
ID: 1522259
You have to reject the other answer first so I can Answer
0
 

Author Comment

by:gthgb
ID: 1522260
ok done

0
 
LVL 1

Accepted Solution

by:
Jaysin28 earned 400 total points
ID: 1522261
Paste this code into a new module.

These functions will do what you want if you need any further help please post.

Option Explicit

Public Declare Function GetPrivateProfileString Lib "kernel32" _
   Alias "GetPrivateProfileStringA" (ByVal sSection As String, _
   ByVal sParameter As String, ByVal sDefault As String, _
   ByVal sReturnedString As String, ByVal nSize As Long, _
   ByVal sFileName As String) As Long

Public Declare Function WritePrivateProfileString Lib "kernel32" _
   Alias "WritePrivateProfileStringA" (ByVal sSection As String, _
   ByVal sParameter As String, ByVal sValue As String, _
   ByVal sFileName As String) As Long

Const sErrFileDontExist = "INI File Not Found"
Const sErrSectionDontExist = "INI Section Not Found"
Public sPath As String

Public Function INIFileExists(sFileName As String) As Boolean
' This function will check to see if sFileName exists.
'
' Returns:
'  True  - sFileName Exists
'  False - sFileName does Not Exist
'
' History:
'  02-19-99 JJJ Created.

Dim bresult As Boolean
Dim sDestFile As String
Dim sDestFileExt() As String
Dim iFileBegin As Integer
Dim bFnrtn As Boolean
Dim i As Integer
   
   bFnrtn = True              ' Assume File Exists
   ' Check to see if sFileName is a valid INI file and that it exists.
   If Dir(sFileName) = "" Then
      bFnrtn = False
      Err.Description = sErrFileDontExist
      GoTo MyExit
   End If


MyExit:
   INIFileExists = bFnrtn
   Exit Function

ErrorHandler:
   bFnrtn = False
   Resume MyExit

End Function


Public Function CreateTempINIFileName(sFileName As String) As String
'  This sub creates a temporary file name associated with sFileName
'  the filename that is created is the original filename with an
'  extension that is relative to the number of the files with the same
'  prefix.
'
'
' Returns:
'  String - FileName that was created
'           (if unsuccessful then the loop will keep going)
'           (until it finds a fileName.)
'
' History:
'  02-19-99 JJJ Created.


On Error GoTo ErrorHandler

Dim i As Integer
Dim bresult As Boolean
Dim sDestFile As String
Dim sDestFileExt() As String
Dim iFileBegin As Integer

   ' Initialize the integer couinter that determines the filename extension
   i = 1
   iFileBegin = InStrRev(sFileName, "\")
   If iFileBegin = 0 Then
      ' We didn't find a '\', return empty string.
      sPath = ""
   Else
      sPath = Left(sFileName, iFileBegin)
   End If
   ' Assign the Destination file a name.
   sDestFile = Mid(sFileName, iFileBegin + 1, Len(sFileName))
   sDestFileExt = Split(sDestFile, ".")
   sDestFile = sDestFileExt(0) & ".in" & Trim(Str(i))
   
   ' If the assigned name exists then create a different name.
   Do Until bresult = True
      sDestFile = sPath & sDestFile
      sDestFile = Trim(sDestFile)
      If Dir(sDestFile) = "" Then
         Open sDestFile For Output Access Write As #2
         bresult = True
      Else
         i = i + 1
         sDestFile = sDestFileExt(0) & ".in" & Trim(Str(i))
         bresult = False
      End If
   Loop



MyExit:

   CreateTempINIFileName = sDestFile
   Exit Function

ErrorHandler:

   CreateTempINIFileName = sDestFile
   Resume MyExit

End Function

Public Function RemoveINISection(sSection As String, sFileName As String) As Boolean
' This function will remove sSection from sFileName of an INI File.
'
'
' Returns:
'  True  - Section was removed
'  Fasle - Section was not removed (Error Occurred)
'
' History:
'  02-19-99 JJJ Created.

On Error GoTo ErrorHandler


Dim bFnrtn As Boolean
Dim bNextSection As Boolean
Dim sDestFile As String
Dim NextLine As String
Dim iSectionLoc As Long

   bNextSection = True
   bFnrtn = True                       'Assume Success
   If INIFileExists(sFileName) Then
      ' Open file for READING only
      Open sFileName For Input Access Read As #1
   Else
      ' Otherwise set the Err.Descriptoin to 'INI NOT Found'
      Err.Description = sErrFileDontExist
      bFnrtn = True
      GoTo MyExit
   End If
   ' Call CreatetTempINIFileName to adopt a new Filename
   sDestFile = CreateTempINIFileName(sFileName)
   
   Do Until EOF(1)
      ' Get line of Information from SOURCE File
      Input #1, NextLine
      ' Check to see if the Section matches the sSection we want to delete
      iSectionLoc = InStr(1, NextLine, "[" & sSection & "]", vbTextCompare)
      ' If so then we have to find out where the next section
      ' is in the INI file.
      If iSectionLoc > 0 Then
         bNextSection = False
         ' While bNextSection is False we are in the section that we want to delete.
         ' So ignore the lines that are being read and keep going.
         Do While bNextSection = False
            If EOF(1) = False Then
               Input #1, NextLine
               ' Check for next Section
               iSectionLoc = InStr(1, NextLine, "[", vbTextCompare)
               ' If we have found a '[' then possibly we have found another
               ' section let's investigate further.
               If iSectionLoc > 0 Then
                  ' If we find a ']' then we definitely know we have
                  ' to start writing the lines to the INI files again.  We
                  ' are in a section that we don't want to delete.
                  iSectionLoc = InStr(1, NextLine, "]", vbTextCompare)
                  If iSectionLoc > 0 Then
                     bNextSection = True
                  End If
               End If
            Else
               bNextSection = True
            End If
         Loop
      End If
      Print #2, NextLine
   Loop
   
MyExit:
   RemoveINISection = bFnrtn
   ' Close the Source and destination Files
   Close
   ' Kill the Original INI File
   Kill (sFileName)
   ' Rename the Temp INI to the ORiginal Filename
   Name sDestFile As sFileName
   Exit Function

ErrorHandler:
   
   bFnrtn = False
   Resume MyExit

End Function


Public Function ReadIniFile(sSection As String, sParm As String, _
   sDefault As String, sIniFile As String) As String
' Read the INI file specified by 'sIniFile' for the requested
' 'sSection' and 'sParm'.  If successful, return a string that
' is the 'value' of 'sParm', otherwise, return an empty string "".

On Error GoTo ErrorHandler

Dim iLen As Integer              ' Length of string returned.
Dim sValue As String
Dim sRtnStr As String            ' What THIS function returns.

   sRtnStr = ""

   ' This variable must be large enough to hold the return string
   ' from the GetPrivateProfileString API.
   sValue = String(255, 0)

   ' Get requested parameter string from the INI file.
   iLen = GetPrivateProfileString(sSection, sParm, sDefault, sValue, _
               Len(sValue), sIniFile)
   If iLen <> 0 Then
      sRtnStr = Left(sValue, iLen)
   End If

MyExit:
   
   ReadIniFile = sRtnStr
   Exit Function

ErrorHandler:
   
   MsgBox Err.Description
   sRtnStr = ""
   Resume MyExit

End Function 'ReadIniFile'


Function ReadIniFileBool(sSection As String, sParm As String, bDefault As Boolean, _
   sIniFile As String) As Boolean
' Read the INI file specified by 'sIniFile' for the requested
' 'sSection' and 'sParm'.
'
' If successful, evaluate the returned string as a boolean and return True or
' False as appropriate.

On Error GoTo ErrorHandler

Dim bRtn As Boolean
Dim sBool As String
Dim lBool As Long

' Get the value from the INI file.
sBool = ReadIniFile(sSection, sParm, CStr(bDefault), sIniFile)

sBool = UCase(sBool)                   ' Convert to upper case.
lBool = Val(sBool)                     ' In case of numeric value.

' Ensure that bRtn is either True or False.
If Left(sBool, 1) = "T" Then
   ' Handle T/True.
   bRtn = True
ElseIf Left(sBool, 1) = "F" Then
   ' Handle F/False.
   bRtn = False
ElseIf lBool <> 0 Then
   ' Handle a non-zero numeric value.
   bRtn = True
Else
   ' Assume a zero numeric value.
   bRtn = False
End If

MyExit:

   ReadIniFileBool = bRtn
   Exit Function

ErrorHandler:

   MsgBox Err.Description
   Resume MyExit

End Function 'ReadIniFileBool'


Function WriteIniFile(sSection As String, sParm As String, ByVal sValue As String, _
   sIniFile As String) As Boolean
' Write the specified 'sValue' to the 'sSection' and 'sParm' in the
' INI file specified by 'sIniFile'.
'
' Returns:
'  True  = Successful
'  False = Failure
   
On Error GoTo ErrorHandler

Dim bRtn As Boolean
Dim lRtn As Long
   
   bRtn = True                            ' Assume success.

   lRtn = WritePrivateProfileString(sSection, sParm, sValue, sIniFile)
   If lRtn = 0 Then
      bRtn = False
   End If

MyExit:
   
   WriteIniFile = bRtn
   Exit Function

ErrorHandler:
   
   MsgBox Err.Description
   bRtn = False
   Resume MyExit

End Function 'WriteIniFile'



0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1522262
:-)
0
 
LVL 1

Expert Comment

by:Jaysin28
ID: 1522263
caraf_g,
Thanks, I tried to figure out the problem with the tooltips...  It's weird how it is working for one and not the other...

Jaysin
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1522264
It's very strange alright. I don't know why this is happening. Let's hope someone will come up with a solution!
0
 

Author Comment

by:gthgb
ID: 1522265
holly poop on a stick ok ill try this and see wot happens

0
 

Author Comment

by:gthgb
ID: 1522266
ummm will this work on vb5  im having trouble (i dun know much code so help me out here)
error

Public Declare Function GetPrivateProfileString Lib "kernel32" _
   Alias "GetPrivateProfileStringA" (ByVal sSection As String, _
   ByVal sParameter As String, ByVal sDefault As String, _
   ByVal sReturnedString As String, ByVal nSize As Long, _
   ByVal sFileName As String) As Long

says that only comments may appear after end sub end fuction or end property
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1522267
You have to stick it in the General declarations section before any procedures...
0
 
LVL 1

Expert Comment

by:Jaysin28
ID: 1522268
Add a new module in your VB project and paste the code directly into it.   You can read the INI file using ReadINIFile.  sSection is the Section you want to read (i.e.  [GENERAL] ), sParm is the parameter in sSection that you want read ( i.e. Load = ), bDefault is any default you value you want the function to return if the sParm don't exist, and sIniFile is the INI filename.
0
 

Author Comment

by:gthgb
ID: 1522269
ok illtry that then
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Suggested Courses

719 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