📄 vb3.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0056)http://www.pconline.com.cn/experience/subject/VB/vb3.htm -->
<HTML><HEAD><TITLE>|><| 太平洋电脑信息网</TITLE><!-- #BeginTemplate "/Templates/0723.dwt" --><!-- #BeginEditable "doctitle" --><!-- #EndEditable -->
<META content="text/html; charset=gb2312" http-equiv=Content-Type><LINK
href="vb3.files/text.css" rel=stylesheet>
<STYLE type=text/css>.sfont {
LINE-HEIGHT: 18pt
}
</STYLE>
<META content="MSHTML 5.00.2014.210" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff leftMargin=0 topMargin=0>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width="100%">
<TBODY>
<TR>
<TD vAlign=top width="100%">
<TABLE border=0 cellPadding=0 cellSpacing=0 width="100%">
<TBODY>
<TR bgColor=#303880>
<TD><A href="http://www.pconline.com.cn/"><IMG alt=太平洋电脑信息网 border=0
height=18 hspace=30 src="vb3.files/txtpconline.gif"
width=96></A><IMG height=10 src="vb3.files/blank.gif"
width=400></TD></TR></TBODY></TABLE></TD>
<TD align=right background=vb3.files/bw.gif noWrap width=130><IMG
height=26 src="vb3.files/bw.gif" width=10><A
href="http://www.pconline.com.cn/sitemap/"><IMG alt=网站地图 border=0
height=26 src="vb3.files/sitemap.gif" width=21></A><IMG height=26
src="vb3.files/bw.gif" width=10><A
href="http://www.pconline.com.cn/tpy/"><IMG alt=关于太平洋 border=0 height=26
src="vb3.files/aboutus.gif" width=21></A><IMG height=26
src="vb3.files/bw.gif" width=10><A
href="http://www.pconline.com.cn/tpy/tariff.html"><IMG alt=与我们联系 border=0
height=26 src="vb3.files/contact.gif" width=21></A><IMG height=26
src="vb3.files/bw.gif" width=10></TD></TR></TBODY></TABLE><BR>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width=760>
<TBODY>
<TR>
<TD bgColor=#666666><IMG height=1 src="vb3.files/blank.gif" width=1></TD>
<TD bgColor=#666666><IMG height=1 src="vb3.files/blank.gif" width=1></TD>
<TD> </TD>
<TD colSpan=2 noWrap><A
href="http://www.pconline.com.cn/experience/index.html"><IMG
align=absBottom alt=企业之窗 border=0 height=15
src="vb3.files/txtexperience.gif" width=68></A> <A
href="http://www.pconline.com.cn/">首 页</A> <A
href="http://www.pconline.com.cn/market/">今日报价</A> <A
href="http://www.pconline.com.cn/recommend/">产品推介</A> <A
href="http://www.pconline.com.cn/yp/">企业之窗</A> <A
href="http://www.pconline.com.cn/news/">科技新闻</A> <A
href="http://www.pconline.com.cn/product/">产品查询</A> <A
href="http://www.pconline.com.cn/download/">软件下载</A></TD></TR>
<TR>
<TD><IMG height=1 src="vb3.files/blank.gif" width=1></TD>
<TD><IMG height=1 src="vb3.files/blank.gif" width=1></TD>
<TD><IMG height=1 src="vb3.files/blank.gif" width=21></TD>
<TD bgColor=#ffa000 colSpan=2><IMG height=1 src="vb3.files/blank.gif"
width=1></TD></TR>
<TR>
<TD bgColor=#666666><IMG height=1 src="vb3.files/blank.gif" width=1></TD>
<TD bgColor=#cdcdcd><IMG height=5 src="vb3.files/blank.gif" width=1></TD>
<TD align=right colSpan=3><IMG height=6 src="vb3.files/ffa000.gif"
width=1></TD></TR>
<TR>
<TD bgColor=#666666><IMG height=1 src="vb3.files/blank.gif" width=1></TD>
<TD align=right><A href="http://www.pconline.com.cn/"><IMG alt=太平洋电脑信息网
border=0 height=35 src="vb3.files/pconline.com.cn.gif" vspace=10
width=162></A></TD>
<TD> </TD>
<TD vAlign=bottom><!-- #BeginEditable "important_news" --><IMG height=40
src="vb3.files/blank.gif" width=350><!-- #EndEditable --></TD>
<TD align=right vAlign=top><!-- #BeginEditable "adv_top" --><IMG alt=广告位
height=43 src="vb3.files/icon.gif"
width=190><!-- #EndEditable --></TD></TR></TBODY></TABLE>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width=760>
<TBODY>
<TR>
<TD bgColor=#666666 width=1><IMG height=1 src="vb3.files/blank.gif"
width=1></TD>
<TD vAlign=top width=170><!-- #BeginEditable "addons" --><!--替代为附加信息 --><!-- #EndEditable --></TD>
<TD bgColor=#666666 width=1><IMG height=1 src="vb3.files/blank.gif"
width=1></TD>
<TD><IMG height=1 src="vb3.files/blank.gif" width=21></TD>
<TD colSpan=3 vAlign=top width=566><!-- #BeginEditable "1" --><IMG
height=1 src="vb3.files/blank.gif" width=566><BR><IMG height=1
src="vb3.files/666666.gif" width=420><BR><IMG height=13
src="vb3.files/blank.gif" width=1><A
href="http://www.pconline.com.cn/experience/">经验共享</A> > <SPAN
class=greenb><A
href="http://www.pconline.com.cn/experience/subject/VB/index.html">专题教程</A></SPAN><A
href="http://www.pconline.com.cn/experience/subject/VB/index.html">
VB专区</A>> <BR><IMG height=1 src="vb3.files/666666.gif" vspace=1
width=420><BR>
<P align=center><B><FONT color=#000066><SPAN
class=sfont>VB查找文件的两种方法</SPAN></FONT></B><SPAN class=sfont><BR></SPAN></P>
<P align=center><SPAN class=sfont>方云波 </SPAN></P>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
VB开发人员都会遇到文件定位的问题。VB提供的Dir[(pathname[,
attributes])]函数应该可以满足各种文件定位问题。使用Dir函数时,我们必须给它提供文件的全部路径,否则是找不到的。而有时我们往往是不知道文件的路径的,如果要利用该函数来定位文件,我们必须编写一定的代码。笔者在利用该函数编制查找文件的函数时,颇费了些工夫,可是发现查找的效率不高,效果不太理想。那么有没有编码少且更快的方法查找文件呢?笔者后来利用一个
API函数成功地编制了一个高效率的查找文件的函数。下面一一介绍给大家,起个抛砖引玉的作用,期望大家能编制出更高效的函数。 </SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT> <B>一. 利用DIR函数查找文件</B>
</SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT> Dir[(pathname[,
attributes])]是VB提供的用来检查某些文件或目录是否存在的函数,它返回一个
String,用以表示一个文件名、目录名或文件夹名称,返回值必须与指定的模式或文件属性、或磁盘卷标相匹配。 </SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
如果文件的路径很清楚,那么确定文件是否存在简单地调用该函数就行了。如果光知道文件名,甚或只知道文件的后缀,要定位文件的话就需要一定的编码了。下面的例子用来定位c:\下所有目录内是否有文件Win.ini。
</SPAN><PRE><SPAN class=sfont>
Function FindDirectory(RootPath As
String, Mydirectory() As String)
Dim intResult, I, intFind As Integer
‘首先查找根目录下的所有子目录
MyPath = "c:\" ' 指定路径c:\。
MyName = Dir(MyPath, vbDirectory) ' 找寻第一项。
intResult = 1
ReDim Mydirectory(intResult) ‘初始化动态数组
Do While MyName < > "" ' 开始循环。
' 跳过当前的目录及上层目录。
If MyName < > "." And MyName < > ".." Then
' 使用位比较来确定 MyName 代表一目录。
If (GetAttr(MyPath & MyName)
And vbDirectory) = vbDirectory Then
' 如果它是一个目录,将其名称存储在一个数组里。
Mydirectory(intResult) = MyPath & MyName
intResult = intResult + 1
ReDim Preserve Mydirectory(intResult)
‘分配动态数组实际的元素个数,并保留数组中的数据
End If
End If
MyName = Dir ' 查找下一个目录。
Loop
‘在所有目录里分别查找文件是否存在。
For I = 1 To UBound(Mydirectory)-1
MyFile = Mydirectory(I) & "\win.ini"
intFind = Len(Dir(MyFile))
If intFind < > 0 Then MsgBox "找到文件" &
Dir(MyFile) & "在:" & Mydirectory(I)
Next I
End Function
</SPAN></PRE>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
该函数的思路很清晰:先遍历根目录下所有的子目录,然后在每个子目录里查找文件。该函数有一个缺陷:只能查找根目录下的一级子目录,无法遍及整个硬盘。如果要遍及整个硬盘,还需要额外的编码,这里不再多述。
</SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT> <B>二. 利用API函数查找文件</B>
</SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
在使用VB的过程中我深深地体会到,只有充分利用API的函数才能更充分地发挥VB的优势。 API函数
SearchTreeFile可以很快地定位一个文件。借助该函数笔者编了一个快速查找文件的函数sysFileFind. </SPAN><PRE><SPAN class=sfont>
Declare
Public Declare Function SearchTreeForFile Lib
"imagehlp.dll" (ByVal lpRoothPath As String,
ByVal lpInputName As String,
ByVal lpOutputName As String) As Long
</SPAN></PRE>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 下面为sysFileFind函数的编码:
</SPAN><PRE><SPAN class=sfont>
Public Function sysFileFind
(ByVal WhichRootPath As String,
ByVal WhichFileName As String) As String
Dim iNull As Integer
Dim lResult As Long
Dim sBuffer As String
On Error GoTo L_FILEFINDERROR
sBuffer = String$(1024, 0)
'查找文件
lResult = SearchTreeForFile
(WhichRootPath, WhichFileName, sBuffer)
'如果文件找到,将返回字符串后续的空格删除
'否则返回一个空字符串
If lResult Then
iNull = InStr(sBuffer, vbNullChar)
If Not iNull Then
sBuffer = Left$(sBuffer, iNull - 1)
End If
sysFileFind = sBuffer
Else
sysFileFind = ""
End If
Exit Function
L_FILEFINDERROR:
MsgBox "查找文件过程中遇到错误!",
vbInformation, "查找文件错误"
sysFileFind = Format(Err.Number)
& " - " & Err.Description
End Function
</SPAN></PRE>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
该函数可以很快遍历整个硬盘,从而查找到我们所需的文件。 </SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT> <B>三. 总结</B> </SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
上面两个函数都在中文VB5和Win98环境下调试通过。我们可以看到第二种方法编码更简单,效率更高。 </SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
VB5强大的功能赢得了越来越多开发人员的青睐。如果适当地利用API函数,我们可以说,利用VB可以轻松地完成我们开发中的所有任务。</SPAN>
<P align=right><A
href="http://www.pconline.com.cn/experience/subject/VB/index.html">----更多的文章</A>
<!-- #EndEditable --></P></TD></TR></TBODY></TABLE>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width=760>
<TBODY>
<TR>
<TD bgColor=#666666 width=1><IMG height=1 src="vb3.files/blank.gif"
width=1></TD>
<TD align=right vAlign=bottom width=170><IMG height=10
src="vb3.files/blank.gif" width=170><BR></TD>
<TD bgColor=#666666 width=1><IMG height=1 src="vb3.files/blank.gif"
width=1></TD>
<TD width=21><IMG height=1 src="vb3.files/blank.gif" width=21></TD>
<TD bgColor=#eeeeee width=566><IMG height=10 src="vb3.files/blank.gif"
width=566><BR>版权所有©1999 太平洋电脑信息网<BR><FONT
face="Arial, Helvetica, sans-serif"><A
href="mailto:exp@pconline.com.cn">exp@pconline.com.cn</A></FONT> <IMG
height=1 src="vb3.files/hhvisit.html" width=1> </TD></TR></TBODY></TABLE><!-- #EndTemplate --></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -