VB编程中,搜寻到文件在列表中例出,并用鼠标双击它打开文件或程序!
OptionExplicitDimfs,folder,subfolders,Files,i,jDimmStopAsBooleanFunctiondigui(path)If...
Option Explicit
Dim fs, folder, subfolders, Files, i, j
Dim mStop As Boolean
Function digui(path)
If mStop = True Then Exit Function
Set folder = fs.getfolder(path)
Set subfolders = folder.subfolders
Set Files = folder.Files
For Each i In Files
If InStr(1, i.Name, Text1.Text, 1) Then
List1.AddItem i.path
End If
DoEvents
Next
For Each j In subfolders
digui (j.path)
Next
End Function
Private Sub Command1_Click()
If Command1.Caption = "搜寻" Then
Command1.Caption = "停止"
mStop = False
List1.Clear
Set fs = CreateObject("scripting.filesystemobject")
digui (Dir1.path)
If List1.ListCount = 1 Then
End If
Command1.Caption = "搜寻"
Else
Command1.Caption = "搜寻"
mStop = True
End If
End Sub
Private Sub Drive1_Change()
Dir1.path = Drive1.Drive
End Sub
Private Sub Form_Load()
End Sub
搜索程序已写好,就缺那个打开文件的程序,其实我要打一条码打印文件,文件类型为“ArgoBar Pro Document”,试了下面的都不成功 展开
Dim fs, folder, subfolders, Files, i, j
Dim mStop As Boolean
Function digui(path)
If mStop = True Then Exit Function
Set folder = fs.getfolder(path)
Set subfolders = folder.subfolders
Set Files = folder.Files
For Each i In Files
If InStr(1, i.Name, Text1.Text, 1) Then
List1.AddItem i.path
End If
DoEvents
Next
For Each j In subfolders
digui (j.path)
Next
End Function
Private Sub Command1_Click()
If Command1.Caption = "搜寻" Then
Command1.Caption = "停止"
mStop = False
List1.Clear
Set fs = CreateObject("scripting.filesystemobject")
digui (Dir1.path)
If List1.ListCount = 1 Then
End If
Command1.Caption = "搜寻"
Else
Command1.Caption = "搜寻"
mStop = True
End If
End Sub
Private Sub Drive1_Change()
Dir1.path = Drive1.Drive
End Sub
Private Sub Form_Load()
End Sub
搜索程序已写好,就缺那个打开文件的程序,其实我要打一条码打印文件,文件类型为“ArgoBar Pro Document”,试了下面的都不成功 展开
7个回答
展开全部
楼主,你不是写好了?递归遍历文件搜索吗?
不能实现搜索吗?
噢,搜索实现了,是想运行选择的文件吧?
我们知道,在windows系统中,能直接运行的只有三种文件,.exe .bat .com其他所有的文件都是通过调用这三种文件而运行的,所以有文件关联的概念.比如一个文本文件,1.txt吧,它的打开是由于windows设定好有notepad.exe文件运行调用1.txt所以我们才能使用,在VB中如果您想实现直接找到关联程序来运行文件那么请用一个api函数,既然您能写出递归,那么也有一定的编程水平了,api的声明等等就不用多解释了.下面的代码您试着在您的程序中用吧.
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
ShellExecute Me.hWnd, "open", fn, "", "", 1
'fn就是要打开的文件的绝对路径,比如c:\111\222\3.doc
有个使用前提,比如上面的3.doc系统需要有它的文件关联程序才可以打开,比如office word.
顺带介绍一下关于批处理的打开任意文件的关联程序并调用它,还用上面的例
start c:\111\222\3.doc
对了,补充一下me.hwnd是以窗体句柄打开文件,这个参数设置为0,貌似以自身句柄打开文件.
不能实现搜索吗?
噢,搜索实现了,是想运行选择的文件吧?
我们知道,在windows系统中,能直接运行的只有三种文件,.exe .bat .com其他所有的文件都是通过调用这三种文件而运行的,所以有文件关联的概念.比如一个文本文件,1.txt吧,它的打开是由于windows设定好有notepad.exe文件运行调用1.txt所以我们才能使用,在VB中如果您想实现直接找到关联程序来运行文件那么请用一个api函数,既然您能写出递归,那么也有一定的编程水平了,api的声明等等就不用多解释了.下面的代码您试着在您的程序中用吧.
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
ShellExecute Me.hWnd, "open", fn, "", "", 1
'fn就是要打开的文件的绝对路径,比如c:\111\222\3.doc
有个使用前提,比如上面的3.doc系统需要有它的文件关联程序才可以打开,比如office word.
顺带介绍一下关于批处理的打开任意文件的关联程序并调用它,还用上面的例
start c:\111\222\3.doc
对了,补充一下me.hwnd是以窗体句柄打开文件,这个参数设置为0,貌似以自身句柄打开文件.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不知道是不是你需要的答案
就在你代码上加了个事件,一个声明
复制代码进去就可以了!
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" _
(ByVal hWnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Dim fs, folder, subfolders, Files, i, j
Dim mStop As Boolean
Function digui(path)
If mStop = True Then Exit Function
Set folder = fs.getfolder(path)
Set subfolders = folder.subfolders
Set Files = folder.Files
For Each i In Files
If InStr(1, i.Name, Text1.Text, 1) Then
List1.AddItem i.path
End If
DoEvents
Next
For Each j In subfolders
digui (j.path)
Next
End Function
Private Sub Command1_Click()
If Command1.Caption = "搜寻" Then
Command1.Caption = "停止"
mStop = False
List1.Clear
Set fs = CreateObject("scripting.filesystemobject")
digui (Dir1.path)
If List1.ListCount = 1 Then
End If
Command1.Caption = "搜寻"
Else
Command1.Caption = "搜寻"
mStop = True
End If
End Sub
Private Sub Drive1_Change()
Dir1.path = Drive1.Drive
End Sub
Private Sub List1_DblClick()
Call ShellExecute(Me.hWnd, vbNullString, List1.Text, vbNullString, List1.Text, vbNormalFocus)
End Sub
这个代码唯一的缺点就是没有文件类型定义的无法打开!
如果觉得不好用,可以尝试
修改这个代码为:
Private Sub List1_DblClick()
Shell "explorer " & List1.Text, 1
End Sub
自己感觉就可以了!觉得行的话!就用吧!
就在你代码上加了个事件,一个声明
复制代码进去就可以了!
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" _
(ByVal hWnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Dim fs, folder, subfolders, Files, i, j
Dim mStop As Boolean
Function digui(path)
If mStop = True Then Exit Function
Set folder = fs.getfolder(path)
Set subfolders = folder.subfolders
Set Files = folder.Files
For Each i In Files
If InStr(1, i.Name, Text1.Text, 1) Then
List1.AddItem i.path
End If
DoEvents
Next
For Each j In subfolders
digui (j.path)
Next
End Function
Private Sub Command1_Click()
If Command1.Caption = "搜寻" Then
Command1.Caption = "停止"
mStop = False
List1.Clear
Set fs = CreateObject("scripting.filesystemobject")
digui (Dir1.path)
If List1.ListCount = 1 Then
End If
Command1.Caption = "搜寻"
Else
Command1.Caption = "搜寻"
mStop = True
End If
End Sub
Private Sub Drive1_Change()
Dir1.path = Drive1.Drive
End Sub
Private Sub List1_DblClick()
Call ShellExecute(Me.hWnd, vbNullString, List1.Text, vbNullString, List1.Text, vbNormalFocus)
End Sub
这个代码唯一的缺点就是没有文件类型定义的无法打开!
如果觉得不好用,可以尝试
修改这个代码为:
Private Sub List1_DblClick()
Shell "explorer " & List1.Text, 1
End Sub
自己感觉就可以了!觉得行的话!就用吧!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在上述代码的基础上,复制下面代码,即可实现你需要的功能!
Private Sub List1_DblClick() '
Dim S As String
Const Sh As String = "rundll32 url.dll,FileProtocolHandler ""#"""
S = List1.Text
Shell Replace(Sh, "#", S) '双击它打开文件或程序(所有注册类型的文件)
End Sub
Private Sub List1_DblClick() '
Dim S As String
Const Sh As String = "rundll32 url.dll,FileProtocolHandler ""#"""
S = List1.Text
Shell Replace(Sh, "#", S) '双击它打开文件或程序(所有注册类型的文件)
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
将文件夹中的文件列出,这个有多种办法,你可以引用FileObjectSystem或用Drive+Dir+File来实现,这个都不困难。关键你要在列表框打开选中的文件,这个有些问题,首先你的文件格式都不统一,打开方式也不一样,你可以根据文件扩展名来设置打开文件方式,不过写起来没那么容易。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
添加一个Dirve控件,Dri控件,和Film控件!在窗口中将会有三个控件、在Dri中编入:Dirve.path=Dri.dirve在Film中编入film.path=dri.divre!点F5运行。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询