Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create an ini file

Status
Not open for further replies.

devinci

MIS
Feb 15, 2002
46
0
0
CA
Hi...I'm trying to figure out how would I be able to create an ini file within my code...when the user runs a program I want to be able to create an ini file to store information without the user knowing about it.

Thanks...
 
OK...

Declare these API functions:

Public Declare Function WritePrivateProfileString _
Lib "kernel32" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationname As String, ByVal lpKeyName As Any, _
ByVal lsString As Any, ByVal lplFilename As String) As Long

Public Declare Function GetPrivateProfileString Lib _
"kernel32" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationname As String, ByVal lpKeyName As _
String, ByVal lpDefault As String, ByVal lpReturnedString _
As String, ByVal nSize As Long, ByVal lpFileName As String) _
As Long


Then to write/create:

WritePrivateProfileString "Title", "Key", "Whatever you want to write", "full path and name of ini"


And to read the data:

'Set temp to 100 chars long (or anything long enough to
'Load default mailto address from ini file
temp = String$(100, vbNullChar)
temp = Left$(temp, GetPrivateProfileString("Title", "Key", "full path and name of ini", temp, Len(temp), "full path and name of ini"))

'Temp will then hold the returned ini data

Note that the "Title" and "Key" can be anything, such as the app title and sub category. You can use the same title but different keys for multiple entries in the same ini, and by calling a specified key it picks the relevant one for you.

Hope that helps.
 
You can use an Api function
Copy the below Url into the address line:
or


or use the below FSO
(Set a reference to the "Micosoft Scriting Runtime"

Public Sub CreateIni(ByVal sPath As String, ByVal sIniName As String)
Dim fso As New Scripting.FileSystemObject
Dim f As Scripting.TextStream
Dim sPathAndName as string

sPathAndName =fso.BuildPath(sPath, sIniName)

fso.CreateTextFile sPathAndName ,overwrite:=True

Set f = fso_OpenTextFile(sPathAndName, IOMode:=ForAppending, Create:=False)

f.WriteLine "[Section1]"
f.WriteLine "myEntry1"
f.WriteLine "myEntry2"

f.Close

Set f = Nothing
Set fso = Nothing

End Sub

Note: I didn't test this exact code. Also, add error handler if desired.
 
when I get to the line of code

fso.CreateTextFile sPathAndName ,overwrite:=True

it tells me "permission denied"
 
sweevo, you beat me to the draw...

One more thing: You may want to store info in the registry instead. A simple way in VB is as foillows:

SaveSetting appname, section, key, setting

temp = GetSetting(appname, section, key[, default])

These will store/get settings to/from the registry under:
HKEY_CURRENT_USER\Software\VB and VBA Program
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top