首页
社区
课程
招聘
[求助]如何读取当前文件夹下的ini文件?
发表于: 2007-9-14 20:24 8264

[求助]如何读取当前文件夹下的ini文件?

2007-9-14 20:24
8264
写了以下的一个读取ini文件的小程序,代码如下:

Private Declare Function GetPrivateProfileString Lib "kernel32" _
   Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
   ByVal lpKeyName As Any, ByVal lpDefault As String, _
   ByVal lpReturnedString As String, ByVal nSize As Long, _
   ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" _
   Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
   ByVal lpKeyName As Any, ByVal lpString As Any, _
   ByVal lpFileName As String) As Long

Private Sub cmdOK_Click()
Dim success As Long
success = WritePrivateProfileString("帐号资料", "ID", UserName.Text, "sgyscfg.ini")
success = WritePrivateProfileString("帐号资料", "PW", PassWord.Text, "sgyscfg.ini")
End Sub

Private Sub Form_Load()
Dim ret As Long
Dim buff As String
buff = String(255, 0)
ret = GetPrivateProfileString("帐号资料", "UserName", "", buff, 256, "sgyscfg.ini")
UserName.Text = buff
buff = String(255, 0)
ret = GetPrivateProfileString("帐号资料", "PassWord", "", buff, 256, "sgyscfg.ini")
PassWord.Text = buff

这样的代码,程序生成后,sgyscfg.ini文件是存在了c:\windows文件夹下了,如果把"sgyscfg.ini"改成"\sgyscfg.ini"就保存到了c:\下,怎样也弄不到保存在执行文件所在的文件夹里,求助!!

[课程]FART 脱壳王!加量不加价!FART作者讲授!

收藏
免费 0
支持
分享
最新回复 (20)
雪    币: 2384
活跃值: (766)
能力值: (RANK:410 )
在线值:
发帖
回帖
粉丝
2
用GetCurrentDirectory函数获取程序的当前目录,再将目录和文件名连接起来就行了。
2007-9-14 21:10
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
版版,不是很明白你的意思,能写写出来吗?这代码是网上找的,不是很懂改!
2007-9-14 22:52
0
雪    币: 224
活跃值: (147)
能力值: ( LV9,RANK:970 )
在线值:
发帖
回帖
粉丝
4
做公司的项目发现GetCurrentDirectory有时候获取到的会有问题

用GetModuleFullName吧

WritePrivateProfileString("帐号资料", "ID", UserName.Text, "sgyscfg.ini")

"sgyscfg.ini"改成用GetModuleFullName获取的路径,基本万无一失。
2007-9-14 23:02
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
改了,还是不行,都是存在windows文件夹下的。存不了在执行文件的目录下
2007-9-14 23:10
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
能改一下硪的帖上的代码,给硪发份吗?搞不懂要怎改才能。头痛
2007-9-14 23:13
0
雪    币: 2384
活跃值: (766)
能力值: (RANK:410 )
在线值:
发帖
回帖
粉丝
7
Private Declare Function GetPrivateProfileString Lib "Kernel32" _
   Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
   ByVal lpKeyName As Any, ByVal lpDefault As String, _
   ByVal lpReturnedString As String, ByVal nSize As Long, _
   ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "Kernel32" _
   Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
   ByVal lpKeyName As Any, ByVal lpString As Any, _
   ByVal lpFileName As String) As Long
Private Declare Function GetCurrentDirectory Lib "Kernel32" _
   Alias "GetCurrentDirectoryA" (ByVal nBuffSize As Long, _
   ByVal lpPathBuff As String) As Long
   
Private Sub Command1_Click()
Dim szPathBuff As String
Dim szPathIniName As String
Dim Length As Long
Dim b As Boolean
szPathBuff = String(256, 0)
szPathIniName = "sgyscfg.ini"
Length = GetCurrentDirectory(256, szPathBuff)
If Mid(szPathBuff, Length, 1) <> "\" Then
  szPathIniName = "\" & szPathIniName
End If
szPathIniName = Mid(szPathBuff, 1, Length) & szPathIniName
b = WritePrivateProfileString("帐号资料", "UserName", Text1.Text, szPathIniName)
b = WritePrivateProfileString("帐号资料", "PassWord", Text2.Text, szPathIniName)
End Sub

Private Sub Command2_Click()
Dim szPathBuff As String
Dim szPathIniName As String
Dim szBuff As String
Dim Length As Long
Dim l As Long
szPathBuff = String(256, 0)
szBuff = String(256, 0)
szPathIniName = "sgyscfg.ini"
Length = GetCurrentDirectory(256, szPathBuff)
If Mid(szPathBuff, Length, 1) <> "\" Then
  szPathIniName = "\" & szPathIniName
End If
szPathIniName = Mid(szPathBuff, 1, Length) & szPathIniName
l = GetPrivateProfileString("帐号资料", "UserName", "", szBuff, 256, szPathIniName)
Text1.Text = szBuff
szBuff = String(256, 0)
l = GetPrivateProfileString("帐号资料", "PassWord", "", szBuff, 256, szPathIniName)
Text2.Text = szBuff
End Sub
2007-9-14 23:19
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
谢谢版版。。。
2007-9-14 23:29
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
9
版版,跟你的代码试了一下,奇迹般的发现,文件是在VB的文件夹里才生成,否则就不能升成了。放了很多地方都不行
2007-9-15 00:07
0
雪    币: 2384
活跃值: (766)
能力值: (RANK:410 )
在线值:
发帖
回帖
粉丝
10
'测试了下,看来如wynney兄所说,GetCurrentDirectory的确有问题
'将GetCurrentDirectory改成GetModuleFileName吧。

