| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6730 人关注过本帖, 1 人收藏
标题:VB通过INI配置文件中的路径读取TXT内容到Text1.Text
只看楼主 加入收藏
dooven
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2017-7-1
收藏(1)
 问题点数:0 回复次数:16 
VB通过INI配置文件中的路径读取TXT内容到Text1.Text
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编辑过]

搜索更多相关主题的帖子: INI 路径 TXT String ByVal 
2017-07-03 23:05
ZHRXJR
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:125
帖 子:1034
专家分:5519
注 册:2016-5-10
收藏
得分:0 
能不能说清楚一点,你说的不好理解,spama.ini配置文件与ss.txt是什么关系,你的意图是什么,实现什么?

请不要选我!!!
2017-07-04 11:29
dooven
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2017-7-1
收藏
得分:0 
回复 2楼 ZHRXJR
意图就是通过ini配置文件中的路径打开TXT文件。谢谢
2017-07-05 00:00
ZHRXJR
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:125
帖 子:1034
专家分:5519
注 册:2016-5-10
收藏
得分:0 
我认为这个问题非常简单,没有你考虑的那样复杂,根本不需要什么函数,打开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

请不要选我!!!
2017-07-05 09:34
dooven
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2017-7-1
收藏
得分:0 
回复 4楼 ZHRXJR
spama.ini配置文件内容有很多
[ptom]
ath=d:\sd
[exe]
exe=e:\sd
[ptomW]
ath=E:\sdD
按照大侠的方法好象无法读取了.该怎么改进呢?谢谢。

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

2017-07-05 22:03
ZHRXJR
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:125
帖 子:1034
专家分:5519
注 册:2016-5-10
收藏
得分:0 
回复 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编辑过]


请不要选我!!!
2017-07-06 07:58
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4938
专家分:30047
注 册:2008-10-15
收藏
得分:0 
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

授人于鱼,不如授人于渔
早已停用QQ了
2017-07-06 09:22
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4938
专家分:30047
注 册:2008-10-15
收藏
得分:0 
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编辑过]


授人于鱼,不如授人于渔
早已停用QQ了
2017-07-06 09:27
Jason666
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2017-7-6
收藏
得分:0 
楼上说的不错,你的配置文件spama.ini路径必须绝对。然后循环读出ini文件所有路径到数组,在进行文件操作
2017-07-06 11:00
dooven
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2017-7-1
收藏
得分:0 
回复 6楼 ZHRXJR
spama.ini 和程序在同一文件夹内,另外ath=d:\sd 是一个变量也可能是ath=e:\hl

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

2017-07-06 13:56
快速回复:VB通过INI配置文件中的路径读取TXT内容到Text1.Text
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017754 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved