问题描述:
我想在VB程序运行时,可以获得本运行程序的路径,请问怎么得到这个?
解析:
窗体载入时显示对话框内容为当前路径!
Private Sub Form_Load()
MsgBox "本程序的路径是: " &App.Path, 48, "提示"
End Sub
给你个VB遍历文件夹的代码吧Private
Declare
Function
FindFirstFile
Lib
"kernel32"
Alias
"FindFirstFileA"
(ByVal
lpFileName
As
String,
lpFindFileData
As
WIN32_FIND_DATA)
As
Long
'查找下一个文件的API
Private
Declare
Function
FindNextFile
Lib
"kernel32"
Alias
"FindNextFileA"
(ByVal
hFindFile
As
Long,
lpFindFileData
As
WIN32_FIND_DATA)
As
Long
'获取文件属性的API
Private
Declare
Function
GetFileAttributes
Lib
"kernel32"
Alias
"GetFileAttributesA"
(ByVal
lpFileName
As
String)
As
Long
'关闭查找文件的API
Private
Declare
Function
FindClose
Lib
"kernel32"
(ByVal
hFindFile
As
Long)
As
Long
Const
MAX_PATH
=
260
Const
MAXDWORD
=
&HFFFF
Const
FILE_ATTRIBUTE_DIRECTORY
=
&H10
Private
Type
FILETIME
dwLowDateTime
As
Long
dwHighDateTime
As
Long
End
Type
Dim
tempstr
As
String
'定义类(用于查找文件)
Private
Type
WIN32_FIND_DATA
dwFileAttributes
As
Long
ftCreationTime
As
FILETIME
ftLastACCESSTime
As
FILETIME
ftLastWriteTime
As
FILETIME
nFileSizeHigh
As
Long
nFileSizeLow
As
Long
dwReserved0
As
Long
dwReserved1
As
Long
cFileName
As
String
*
MAX_PATH
cAlternate
As
String
*
14
End
Type
Dim
filecount
As
Integer
Dim
dirs()
As
String
Dim
curr
As
Long
Dim
ss()
As
String
Private
Sub
Command1_Click()
tempstr
=
"c:"
searchdir
tempstr
filecount
=
0
End
Sub
Public
Function
searchdir(path
As
String)
Dim
WFD
As
WIN32_FIND_DATA
Dim
i
As
Long
Dim
temp
As
String
Dim
h
As
Long
Dim
zhaodao
As
Long
Dim
iindex
As
Integer
Dim
dirs()
As
String
Dim
l
As
Long
zhaodao
=
1
h
=
FindFirstFile(path
&
"\*.*",
WFD)
If
h
<>
-1
Then
While
zhaodao
zhaodao
=
1
temp
=
Left(WFD.cFileName,
InStr(WFD.cFileName,
Chr$(0))
-
1)
If
temp
<>
"."
And
temp
<>
".."
Then
If
WFD.dwFileAttributes
And
vbDirectory
Then
ReDim
Preserve
dirs(iindex)
dirs(iindex)
=
path
&
"\"
&
temp
iindex
=
iindex
+
1
ReDim
Preserve
ss(filecount)
ss(filecount)
=
path
&
"\"
&
temp
filecount
=
filecount
+
1
End
If
End
If
zhaodao
=
FindNextFile(h,
WFD)
Wend
End
If
FindClose
(h)
If
iindex
>
0
Then
For
i
=
0
To
iindex
-
1
Call
searchdir(dirs(i))
Next
i
End
If
End
Function
“当用Dialog一次打开多个文件”————你这里的Dialog指的是通用的OpenDialog“打开”对话框吗?这样做:
引用Common Dialog 控件库。
在窗口上添加一个List1,一个Label1,两个按钮Command1和Command2,一个ComDlg控件命名为CD1。
然后加入以下代码:
Option Explicit
Private Sub Command1_Click()
CD1.Flags = cdlOFNAllowMultiselect Or cdlOFNExplorer
CD1.ShowOpen
If CD1.FileName = "" Then Exit Sub
Dim Str1 As String
Dim Ary1() As String
Str1 = CD1.FileName
Ary1 = Split(Str1, Chr(0))
If UBound(Ary1) = 0 Then
ReDim Preserve Ary1(1)
Dim n1 As Long
n1 = InStrRev(Ary1(0), "\")
Ary1(1) = Mid(Ary1(0), n1 + 1)
Ary1(0) = Left(Ary1(0), n1)
End If
Dim s1 As Long, Apath1 As String
Apath1 = App.Path
If Right(Apath1, 1) <>"\" Then Apath1 = Apath1 &"\"
If Right(Ary1(0), 1) <>"\" Then Ary1(0) = Ary1(0) &"\"
Open Apath1 &"st.ini" For Output As #2
For s1 = 1 To UBound(Ary1)
Print #2, Ary1(0) &Ary1(s1)
Next
Close #2
End Sub
Private Sub Command2_Click()
Dim s1 As Long, Apath1 As String
Apath1 = App.Path
If Right(Apath1, 1) <>"\" Then Apath1 = Apath1 &"\"
Dim Str1 As String
Open Apath1 &"st.ini" For Input As #3
Do Until EOF(3)
Line Input #3, Str1
List1.AddItem Str1
Loop
Close #3
End Sub
Private Sub List1_Click()
If List1.ListIndex <0 Then Exit Sub
Label1.Caption = List1.List(List1.ListIndex)
End Sub
ok,运行看看吧。
点击Command1会打开文件并把文件路径写入st.ini
点击Command2会读取st.ini,并显示到List1中
点击List1中的项目会在Label中显示内容。
完成了,已经在VB6下调试通过,给分吧~~
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)