Private Declare Function GetPrivateProfileString Lib "Kernel32" _
   Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
   ByVal lpKeyName As Any, ByVal lpDefault As String, _
   ByVal lpReturnedString As String, ByVal nSize As Long, _
   ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "Kernel32" _
   Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
   ByVal lpKeyName As Any, ByVal lpString As Any, _
   ByVal lpFileName As String) As Long
Private Declare Function GetModuleFileName Lib "Kernel32" _
   Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpPathBuff As String, _
   ByVal nBuffSize As Long) As Long
   
Private Sub Command1_Click()
Dim szPathBuff As String
Dim szPathIniName As String
Dim Length As Long
Dim b As Boolean
Dim i As Integer, j As Integer
szPathBuff = String(256, 0)
szPathIniName = "sgyscfg.ini"
Length = GetModuleFileName(App.hInstance, szPathBuff, 256)
j = Length
For i = 1 To Length
  If Mid(szPathBuff, j, 1) = "\" Then
    Exit For
  End If
  j = j - 1
Next i
szPathIniName = Mid(szPathBuff, 1, j) & szPathIniName
b = WritePrivateProfileString("帐号资料", "UserName", Text1.Text, szPathIniName)
b = WritePrivateProfileString("帐号资料", "PassWord", Text2.Text, szPathIniName)
End Sub

Private Sub Command2_Click()
Dim szPathBuff As String
Dim szPathIniName As String
Dim szBuff As String
Dim Length As Long
Dim l As Long
Dim i As Integer, j As Integer
szPathBuff = String(256, 0)
szBuff = String(256, 0)
szPathIniName = "sgyscfg.ini"
Length = GetModuleFileName(App.hInstance, szPathBuff, 256)
j = Length
For i = 1 To Length
  If Mid(szPathBuff, j, 1) = "\" Then
    Exit For
  End If
  j = j - 1
Next i
szPathIniName = Mid(szPathBuff, 1, j) & szPathIniName
l = GetPrivateProfileString("帐号资料", "UserName", "", szBuff, 256, szPathIniName)
Text1.Text = szBuff
szBuff = String(256, 0)
l = GetPrivateProfileString("帐号资料", "PassWord", "", szBuff, 256, szPathIniName)
Text2.Text = szBuff
End Sub
2007-9-15 00:38
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
11
测试过,终于好了。感谢 版版:小虾,感谢:wynney兄 的帮忙!!

还有一个小问题想问下,如果把Command2里的代码,加在check(单选框)里面,不知道可以不?想实现像TM一样,选择了后,下次启动时就不用再选择的,要怎弄?有人说自定义0和1在加判断就行了,但不太懂,版版能再帮硪一下吗?
2007-9-15 09:05
0
雪    币: 243
活跃值: (169)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
12
GetCurrentDirectory得到的可能是父进程路径,可能说发不对
比如我用c:\a.exe启动d:\b.exe,那么b.exe中GetCurrentDirectory获得的是a.exe的路径c:\
2007-9-15 09:57
0
雪    币: 224
活跃值: (147)
能力值: ( LV9,RANK:970 )
在线值:
发帖
回帖
粉丝
13
呵呵,正是这样,
2007-9-15 10:49
0
雪    币: 153
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
14
晕....基本功先学好。
app.path
2007-9-15 12:51
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
15
app.path 试过了,保存不了+不能读取啊。
2007-9-15 14:23
0
雪    币: 153
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
16
在VB环境中调试运行时,app.path就是vb.exe的path,程序单独运行时,app.path就是程序的path。  
为什么呢?  因为它是VB!!!!
在VB环境中调试运行时,你的程序不是一个单独的进程,而是VB自己的进程。所以你拿到的path就是VB的path。同样用GetModuleFileName得到的也是VB的路径。

而单独运行你的exe时,通过GetModuleFileName得到的就是你exe的路径,app.path也是你的路径。

这个你可以用msgbox弹出来看一下就知道了。
2007-9-15 16:22
0
雪    币: 153
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
17
另外,是app.path +"\your.ini" !!!!
2007-9-15 16:22
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
18
是的,就是你这样,但结果是文件保存不出来,读取也不出来!(读取不出来应该是因为没有保存吧)
2007-9-15 18:09
0
雪    币: 153
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
19
那只能说是RPWT了。
另外,你的程序:

success = WritePrivateProfileString("帐号资料", "ID", UserName.Text, "sgyscfg.ini")
success = WritePrivateProfileString("帐号资料", "PW", PassWord.Text, "sgyscfg.ini")

Private Sub Form_Load()
Dim ret As Long
Dim buff As String
buff = String(255, 0)
ret = GetPrivateProfileString("帐号资料", "UserName", "", buff, 256, "sgyscfg.ini")
UserName.Text = buff
buff = String(255, 0)
ret = GetPrivateProfileString("帐号资料", "PassWord", "", buff, 256, "sgyscfg.ini")

写的是ID,读的是UserName,怎么可能拿到啊。
2007-9-16 19:27
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
20
这个当时的确没有注意到,但后来改了也是不行,最后用上版版的代码,终于好了。
2007-9-18 09:28
0
雪    币: 196
活跃值: (96)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
21
用 .\\sgyscfg.ini
2007-9-18 14:31
0
游客
登录 | 注册 方可回帖
返回
//