📄 vbtips08.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=gb_2312-80">
<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
<title>vbtips08</title>
</head>
<body>
<h1 align="center"><a name="home"></a>VB技巧(8)</h1>
<blockquote>
<p><strong>1、</strong><a href="#tips00"><strong>从全路径名中提取文件名</strong></a><strong><br>
2、</strong><a href="#tips01"><strong>把文件置入到Text或RichText中</strong></a><strong><br>
3、</strong><a href="#tips02"><strong>目录所占的字节数</strong></a><strong><br>
4、</strong><a href="#tips03"><strong>打开 Win95
的创建快捷方式窗口</strong></a><strong><br>
5、</strong><a href="#tips04"><strong>显示盘中所有的目录</strong></a><strong><br>
6、</strong><a href="#tips05"><strong>取得长文件名</strong></a></p>
<div align="center"><center><table border="0" cellspacing="1"
width="88%">
<tr>
<td width="80%"><p align="left"><a
href="vbtips.htm#Return">[1]</a> <a
href="vbtips1.htm">[2]</a> <a href="vbtips2.htm">[3]</a>
<a href="vbtips3.htm">[4]</a> <a href="vbtips4.htm">[5]</a>
<a href="vbtips5.htm">[6]</a> <a href="vbtips7.htm">[7]</a>
[8] <a href="vbtips9.htm">[9]</a> <a
href="vbtips10.htm">[10]</a></p>
</td>
<td><p align="right"><font size="2">第八页(共十页)</font></p>
</td>
</tr>
</table>
</center></div>
</blockquote>
<hr>
<div align="center"><center>
<table border="0" cellspacing="1" width="88%">
<tr>
<td width="100%"><a name="tips00"></a><strong>从全路径名中提取文件名</strong><br>
Function StripPath(T$) As String<br>
Dim x%, ct%<br>
StripPath$ = T$<br>
x% = InStr(T$, "\")<br>
Do While x%<br>
ct% = x%<br>
x% = InStr(ct% + 1, T$, "\")<br>
Loop<br>
If ct% > 0 Then StripPath$ = Mid$(T$, ct% + 1)<br>
End Function<br>
例子:<br>
File = StripPath("c:\windows\hello.txt")<br>
<a href="#home">返回</a><p><a name="tips01"></a><strong>把文件置入到Text或RichText中</strong><br>
dim sFile as string<br>
'Set sFile equal to your filename<br>
dim i as long<br>
<br>
i = freefile()<br>
<br>
open sFile for input as #i<br>
txtMain.text = input$(i,LOF(i))<br>
close #1<br>
<a href="#home">返回</a></p>
<p><a name="tips02"></a><strong>目录所占的字节数</strong><br>
该函数返回目录使用的字节数:<br>
<br>
Function DirUsedBytes(ByVal dirName As String) As Long<br>
Dim FileName As String<br>
Dim FileSize As Currency<br>
If Right$(dirName, 1) <> "\" Then<br>
dirName = dirName & "\" <br>
Endif<br>
FileSize = 0<br>
FileName = Dir$(dirName & "*.*")<br>
Do While FileName <> ""<br>
FileSize = FileSize + _<br>
FileLen(dirName & FileName)<br>
FileName = Dir$<br>
Loop<br>
DirUsedBytes = FileSize<br>
使用:<br>
MsgBox DirUsedBytes(<code>"C:\Windows"</code>)<br>
<a href="#home">返回</a></p>
<p><a name="tips03"></a><strong>打开 Win95
的创建快捷方式窗口</strong><br>
以下的代码演示了如何利用 Win95 的 Wizard
在指定的目录中建立快捷方式。<br>
<br>
Dim X As Integer<br>
X = Shell("C:\WINDOWS\rundll32.exe
AppWiz.Cpl,NewLinkHere " & App.Path &
"\", 1)<br>
<a href="#home">返回</a></p>
<p><a name="tips04"></a><strong>显示盘中所有的目录</strong><br>
以下的代码把盘中所有的目录都显示在DriveListBox
和一个Listbox 中。需要一个 如果
DirListBox 隐藏的话,处理可以快一些。<br>
<br>
Dim iLevel As Integer, iMaxSize As Integer<br>
Dim i As Integer, j As Integer<br>
ReDim iDirCount(22) As Integer <br>
'最大 22 级目录<br>
ReDim sdirs(22, 1) As String<br>
'drive1 是 DriveListBox 控件<br>
'dir1 是 DirListBox 控件<br>
iLevel = 1<br>
iDirCount(iLevel) = 1<br>
iMaxSize = 1<br>
sdirs(iLevel, iDirCount(iLevel)) =
Left$(drive1.Drive, 2) & "\"<br>
Do <br>
iLevel = iLevel + 1<br>
iDirCount(iLevel) = 0<br>
For j = 1 To iDirCount(iLevel - 1)<br>
dir1.Path = sdirs(iLevel - 1, j)<br>
dir1.Refresh<br>
If iMaxSize < (iDirCount(iLevel) +
dir1.ListCount) Then<br>
ReDim Preserve
sdirs(22, iMaxSize + dir1.ListCount + 1) As String<br>
iMaxSize =
dir1.ListCount + iDirCount(iLevel) + 1<br>
End If<br>
For i = 0 To dir1.ListCount - 1<br>
iDirCount(iLevel) = _<br>
iDirCount(iLevel) + 1 '子目录记数<br>
sdirs(iLevel,
iDirCount(iLevel)) = dir1.List(i)<br>
Next i<br>
Next j<br>
'所有名称放到 List1 中<br>
list1.Clear<br>
If iDirCount(iLevel) = 0 Then<br>
'如果无自目录<br>
For i = 1 To iLevel<br>
For j = 1 To
iDirCount(i)<br>
list1.AddItem sdirs(i, j)<br>
Next j<br>
Next i<br>
Exit Do<br>
End If<br>
Loop <br>
<a href="#home">返回</a></p>
<p><a name="tips05"></a><strong>取得长文件名</strong><br>
Public Function GetLongFilename (ByVal sShortName As
String) As String<br>
<br>
Dim sLongName As String<br>
Dim sTemp As String<br>
Dim iSlashPos As Integer<br>
<br>
'Add \ to short name to prevent Instr from failing<br>
sShortName = sShortName & "\"<br>
<br>
'Start from 4 to ignore the "[Drive Letter]:\"
characters<br>
iSlashPos = InStr(4, sShortName, "\")<br>
<br>
'Pull out each string between \ character for conversion<br>
While iSlashPos<br>
sTemp = Dir(Left$(sShortName, iSlashPos - 1), _<br>
vbNormal + vbHidden + vbSystem + vbDirectory)<br>
If sTemp = "" Then<br>
'Error 52 - Bad File Name or Number<br>
GetLongFilename = ""<br>
Exit Function<br>
End If<br>
sLongName = sLongName & "\" & sTemp<br>
iSlashPos = InStr(iSlashPos + 1, sShortName,
"\")<br>
Wend<br>
<br>
'Prefix with the drive letter<br>
GetLongFilename = Left$(sShortName, 2) & sLongName<br>
<br>
End Function<br>
<a href="#home">返回</a></p>
</td>
</tr>
</table>
</center></div>
<hr>
<div align="center"><center>
<table border="0" cellspacing="1" width="88%">
<tr>
<td width="80%"><p align="left"><a
href="vbtips.htm#Return">[1]</a> <a href="vbtips1.htm">[2]</a>
<a href="vbtips2.htm">[3]</a> <a href="vbtips3.htm">[4]</a>
<a href="vbtips4.htm">[5]</a> <a href="vbtips5.htm">[6]</a>
<a href="vbtips7.htm">[7]</a> [8] <a href="vbtips9.htm">[9]</a>
<a href="vbtips10.htm">[10]</a></p>
</td>
<td><p align="right"><font size="2">第八页(共十页)</font></p>
</td>
</tr>
</table>
</center></div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -