开发者

Is there a way to read an .ini file into memory and then read particular values from it?

I've got a situation where an ini file is in memory (in a string variable) and I'd like to read values out of it without writing the in开发者_如何学编程i file data to disk.

UPDATE:This is data that I do not want to write to the HD. I'm downloading it from a web server into memory and then getting some data.

Is there any way to do that in VB6? Maybe with a Win API call?


Clay,

Check out this article at DevX.com

Read/Write INI without using API

This should get you pointed in a good direction. The modules are a bit dirty and do, at this point, require a path for the INI to be stored. Instead simply modify the modules to use your string directly and you should receive the desired result. Let me know how this works out for you.


The ini file is to be stored on hard disk. If you want to save in registry instead (this should be added to the registry file too), you can use these functions:

SaveSetting
GetSetting


If you don't want to save it on disk I think it might be difficult to use any specific API calls. But if it's an ini file it should be in a structured format, why not just loop through it until the section you want and then read the values you want from it.

It's been a long time but it should be something like this I think:

Just Split it on newlines to get an array of lines, then each section should start in a certain format and for each line in there you check if it contains an = I think, and if so everything to the left of the first = is the name of the value and everything to the right of it is the value.


Declare the two Windows API function imports

Public Declare Function GetPrivateProfileStringA Lib "kernel32.dll" (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 

Public Declare Function WritePrivateProfileStringA Lib "kernel32.dll" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long 

For reading

sReturn = Space(255)
sReturn = Left(sReturn, GetPrivateProfileStringA(header, key, defaultReturn, sReturn, 255, filePath))

For writting

WritePrivateProfileStringA header, key, Datum, filePath

Remember the structure of an INI file is

[header]
key=data 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