⑴ 鐢╲b鐩戞帶鏌愪釜鏂囦欢澶逛笅鐨勬枃浠舵槸鍚︽湁鏇存敼锛屽傛灉鍑虹幇浜嗘洿鏀癸紝灏嗘洿鏀规枃浠剁殑鏂囦欢鍚嶈板綍鍒拌颁簨鏈閲岋紝濡備綍瀹炵幇锛
瀵癸紝鑾峰彇鐩褰曚笅鎵鏈夋枃浠讹紝鑾峰彇MD5鍊硷紝淇濆瓨銆備竴娈垫椂闂村悗锛屽啀娆¤繃鍘籑D5鍊硷紝濡傛灉涓嶄竴鏍凤紝灏嗘枃浠跺悕鍐欏叆鏂囦欢锛岀粨鏉
⑵ 如何对文件进行监控 vb
监视程序,它的用途主要是在后台监视系统中关键信息的改变,比如注册表的改变及硬盘上由于文件操作引起的改变等等。下面我们开始讨论如何编程实现这一监视功能。首先介绍几个重要的api函数:
FindFirstChangeNotification( );
FindNextChangeNotification( );
WaitForSingleObject( );
其中FindFirstChangeNotification(lpzpath,fwatchsubtree,fdwfilter)中的 lpzpath表示要监视的路径名,fwatchsubtree判断是否查看子目录,fdwfilter为要监视的事件,函数执行成功后返回一个句柄。
参数fdwfilter取值及其含义如下:
FILE_NOTIFY_CHANGE_FILE_NAME 查看指定目录下任何文件名的改变
FILE_NOTIFY_CHANGE_DIR_NAME 查看指定目录下任何目录名的改变
FILE_NOTIFY_CHANGE_SIZE 查看指定目录下文件大小的改变
FILE_NOTIFY_CHANGE_ATTRIBUTES 查看指定目录下文件属性的改变
FindNextChangeNotification(hchange),hchange为 FindFirstChangenNotification返回的句柄,其作用是请求系统在下次检测到相应改变时发出改变通知消息句柄。当函数成功返回后,应用程序可通过WaitForMultipleObjects或WaitfForSingleObject来等待发生改变的通知。 WaitForSingleObject(hchange,dwmilliseconds)中hchange为 FindFirstChangeNotification 返回的句柄,dwmilliseconds为等待时间值,指定等待完成需要的时间,单位为毫秒。该值为-1时表示时间无限长。最好在结束监视程序之前先用FindCloseChangeNotification(hchange)来关闭句柄。
下面给出一个简单的实例,其功能就是监视c:\pwin98目录下是否有文件发生变化。一旦有重命名、创建或删除情况发生时,通过Edit控件给出提示。
DWORD dwWaitStatus;
HANDLE dwChangeHandle; //返回通知的句柄
dwChangeHandle=FindFirstChangeNotification(
“C:\\PWIN98”,false,FILE_NOTIFY_CHANGE_FILE_NAME); //设置返回通知的句柄
if(dwChangeHandle==INVALID_HANDLE_VALUE)
//判断是否设置成功
ExitProcess(GetLastError());
while(true){ //设置循环,监视是否有
dwWaitStatus=WaitForSingleObject(dwChangeHandle,-1); //通知返回
if(dwWaitStatus==0){
MessageBox(NULL,"Notification",“Something Changed”,MB_OK); //给出提示
FindCloseChangeNotification(dwcChangeHandle); //关闭句柄
exit(EXIT_SUCCESS); //退出程序
}
}
此例说明如何监视硬盘中文件变化,对于注册表,则有函数RegNotifyChangeKeyValue()可以实现类似功能。
Option Explicit
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'-------------------------------------------------------
Private Const FILE_ATTRIBUTE_COMPRESSED = &H800
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const MAX_PATH = 260
Private Const INVALID_HANDLE_VALUE = -1
'-------------------------------------------------------
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
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
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
'-------------------------------------------------------
Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
'-------------------------------------------------------
Private mlngFile As Long
Private mstrDateFormat As String
Private mstrUnknownDateText As String
Private mwfdFindData As WIN32_FIND_DATA
'-------------------------------------------------------
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'========================================================
Public Property Let DateFormat(strDateFormat As String)
mstrDateFormat = strDateFormat
End Property
'========================================================
'========================================================
Public Property Let UnknownDateText(strUnknownDateText As String)
mstrUnknownDateText = strUnknownDateText
End Property
'========================================================
'========================================================
Public Property Get FileAttributes() As Long
If mlngFile Then FileAttributes = mwfdFindData.dwFileAttributes
End Property
'========================================================
'========================================================
Public Property Get IsCompressed() As Boolean
If mlngFile Then IsCompressed = mwfdFindData.dwFileAttributes _
And FILE_ATTRIBUTE_COMPRESSED
End Property
'========================================================
'========================================================
Public Property Get NormalAttribute() As Long
NormalAttribute = FILE_ATTRIBUTE_NORMAL
End Property
'========================================================
'========================================================
Public Function Find(strFile As String, _
Optional blnShowError As Boolean) As String
If mlngFile Then
If blnShowError Then
If MsgBox("Cancel the current search?", vbYesNo Or _
vbQuestion) = vbNo Then Exit Function
End If
EndFind
End If
mlngFile = FindFirstFile(strFile, mwfdFindData)
If mlngFile = INVALID_HANDLE_VALUE Then
mlngFile = 0
If blnShowError Then
MsgBox strFile & "counld not be found!", vbExclamation
Else
'Err.Raise vbObjectError + 5000, "clsFindFile_Find", _
strFile & "counld not be found!"
End If
Exit Function
End If
Find = Left(mwfdFindData.cFileName, _
InStr(mwfdFindData.cFileName, Chr(0)) - 1)
End Function
'========================================================
'========================================================
Public Function FindNext() As String
If mlngFile = 0 Then Exit Function
mwfdFindData.cFileName = Space(MAX_PATH)
If FindNextFile(mlngFile, mwfdFindData) Then
FindNext = Left(mwfdFindData.cFileName, _
InStr(mwfdFindData.cFileName, Chr(0)) - 1)
Else
EndFind
End If
End Function
'========================================================
'========================================================
Private Sub EndFind()
FindClose mlngFile
mlngFile = 0
End Sub
'========================================================
'========================================================
Public Function GetShortName() As String
Dim strShortFileName As String
If mlngFile = 0 Then Exit Function
strShortFileName = Left(mwfdFindData.cFileName, _
InStr(mwfdFindData.cAlternate, Chr(0)) - 1)
If Len(strShortFileName) = 0 Then
strShortFileName = Left(mwfdFindData.cFileName, _
InStr(mwfdFindData.cFileName, Chr(0)) - 1)
End If
GetShortName = strShortFileName
End Function
'========================================================
'========================================================
Public Function GetCreationDate(Optional datDate As Date, _
Optional datTime As Date) As String
If mlngFile = 0 Then Exit Function
If mwfdFindData.ftCreationTime.dwHighDateTime = 0 Then
GetCreationDate = mstrUnknownDateText
End If
With GetSystemTime(mwfdFindData.ftCreationTime)
datDate = DateSerial(.wYear, .wMonth, .wDay)
datTime = TimeSerial(.wHour, .wMinute, .wSecond)
GetCreationDate = Format(datDate + datTime, mstrDateFormat)
End With
End Function
'========================================================
⑶ 用VB怎么监控文件夹是否有新的文件生成
我自创的方法。考虑到实时检测是否有新文件,应该用一个Timer。
画两个FileListBox控件,控件名分别为File1、File2;画一个TextBox,名为Text1;画一个Timer,名为Timer1。要求将目标文件夹新生成的Txt文件内容显示到Text1中。
Private Sub Form_Load()
File1.Path = "在引号里打上要监测的路径。如果路径不是某个盘的根目录则不要在最后一个文件夹名的后面打上“\”"
File2.Path = File1.Path
File1.Pattern = "*.txt"
File2.Pattern = "*.txt"
File1.Visible = False
File2.Visible = False
Timer1.Interval = 在等号后面打上一个整数用来确定扫描新文件的频率,单位是毫秒,最低值是1
End Sub
Private Sub Timer1_Timer()
File2.Refresh
If File2.ListCount > File1.ListCount Then
If File1.ListCount = 0 Then
File2.ListIndex = 0
Else
i = 0
File1.ListIndex = i
File2.ListIndex = i
Do While File2.FileName = File1.FileName And i <= File1.ListCount - 1
File1.ListIndex = i
File2.ListIndex = i
i = i + 1
Loop
If i = File1.ListCount - 1 Then
File2.ListIndex = i - 1
Else
File2.ListIndex = i
End If
End If
url = File2.Path
If Right(url, 1) <> "\" Then
url = url & "\"
End If
url = url & File2.FileName
Open url For Input As #1
Text1.Text = StrConv(InputB(LOF(1), #1), vbUnicode)
Close #1
File1.Refresh
End If
If File2.ListCount < File1.ListCount Then
File1.Refresh
End If
End Sub
绝对好使,我在VB上试过了。