導航:首頁 > 文件教程 > vb監控文件

vb監控文件

發布時間:2024-07-01 21:18:01

⑴ 鐢╲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上試過了。

閱讀全文

與vb監控文件相關的資料

熱點內容
湘潭編程學校哪裡好 瀏覽:389
win10設置桌面小圖標怎麼去掉嗎 瀏覽:122
網路安全創業 瀏覽:787
修改linux 瀏覽:464
如何編程計算機cpu高佔用程序 瀏覽:808
程序員活動策劃方案 瀏覽:130
數據漫遊什麼意思需不需要開啟 瀏覽:804
qq圖片刷新很慢 瀏覽:40
數據的採集方法都有什麼 瀏覽:401
pps影音iphone版282 瀏覽:214
影梭安卓客戶端341 瀏覽:636
有網路為什麼qq登不上去 瀏覽:87
視頻插上不顯示視頻文件 瀏覽:665
投標製作軟體生成文件失敗 瀏覽:756
什麼是數據指標 瀏覽:243
三菱plc編程用什麼語言比較好 瀏覽:496
路虎找不到配置文件怎麼辦 瀏覽:447
linux打包jar 瀏覽:556
好看的word文檔排版樣式 瀏覽:331
英朗gt高德導航升級 瀏覽:910

友情鏈接