居裡貓今天要來講講第三集,小程式的第三集啊!
先前分享了選擇路徑跟寫 .xml 檔案,
那我們寫好了檔案到 Local Folder 裡面,要怎麼讓他去到其他資料夾呢?
總不是在寫幾個 Button 去寫檔案吧!!!
又要在什麼時候,讓這個寫好的檔案去到其他資料夾呢!!?
好的~
那麼講到什麼時候去,就表示我們得知道他什麼時候寫了一個檔案對吧!
那要知道什麼時候寫檔案,那我們就要像眼線一樣在那邊監控他嘛!
這樣我們才會知道它在什麼時候寫了檔案!
這就是今天的主題監控資料夾~~的原因!
這次其實也沒什麼圖片可以看,那就直接上程式碼吧!!!
----------------------------------------------------------------------------------------------------------------
- '這裡很直接的就是要在程式開啟的那個時間開始就要監控了
- '因為檔案有可能就在開啟之後馬上寫檔案麻~~
- Private Sub UploadReport_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- 'At program started to run monitor folder programming
- Watcher.Main(TxB_Local.Text)
- back.WorkerSupportsCancellation = True
- back.WorkerReportsProgress = True
- AddHandler back.DoWork, AddressOf back_DoWork
- End Sub
- '---我是分割線,上面是當程式Load的時候做的事,下面才是監控的主要 Class
- 'About Monitor Foldaer
- Public Class Watcher
- Public Shared Sub Main(path As String)
- '"path" was monitored folder file path
- Run(path)
- End Sub
- Public Shared watcher As New FileSystemWatcher()
- Private Shared Sub Run(p As String)
- 'Create new FileSystemWatcher and set path
- watcher.Path = p
- 'Only watch XML files
- watcher.Filter = "*.xml"
- 'AddHandler watcher.Changed, AddressOf OnChanged
- AddHandler watcher.Created, AddressOf OnCreated
- 'AddHandler watcher.Deleted, AddressOf OnDeleted
- 'AddHandler watcher.Renamed, AddressOf OnRenamed
- 'Begin watching
- watcher.EnableRaisingEvents = True
- End Sub
- '這邊下面的內容包含了一些其他功能的地方請暫時忽略他們吧~______
- Private Shared Sub OnCreated(source As Object, e As FileSystemEventArgs)
- 'When file was created
- Console.WriteLine("File : " & e.FullPath & " _ " & e.ChangeType)
- 'When into here and then get now file name
- Copy_Delete_File.GetFileName(e.Name)
- System.Threading.Thread.Sleep(500)
- 'Wait few second then do copyfile work
- Copy_Delete_File.copyfile()
- End Sub
- Private Shared Sub OnChanged(source As Object, e As FileSystemEventArgs)
- 'When file was changed
- End Sub
- Private Shared Sub OnDeleted(source As Object, e As FileSystemEventArgs)
- 'When file was deleted
- End Sub
- Private Shared Sub OnRenamed(source As Object, e As RenamedEventArgs)
- 'When file was renmaed
- Console.WriteLine("File : {0} Renamed to {1}", e.OldFullPath, e.FullPath)
- Copy_Delete_File.GetFileName(e.Name)
- System.Threading.Thread.Sleep(100)
- Copy_Delete_File.copyfile()
- End Sub
- End Class
---------------------------------------------------------------------------------------------------------------------
今天分享的就是一個監控資料夾的程式段落,
那居裡貓在監控程式段落中寫了一些其他功能,
就是希望在監控的時候發生事件就去執行別的功能這樣~~~~
希望今天的分享大家還覺得喜歡!
也感謝各位先進的教學文~
底下就是這個
1. 上報 練習 _ 選擇資料夾
2. 上報 練習 _ 寫一個 .XML 資料
3. 上報 練習 _ 監控資料夾
4. 上報 練習 _ 複製檔案
5. 上報 練習 _ FTP Server 檔案上傳
6. 上報 練習 _ 上傳錯誤紀錄
7. 上報 練習 _ 使用 BackgroundWorker 再次上傳
8. 上報 練習 _ Log 紀錄
9. 上報 練習 _ 完整程式碼分享
沒有留言:
張貼留言