怎么创建ini文件,如何使用!??

xuxf 2002-08-12 06:57:31
谢谢!
...全文
1617 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjrhf 2002-08-14
  • 打赏
  • 举报
回复
up
xuxf 2002-08-14
  • 打赏
  • 举报
回复
非常感谢,各位大虾!
xuxf 2002-08-14
  • 打赏
  • 举报
回复
假如INI文件为只读属性怎么判断!
dason2u 2002-08-13
  • 打赏
  • 举报
回复
给你发了,收收看。
xuxf 2002-08-13
  • 打赏
  • 举报
回复
呵呵,谢谢各位大虾!

我的email 是: xxftz@21cn.com
dason2u 2002-08-13
  • 打赏
  • 举报
回复
实际上可以当作文本文件来处理,不过有规定的格式,处理的时候按照这个规则来就可以了。例如:
[System]
Version=1.0

如果调用平台API,上面已有人写出示例代码了,如果自己从头写,可能要费点周折,我前不久用C#写了一个IniFile组件,你要吗,给我E-mail
wzsswz 2002-08-13
  • 打赏
  • 举报
回复

Private Function get_phonenumber() As Integer
Dim i As Integer
Dim array_dll As Variant
Dim hh As New gssf.gssfcj
Dim fname0 As String
Dim fname1 As String
Dim msg_reg As Integer
Dim xx As String
get_phonenumber = -1
msg_reg = MsgBox("按”是“将重新拨号催款,按“否”将继续上次未完的拨号催款", vbInformation + vbYesNoCancel, "操作提示")
If msg_reg = vbCancel Then
readok_reg = False
Exit Function
End If
If msg_reg = vbYes Then
Stb1.SimpleText = "系统正从数据库获取数据。。。"
fname0 = App.Path + "\ini\No_dial.ini"
Call delete_file(fname0)
fname0 = App.Path + "\ini\dial_pass.ini"
Call delete_file(fname0)
Create_file (fname0)
fname0 = App.Path + "\ini\dial_unpass.ini"
Call delete_file(fname0)
Create_file (fname0)
fname0 = App.Path + "\ini\temp.ini"
Call delete_file(fname0)
Create_file (fname0)
array_dll = hh.jfcjrecord
If UBound(array_dll) = 0 Then
readok_reg = False
Stb1.SimpleText = "系统无法正从数据库获取数据!"
Sleep 2000
Exit Function
End If
ReDim pho_info(UBound(array_dll) - 1) As PHO
Stb1.SimpleText = "系统正在把数据写入文件" + fname0 + "。。。"
For i = 0 To UBound(array_dll) - 1
' array_dll(i, 3) = "4133002"
pho_info(i).index = i
pho_info(i).comp = array_dll(i, 0)
pho_info(i).linkman = array_dll(i, 1)
pho_info(i).serial = array_dll(i, 2)
pho_info(i).code = array_dll(i, 3)
pho_info(i).sum_month = array_dll(i, 4)
pho_info(i).sum_money = array_dll(i, 5)
pho_info(i).S_index = 0
pho_info(i).E_index = UBound(array_dll) - 1
' pho_number(i) = array_dll(i, 3)
Call ReadWriteINI("write", fname0, CStr(i), "欠款单位", array_dll(i, 0))
Call ReadWriteINI("write", fname0, CStr(i), "联系人", array_dll(i, 1))
Call ReadWriteINI("write", fname0, CStr(i), "收费号码", array_dll(i, 2))
Call ReadWriteINI("write", fname0, CStr(i), "联系电话", array_dll(i, 3))
Call ReadWriteINI("write", fname0, CStr(i), "拖欠月数", array_dll(i, 4))
Call ReadWriteINI("write", fname0, CStr(i), "拖欠款额", array_dll(i, 5))
Next i
readok_reg = True
Stb1.SimpleText = "数据接收完毕。"
Sleep 2000
get_phonenumber = 0
End If
If msg_reg = vbNo Then
fname1 = App.Path + "\ini\No_dial.ini"
If file_exist(fname1) = False Then
Stb1.SimpleText = "系统无法正从文件中获取数据!"
Sleep 2000
Exit Function
End If
Stb1.SimpleText = "系统正在从" + fname1 + "文件中读取数据。。。"
start_id = 0
end_id = 0
For i = 0 To 10000
xx = ReadWriteINI("get", fname1, CStr(i), "联系电话")
If xx <> "" Then
start_id = i
Exit For
End If
Next i
For i = 10000 To 0 Step -1
xx = ReadWriteINI("get", fname1, CStr(i), "联系电话")
If xx <> "" Then
end_id = i
Exit For
End If
Next i
If start_id = 0 And end_id = 0 And xx = "" Then
readok_reg = False
Stb1.SimpleText = "系统无法正从文件中获取数据!"
Sleep 2000
Exit Function
End If

ReDim pho_info(end_id - start_id) As PHO
' ReDim pho_number(end_id - start_id)
For i = 0 To end_id - start_id
pho_info(i).index = i
pho_info(i).comp = Trim(ReadWriteINI("get", fname1, CStr(i + start_id), "欠款单位"))
pho_info(i).linkman = Trim(ReadWriteINI("get", fname1, CStr(i + start_id), "联系人"))
pho_info(i).serial = Trim(ReadWriteINI("get", fname1, CStr(i + start_id), "收费号码"))
pho_info(i).code = Trim(ReadWriteINI("get", fname1, CStr(i + start_id), "联系电话"))
pho_info(i).sum_month = ReadWriteINI("get", fname1, CStr(i + start_id), "拖欠月数")
pho_info(i).sum_money = ReadWriteINI("get", fname1, CStr(i + start_id), "拖欠款额")
pho_info(i).S_index = start_id
pho_info(i).E_index = end_id
' pho_number(i) = ReadWriteINI("get", fname1, CStr(i + start_id), "联系电话")
Next i
readok_reg = True
Stb1.SimpleText = "数据读取完毕。"
Sleep 2000
get_phonenumber = 0
End If

End Function
wzsswz 2002-08-13
  • 打赏
  • 举报
回复
Option Explicit
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As Any, ByVal lpFileName As String) As Long
Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
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

Function ReadWriteINI(Mode As String, FileName As String, tmpSecname As String, Optional tmpKeyname As String, Optional tmpKeyValue) As String
Dim tmpString As String
Dim secname As String
Dim keyname As String
Dim keyvalue As String
Dim anInt
Dim defaultkey As String

On Error GoTo ReadWriteINIError
If IsNull(Mode) Or Len(Mode) = 0 Then
ReadWriteINI = "MODE ERROR "
Exit Function
End If
If Len(FileName) = 0 Then
ReadWriteINI = "FileName ERROR "
Exit Function
End If
If IsNull(tmpSecname) Or Len(tmpSecname) = 0 Then
ReadWriteINI = "Secname ERROR "
Exit Function
End If
If IsNull(tmpKeyname) Or Len(tmpKeyname) = 0 Then
ReadWriteINI = "Keyname ERROR "
Exit Function
End If

' WRITE MODE
If UCase(Mode) = "WRITE" Then
If IsNull(tmpKeyValue) Or Len(tmpKeyValue) = 0 Then
ReadWriteINI = "ERROR KeyValue"
Exit Function
Else
secname = tmpSecname
keyname = tmpKeyname
keyvalue = tmpKeyValue
anInt = WritePrivateProfileString(secname, keyname, keyvalue, FileName)
End If
End If
' READ MODE
If UCase(Mode) = "GET" Then

secname = tmpSecname
keyname = tmpKeyname
defaultkey = "Failed"
keyvalue = String$(50, 32)
anInt = GetPrivateProfileString(secname, keyname, defaultkey, keyvalue, Len(keyvalue), FileName)
If Left(keyvalue, 6) <> "Failed" Then
tmpString = keyvalue
tmpString = RTrim(tmpString)
tmpString = Left(tmpString, Len(tmpString) - 1)
End If
ReadWriteINI = tmpString
End If
Exit Function
ReadWriteINIError:
MsgBox Error
Stop
End Function
edreamy 2002-08-13
  • 打赏
  • 举报
