您的批评和鼓励都是我把编程无限办好的动力! 您编程时遇到困难,或遇到不顺心的事想发发牢骚尽管到这里来吧! 虽然本网力求全面,但也不能包罗万象,这些我筛选出的优秀网站连接就是对本站最好的补充! 学习编程当然也离不开书本了,这里收集大量编程书籍! 编程无限之源码超市,这里收集的代码令你意想不到的全面! 欢迎光临编程网校,这里专门收集VB/CB入门文章及技术文章! 欢迎光临编程无限!
     
       
 
当前位置:英文资料 >> FileSystem
go over the major functions you can do with an INI file
  资料类型: FileSystem 上传时间: 2001-02-21 阅读次数: 1500



Most programs need to keep some sort of settings information. This is commonly used for setting up file paths, registration numbers, the form’s last location, users’ names and tons of varying but specific information. You could keep this in your own file format, but Windows has given us a simpler way to do this by using initialization (INI) files.

Not only are INI files easy to use, but they are fast! Even though Microsoft wants us to use the registration database for this type of information in Visual Basic 4.0 and Visual Basic 5.0, INI files are still faster and the only way to keep this type of informaion local to your program. This way, if the Registration Database gets wiped out or corrupted, your program will still work.

Using INI files is not built into Visual Basic 3.0 and below. To further complicate matters, Microsoft has not done a good job of documenting INI file use for Visual Basic programmers.

In this tip, we will go over the major functions you can do with an INI file.

Declare The Following
Declare Function WritePrivateProfileString Lib "Kernel" _
(ByVal AppName As String, ByVal KeyName As String, _
ByVal NewString As String, ByVal filename As String) _
As Integer
Declare Function GetPrivateProfileString Lib "Kernel" _
(ByVal AppName As String, ByVal KeyName As String, _
ByVal default As String, ByVal ReturnedString As _
String, ByVal MAXSIZE As Integer, ByVal filename As _
String) As Integer
Declare Function GetProfileInt Lib "Kernel" (ByVal _
lpAppName As String, ByVal lpKeyName As String, _
ByVal nDefault As String) As Integer
Declare Function GetProfileString Lib "Kernel" (ByVal _
lpAppName As String, ByVal lpKeyName As String, _
ByVal lpDefault As String, ByVal lpReturnedString _
As String, ByVal nSize As Integer) As Integer
Declare Function WriteProfileString Lib "Kernel" _
(ByVal lpAppName As String, ByVal lpKeyName As _
String, ByVal lpString As String) As Integer
Declare Function GetPrivateProfileInt Lib "Kernel" _
(ByVal lpAppName As String, ByVal lpKeyName As _
String, ByVal nDefault As Integer, ByVal lpFileName _
As String) As Integer
You will notice here that there are basically two different types of INI files, private and non-private. Private profile calls should be used for your program’s INI file while the non-private profile calls should be used for the WIN.INI file that’s found in the C:WINDOWS directory..

Parameter Values
Here is some terminology you will need to remember when dealing with INI files.



AppName - Heading of the section in an INI file.
KeyName - Key within a heading.
Value - Value either being set or retrieved.
Reading An INI File
For most purposes, you will be reading either strings or integers from INI files. Well, for that matter, everything could be strings and you can convert them to integers.

Reading Integer Values
Function iReadINI (sAppName As String, sKeyName As _
String, sFilename As String, iDefault As Integer) _
As Integer


iReadINI = GetPrivateProfileInt(sAppName, _
ByVal sKeyName, iDefault, sFilename)
End Function
Usage
Dim iMyValue As Integer
iMyValue = GetPrivateProfileInt("VBTT", "Download", _
App.Path & "VBTT.INI", 0)
One key thing to remember, here when writing or reading INI files, is that if you don’t specifically specify a path where for the INI file. Windows, by default, puts it in the C:WINDOWS directory. In most cases, you would want to keep your INI file in the same directory as your application. It makes it easier to remove all traces of your program, if needed.

Reading String Values
Function sReadINI (sAppName As String, sKeyName As _
String, sFilename As String) As String
Dim sReturn As String
sReturn = String(255, Chr(0))
sReadINI = Left(sReturn, GetPrivateProfileString(sAppName, ByVal sKeyName, "", sReturn, Len(sReturn), sFilename))
End Function
Since this API call returns a string, we need to first create a string large enough to hold the return value. Since most of the time, that length is unknown, it’s better to create a large string. That’s why this code uses the maximum of 255 characters.

You can change the code to return a default value, but, in my experience, I have not found a use for it. If there is no value, then it’s never been set.

Usage
Dim sMyValue As String

sMyValue = sReadINI("VBTT", "Download Directory", _

App.Path & "VBTT.INI")

Note: One thing to remember about when reading reading INI files: if the INI file does not exist you will get an empty string, when reading strings, or the default value, when reading integers.

Writing To An INI File
Writing to an INI file is just as easy. I always use the WriteProfileString, because any value can be sent, including integers.

Code
Sub WriteINI (sAppName As String, sKeyName As String, _
sNewString As String, sFilename As String)
Dim R As Integer


R = WritePrivateProfileString(sAppName, sKeyName, _
sNewString, sFilename)
End Sub
Usage
WriteINI "VBTT", "Download Directory", _
"c:Vbttdownload", App.Path & "VBTT.INI"
Removing KeyNames
Now for some tricks that are very hard to find. How about if you want to remove a Key or AppName? Well, it’s straight-forward too, just not well documented.

Declare The Following
Declare Function RemovePrivateProfileString Lib _
"Kernel" Alias "WritePrivateProfileString" (ByVal _
AppName As String, ByVal KeyName As Any, ByVal _
NewString As Any, ByVal filename As String) As Integer
Here we are using the Alias feature of an API call. We are just renaming the WritePrivateProfileString call to a new name of RemovePrivateProfileString. This way, we can pass it a key "As Any". We are doing this because the value that we will use is not a string, but we want to keep the "As String" in the original WritePrivateProfileString call for type checking.

Removing KeyNames
Sub RemoveKeyName (sAppName As String, sKey As String, _
sFileName As String)
Dim R As Integer


R = RemovePrivateProfileString(sAppName, sKey, 0&, _
sFileName)
End Sub
Usage
RemoveKeyName "VBTT", "Download Directory", _
App.Path & "VBTT.INI"
The trick here is the 0&, in the place the value would normally be used, will erase the key from the INI file.

Removing AppName Section
Sub RemoveAppNameSection (sAppName As String, _
sFileName As String)
Dim R As Integer


R = RemovePrivateProfileString(sAppName, 0&, 0&, _
sFileName)
End Sub
Usage
RemoveAppNameSection "VBTT", App.Path & "VBTT.INI"

推荐给朋友 点 评( 0 ) 返回前页 关闭此页
   
  本类最热文章排名:
  1.Copy File
2.how to send files to the Recycle Bin
3.detect a DBF file is being used
4.a function to verify if a file exists
5.Format a floppy disk
6.Move a File
7.This function return...
8.Create an entire dir...
9.Lauches an applicati...
10.complete control ove...
   
   
  评论:
 
 
 

 

关于本站 版权声明 联系方法
编程无限 V4.1 Copyright © 1999-2008 21code.com

京ICP备05006938号