注册 登录
编程论坛 VB6论坛

VB通过INI配置文件中的路径读取TXT内容到Text1.Text

dooven 发布于 2017-07-03 23:05, 7179 次点击
spama.ini配置文件内容
[ptom]
ath=d:\sd

VB通过spama.ini配置文件中的路径读d:\sd\ss.txt的内容到Text1.Text。(ss.txt不在配置文件的路径中显示。)
求代码。

自己偿试写了代码,经调试出现错误:

路径/文件访问错误
调试
open strvalue & "\ss.txt" for input as #1
这里路径无法访问。求解!谢谢!



Private Declare Function GetPrivateProfileString Lib "kernel32.dll" 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

Private Sub Command1_Click()
strValue = String(FileLen( "spama.ini"), 0)
n=GetPrivateProfileString("ptom","ath", "", strValue, Len(strValue) - 1, "spama.ini")
If n > 1 Then
 strValue = StrConv(LeftB(StrConv(strValue, vbFromUnicode), n), vbUnicode)
End If
open strvalue & "\ss.txt" for input as #1

Text1.Text = Input$(LOF(1), #1)
Close #1
End Sub

[此贴子已经被作者于2017-7-5 00:02编辑过]

16 回复
#2
ZHRXJR2017-07-04 11:29
能不能说清楚一点,你说的不好理解,spama.ini配置文件与ss.txt是什么关系,你的意图是什么,实现什么?
#3
dooven2017-07-05 00:00
回复 2楼 ZHRXJR
意图就是通过ini配置文件中的路径打开TXT文件。谢谢
#4
ZHRXJR2017-07-05 09:34
我认为这个问题非常简单,没有你考虑的那样复杂,根本不需要什么函数,打开ini文件读取 ath=d:\sd 内容,截取 d:\sd 字符串,然后再打开 ss.txt 文件就可以了。
程序代码:
Dim iniLJ As String, txtLJ As String, XX As String
iniLJ = "spama.ini的路径"     '这里填写spama.ini的路径,必须是绝对路径,例如 C:\AB\CD,不能包含文件名
Open iniLJ & "\spama.ini" For Input As #1
Do While Not EOF(1)
Input #1, txtLJ    '读取 spama.ini 的内容
If txtLJ = "ath=d:\sd" Then Exit Do   '判断读取内容是不是 ath=d:\sd ,是退出循环,txtLJ 字符串存储的是 ath=d:\sd
Loop
Close #1
txtLJ = Right(txtLJ, Len(txtLJ) - 4)   '得到 d:\sd 的字符串
Open txtLJ & "\ss.txt" For Input As #1    '打开ss.txt文件
Do While Not EOF(1)
Input #1, XX   '读取 ss.txt 的内容
Text1.Text = Text1.Text & XX & vbCrLf   '在Text1文本框将 ss.txt 的内容全部显示出来
Loop
Close #1
#5
dooven2017-07-05 22:03
回复 4楼 ZHRXJR
spama.ini配置文件内容有很多
[ptom]
ath=d:\sd
[exe]
exe=e:\sd
[ptomW]
ath=E:\sdD
按照大侠的方法好象无法读取了.该怎么改进呢?谢谢。

[此贴子已经被作者于2017-7-5 22:45编辑过]

#6
ZHRXJR2017-07-06 07:58
回复 5楼 dooven
iniLJ = "spama.ini的路径"    '这里填写spama.ini的路径,必须是绝对路径,例如 C:\AB\CD,不能包含文件名
关键是你填写的这个 "spama.ini的路径" 路径对不对,是不是 spama.ini 的路径,如果路径不对,就不行的!!!
Do While Not EOF(1) 是循环,就是从第一行开始读取,直到最后一行
Input #1, txtLJ    '读取 spama.ini 的内容,如果路径不对,肯定读不出来
If txtLJ = "ath=d:\sd" Then Exit Do   '判断读取内容是不是 ath=d:\sd ,是退出循环,只要 spama.ini 的路径没有问题,txtLJ 字符串存储的是 ath=d:\sd,然后退出循环
Loop
Close #1

“按照大侠的方法好象无法读取了.该怎么改进呢?谢谢。”估计是 iniLJ = "spama.ini的路径"  语句中的路径不对!!!
spama.ini配置文件内容再多,即就是有几千上万行也没有问题,If txtLJ = "ath=d:\sd" Then Exit Do 这个语句就绝对在 txtLJ 变量中存储的是 ath=d:\sd 。
txtLJ = Right(txtLJ, Len(txtLJ) - 4)   '得到 d:\sd 的字符串,这个语句截取字符串得到新的字符串,就是 d:\sd ,不会有错的。前提是你的 spama.ini 文件中有 ath=d:\sd 这个记录。

因为不知道你的  spama.ini 配置文件的路径,因此 iniLJ = "spama.ini的路径" 语句中的  "spama.ini的路径" 字符串必须是你自己修改的,不能就这样!!

当然还有一个办法就是使用打开文件对话框打开 spama.ini 配置文件

[此贴子已经被作者于2017-7-6 08:03编辑过]

#7
风吹过b2017-07-06 09:22
Private Declare Function GetPrivateProfileString Lib "kernel32.dll" 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

Private Sub Command1_Click()
strValue = String(FileLen( "spama.ini"), 0)                               '这里有BUG,没有指定路径,当程序起始目录不是程序所在目录,会导致错误
n=GetPrivateProfileString("ptom","ath", "", strValue, Len(strValue) - 1, "spama.ini")         '这里有错误,API调用时,需要给出配置文件全路径,所以读到的空串,返回结果为 0
If n > 1 Then
 strValue = StrConv(LeftB(StrConv(strValue, vbFromUnicode), n), vbUnicode)
End If
open strvalue & "\ss.txt" for input as #1                                 '这里有BUG,没有对返回的路径进行检测,直接使用,当返回空串时,导致出错

Text1.Text = Input$(LOF(1), #1)
Close #1
End Sub
#8
风吹过b2017-07-06 09:27
public Path as string            '定义为全局变量

Private Sub Form_Load()
Path = App.Path                    '读程序的路径
If Right(Path, 1) <> "\" Then        '统一格式,防止放在根目录下出现 BUG
    Path = Path & "\"
End If
End Sub
-------------------------
strValue = String(FileLen( path & "spama.ini"), 0)                           
n=GetPrivateProfileString("ptom","ath", "", strValue, Len(strValue) - 1, path & "spama.ini")
if n>1 then
.....

else
  exit sub            '返回长度为0,直接退出过程
end if
if len(strvalue) < 3 then Exit Sub        '路径无效,直接退出过程
If Right(strvalue , 1) <> "\" Then        '统一格式,容错。
    strvalue = strvalue & "\"
End If

open strvalue & "ss.txt" for input as #1


[此贴子已经被作者于2017-7-6 09:31编辑过]

#9
Jason6662017-07-06 11:00
楼上说的不错,你的配置文件spama.ini路径必须绝对。然后循环读出ini文件所有路径到数组,在进行文件操作
#10
dooven2017-07-06 13:56
回复 6楼 ZHRXJR
spama.ini 和程序在同一文件夹内,另外ath=d:\sd 是一个变量也可能是ath=e:\hl

[此贴子已经被作者于2017-7-7 17:55编辑过]

#11
wds12017-07-08 20:25
Private Sub Form_Load()
 dim cur_dir1,cur_dir2
 dim temp,i
 cur_dir1 = App.Path'程序路径
 CommonDialog1.filename=cur_dir1 & "\spama.ini"
 Open CommonDialog1.FileName For Binary As #1
 temp = Split(Input(LOF(1), 1), vbNewLine)'读入ini文件到内存数组
 close #1
 for i=0 to ubound(temp)
   if  temp(i)="[ptom]" then
    cur_dir2=right(temp(i+1),len(temp(i+1)-4)'取得[ptom]字段的路径
    exit for   
   end if
 next i
 CommonDialog1=cur_dir2 & "\ss.txt"
 Open Cmd1.FileName For Binary As #1
 temp = Split(Input(LOF(1), 1), vbNewLine)'读入ini文件到内存数组1.text
 close #1
 for i=0 to ubound(temp)
  text1.text=text1.text & temp(i) & vbcrlf
 next i
end sub


[此贴子已经被作者于2017-7-8 22:02编辑过]

#12
ZHRXJR2017-07-08 23:23
哎,还是给你做一个吧
只有本站会员才能查看附件,请 登录
这个是有txt文件的界面;
只有本站会员才能查看附件,请 登录
这个是没有txt文件的提示对话框。
下面是给你的exe程序文件,应该满足你的要求了。
只有本站会员才能查看附件,请 登录

#13
wlrjgzs2017-07-09 11:45
回复 12楼 ZHRXJR
老哥,楼主一直说是ini文件,为什么你总是钻牛角尖用文本文件呢?建议你先了解一下ini文件格式再来帮楼主解决问题。
#14
ZHRXJR2017-07-09 11:58
回复 13楼 wlrjgzs
嗨嗨,你没有看程序,你怎么知道我不是使用ini文件的,在压缩包就有 spama.ini 文件,是以 spama.ini 文件打开 所在目录中的 "\ss.txt" 文件的,你下载看看,不要乱说。
spama.ini 文件的内容如下:
[ptom]
ath=d:\sd
[exe]
exe=e:\sd
[ptomW]
ath=E:\sdD
[ptom]
ath=e:\hl
另外界面中的下拉框就是读取了 spama.ini 文件中的 txt 文件的路径的。

[此贴子已经被作者于2017-7-9 12:10编辑过]

#15
wlrjgzs2017-07-10 12:28
回复 14楼 ZHRXJR
哈哈,到底谁乱说,你看清楚了。ini文件是有专门处理ini文件的API函数的。你这个虽然文件是ini文件,但却用处理文本文件的方法来处理,实在是张冠李戴,是个混血儿啊
#16
ZHRXJR2017-07-10 15:17
以下是引用wlrjgzs在2017-7-10 12:28:58的发言:

哈哈,到底谁乱说,你看清楚了。ini文件是有专门处理ini文件的API函数的。你这个虽然文件是ini文件,但却用处理文本文件的方法来处理,实在是张冠李戴,是个混血儿啊


第一,这里是讨论VB编程的论坛,不是骂人的地方,请自重一点。
第二,我没有听说过,VB编程对ini文件处理必须是使用API函数处理的,可能是我孤陋寡闻吧,好像微软也没有这个规定吧。
第三,编程的目的是为了使用方便,有简洁的办法,为什么必须使用复杂的方法。
第四,如果我说的不对,可以讨论,但请不要侮辱其他论坛的网友,这也是尊重你自己。
#17
wlrjgzs2017-07-10 16:12
回复 16楼 ZHRXJR
我哪一句骂人了,老哥不妨指出来。如果确实哪里骂你了,我可以向你道歉。另外,用处理文本的方法来处理ini文件,事实上是变得复杂了,而不是变得简单了。另外,微软就是提倡ini文件不能按文本方式进行处理,否则微软也没必要专门提供处理ini文件的API出来。
你现在只是用处理文本的方式来读取ini文件内容。如果还要对ini文件进行添加、修改以及删除部分内容的情况下,你用处理文本的方式来处理这些操作,岂不把人累死?

[此贴子已经被作者于2017-7-10 16:16编辑过]

1