回复
一个读写INI文件的类,有Write,Delete,Read三个方法对INI文件操作

Public Class INIManage
#Region "API函数声明"
'写INI文件API函数
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32
'读INI文件API函数
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 Int32, ByVal lpFileName As String) As Int32
#End Region

#Region "写INI文件函数"
Public Function Write(ByVal INIPath As String, ByVal SectionName As String, ByVal KeyName As String, ByVal Value As String) As Boolean
Try
WritePrivateProfileString(SectionName, KeyName, Value, INIPath)
Return True
Catch
Return False
End Try
End Function
#End Region

#Region "读INI文件函数"
Public Function Read(ByVal INIPath As String, ByVal SectionName As String, ByVal KeyName As String) As String
Dim intRetVal As Int32
Dim strData, Value As String
strData = Space(1024)
Try
intRetVal = GetPrivateProfileString(SectionName, KeyName, Value, strData, strData.Length, INIPath)
If intRetVal > 0 Then
Read = strData.Substring(0, intRetVal)
Else
Read = ""
End If
Catch
Read = ""
End Try
End Function
#End Region

#Region "删INI文件函数"
'删除指定的键
Public Overloads Function Delete(ByVal INIPath As String, ByVal SectionName As String, ByVal KeyName As String) As Boolean
Try
WritePrivateProfileString(SectionName, KeyName, Nothing, INIPath)
Return True
Catch
Return False
End Try
End Function

'删除指定的节
Public Overloads Function Delete(ByVal INIPath As String, ByVal SectionName As String) As Boolean
Try
WritePrivateProfileString(SectionName, Nothing, Nothing, INIPath)
Return True
Catch
Return False
End Try
End Function
#End Region
End Class
xinsb 2002-08-12
  • 打赏
  • 举报
回复
感谢您使用微软的产品。

您可以通过使用api来实现在vb.net中创建ini文件,并实现对ini文件的读写操作。
创建ini文件和创建普通的文本文件是一样的。对ini文件进行读写关键在于两个函数,WritePrivateProfileString(写操作),GetPrivateProfileString(读操作)
具体的代码如下:

Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName 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 StringBuilder, ByVal nSize As Int32, ByVal lpFileName As String) As Int32

Private Function IniKeySet(ByVal Section As String, ByVal Key As String, ByVal Value As String, ByVal filepath As String) As Long '对ini文件进行写操作
WritePrivateProfileString(Section, Key, Value, filepath)
End Function

Private Function IniKeyRead(ByVal Section As String, ByVal key As String, ByVal filepath As String) As String '对ini文件进行读操作
Dim buffer As New StringBuilder(255)
Dim i As Int32

i = GetPrivateProfileString(Section, key, "", buffer, 255, filepath)

Console.WriteLine(buffer.ToString())
Console.WriteLine(i)
Return (buffer.ToString())
End Function

Dim filepath As String '创建文件
filepath = "e:\\temp\\db.ini"
Dim sr As StreamWriter
sr = New StreamWriter(filepath)
sr.Close()

Me.IniKeySet("ODBC 32 bit Data Sources", "MS Access Database", "skywqlker", filepath) '创建ODBC 32 bit Data Sources section,以及下面的三个key
Me.IniKeySet("ODBC 32 bit Data Sources", "dBASE Files", "Microsoft dBase Driver (*.dbf) (32 bit)", filepath)
Me.IniKeySet("ODBC 32 bit Data Sources", "Excel Files", "Microsoft Excel Driver (*.xls) (32 bit)", filepath)

Me.TextBox1.Text = Me.IniKeyRead("ODBC 32 bit Data Sources", "dBASE Files", "e:\\temp\\db.ini") '实现对ini的key的读取。





- 微软全球技术中心 VB技术支持

本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款
(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查
(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
landog 2002-08-12
  • 打赏
  • 举报
回复
msdn中有,查ini
proking 2002-08-12
  • 打赏
  • 举报
回复
我想同文本文件一样,应该没问题吧。

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