焦点访谈:---->进来看看!(有关Ini文件操作,比较郁闷!)

ZQY801105 2003-08-22 03:06:19
请教:
  我已经有一个操作Ini文件的类了,我在程序中调用此类,我想向ini文件中一次写入一个Session 多个 KeyWord,如下
SetValue_Str("配置信息", "Server", "user1")
SetValue_Str("配置信息", "DataBase", "sk_2003")
SetValue_Str("配置信息", "Password", "")
为什么总是只写入了第一条,剩下的两条没有写入??
    我试着,每一条都可以单独写入,就是不能全部写入!!
    我好郁闷呀!救命呀!
...全文
27 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zj492 2004-01-15
  • 打赏
  • 举报
回复
文本处理
LVOLCANO 2004-01-15
  • 打赏
  • 举报
回复
Mark
baiweibbc 2004-01-15
  • 打赏
  • 举报
回复
别人的东东,我只是看到了相同的问题。凑个热闹。
baiweibbc 2004-01-15
  • 打赏
  • 举报
回复
把读写ini文件编成一个类,就可以解决了,
因为你操作ini文件时,只允许一个操作执行
iThinkPad 2003-08-22
  • 打赏
  • 举报
回复
这是我自己写的一个类
也是初学,见笑

Public Class MyCls
'--------------------------------------------------
'功能:读出或设置指定INI文件中的指定条目的值
'--------------------------------------------------
Private 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

Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal nDefault As Int32, _
ByVal lpFileName As String) As Int32
'功能:从 INI 文件中指定的条目获取一个整数值
'输出:找到的条目的值;
' 如指定的条目未找到,就返回默认值。
' 如找到的数字不是一个合法的整数,函数会返回其中合法的一部分。
' 如,对于“xyz=55zz”这个条目,函数返回55

Private 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

Private strIniFlName As String '内部私有变量,存放获得的ini文件的路径

Public Property FlName() As String
Get '获得该属性的当前值
Return strIniFlName
End Get
Set(ByVal Value As String) '设置该属性的当前值
strIniFlName = Value '将外部的赋值存储到类内部的私有变量中
End Set
End Property

Public Function GetValue_Str(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefString As String) As String
'如果指定条目的值不为空,则返回该值;
'如果为空,将默认值赋给该条目,并返回默认值
Dim strResult As String, intTmpCnt As Integer
Dim strCharact As String, intCnt As Integer
strResult = Space(255)
intTmpCnt = GetPrivateProfileString(SectionName, KeyWord, "", strResult, 255, strIniFlName)
'检索关键词的值
If intTmpCnt > 0 Then '关键词的值不为空
strCharact = ""
For intCnt = 1 To 255
If Asc(Mid(strResult, intCnt, 1)) = 0 Then '如果遇到空格,则认为结束
Exit For
Else
strCharact = strCharact & Mid(strResult, intCnt, 1)
End If
Next
Else
'将缺省值写入INI文件
'intTmpCnt = WritePrivateProfileString(SectionName, KeyWord, DefString, strIniFlName)
strCharact = DefString
End If
Return strCharact
End Function

Public Function GetValue_Int(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefValue As Integer) As Integer
Dim lngDefValue As Int32
'返回整型值
lngDefValue = GetPrivateProfileInt(SectionName, KeyWord, DefValue, strIniFlName)
Return lngDefValue
End Function

Public Sub SetValue_Str(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValStr As String)
Dim lngReturnValue As Int32
lngReturnValue = WritePrivateProfileString(SectionName, KeyWord, ValStr, strIniFlName)
End Sub

Public Sub SetValue_Int(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValInt As Integer)
Dim lngReturnValue As Int32, ValStr As String
ValStr = CStr(ValInt)
lngReturnValue = WritePrivateProfileString(SectionName, KeyWord, ValStr, strIniFlName)
End Sub
End Class

这是调用,使用的是你的例子
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim MyNewCls As New MyCls()
MyNewCls.FlName = "f:\cnnset.ini"
With MyNewCls
.SetValue_Str("配置信息", "Server", "user1")
.SetValue_Str("配置信息", "DataBase", "sk_2003")
.SetValue_Str("配置信息", "Password", "")
End With

End Sub
没问题,可以写入
iThinkPad 2003-08-22
  • 打赏
  • 举报
回复
没问题,可以写入,是不是其他代码有问题
最好源码贴出来看看
baqiao1211 2003-08-22
  • 打赏
  • 举报
回复
mark
lihonggen0 2003-08-22
  • 打赏
  • 举报
回复
在.net中读写ini文件和Vb6中的做法是一致的,唯一注意的一点是Api声明中的Long型变量要改为int32类型在.net中


Private 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
Private 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

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strIni As String
strIni = New String(" ", 100)
GetPrivateProfileString("AppName", "KeyA", "", strIni, 100, "C:\a.ini")
WritePrivateProfileString("AppName", "KeyB", "MyValue", "C:\a.ini")
MsgBox(strIni)
End Sub

这段代码在.net + Win2000中调试通过

16,554

社区成员

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

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