INI"S

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\
gthgbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaysin28Commented:
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
caraf_gCommented:
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
gthgbAuthor Commented:
yeah ill take the module

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

caraf_gCommented:
You'll have to reject my answer then to allow Jaysin28 to lock the question.
0
Jaysin28Commented:
You have to reject the other answer first so I can Answer
0
gthgbAuthor Commented:
ok done

0
Jaysin28Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
caraf_gCommented:
:-)
0
Jaysin28Commented:
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
caraf_gCommented:
It's very strange alright. I don't know why this is happening. Let's hope someone will come up with a solution!
0
gthgbAuthor Commented:
holly poop on a stick ok ill try this and see wot happens

0
gthgbAuthor Commented:
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
caraf_gCommented:
You have to stick it in the General declarations section before any procedures...
0
Jaysin28Commented:
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
gthgbAuthor Commented:
ok illtry that then
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.