居裡貓今天講講第四集要做什麼!
就是當我們監控完資料夾之後,一旦資料夾出現了檔案那我們該做什麼!!!?
那該做什麼我們也來回顧一下前情提要,就可以明白的一二了!
當然是要把檔案複製到其他路徑去啊!!!
好!第四集就是複製檔案到其他的路徑~~
開始程式碼吧!
等等!!!!!!!!!!!!!!!
我還是先說說流程大概是什麼吧!
首先是,我們在監控的資料夾出現了一個檔案,
因為出現的這個檔案,所以必須把它複製到其他路徑,
包含 Remote Folder 、NAS Folder 或者是 FTP Folder,
這裡提醒各位一下,居裡貓這邊有做一個 CheckBox ,
居裡貓用它來判斷我們今天是要上傳到FTP還是NAS,所以這個部分要稍微注意一下才好。
正式開始!上程式碼~
-------------------------------------------------------------------------------------------------------------------------
- '這裡大家會好奇怎麼又出現 "Write XML" 按鈕了
- '因為居裡貓利用這個按鈕來取得表單上的資訊,包含 CheckBox 的資訊
- 'Button Contorl XML Write
- Private Sub Btn_WriteXML_Click(sender As Object, e As EventArgs) Handles Btn_WriteXML.Click
- 'Check watch event was raising
- If Watcher.watcher.EnableRaisingEvents = False Then
- Watcher.watcher.EnableRaisingEvents = True
- End If
- '在下面這一行取得表單資訊_______________
- Copy_Delete_File.GetPath(TxB_Local.Text, TxB_Remote.Text, TxB_NAS.Text, TxB_FTP.Text, ChB_UploadFTP.Checked)
- FTPupload.Get_FTPinfo(TxB_FTPAddress.Text, TxB_FTPAccount.Text, Txb_FTPPassword.Text)
- Dim wrXML As New XML
- wrXML.LocalPath = TxB_Local.Text
- wrXML.RemotePath = TxB_Remote.Text
- wrXML.NASPath = TxB_NAS.Text
- wrXML.FTPPath = TxB_FTP.Text
- wrXML.Data = "This is a Programming Practices " & Now.ToString
- wrXML.DateTime = Now.ToString
- If ChB_UploadFTP.Checked Then
- wrXML.CheckFTP = "Ture"
- Else
- wrXML.CheckFTP = "Flase"
- End If
- Dim objectStreamWriter As New StreamWriter(TxB_Local.Text + ("\Program Practices " & Now.ToString("_yyyyMMddHHmmss") & ".xml"))
- Dim xmlserialize As New XmlSerializer(wrXML.GetType)
- xmlserialize.Serialize(objectStreamWriter, wrXML)
- objectStreamWriter.Dispose()
- 'Check backwork status
- 'If Not back.IsBusy = True Then
- ' back.RunWorkerAsync()
- 'End If
- Backwork.main()
- End Sub
- '---分割線,上面用來取得表單資訊,下面則是啟動複製檔案的工作
- '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
- '-----分割線,下面才是複製檔案的 Class 部分
- 'About Copy and Delete file
- Public Class Copy_Delete_File
- 'Parameter use for down below work
- Public Shared filename As String 'use get file name
- Public Shared sourcepath As String 'use get soure file path
- Public Shared copypath As String 'use get copy file path
- Public Shared uptonas As String 'use get upload to nas path
- Public Shared uptoftp As String 'use get upload to ftp path
- Public Shared checkbox As Boolean 'use get checkbox condition
- '居裡貓這邊寫了兩個程式是專門用來取得需要的資訊
- 'Get now file name
- Public Shared Sub GetFileName(Name As String)
- Console.WriteLine(Name & " Get")
- filename = Name
- End Sub
- 'Get path to use copy file, delete file, upload file
- Public Shared Sub GetPath(source As String, copyformpath As String, upNASpath As String, upFTPpath As String, check As Boolean)
- Console.WriteLine(source & vbCr & copyformpath & vbCr & upNASpath & vbCr & upFTPpath)
- sourcepath = source
- copypath = copyformpath
- uptonas = upNASpath
- uptoftp = upFTPpath
- checkbox = check
- End Sub
- '這裡才是真正開始複製檔案
- 'Do copy file to remote folder, upload file to NAS folder and FTP folder then deleted local file
- Public Shared Sub copyfile()
- Try
- '這邊先把檔案複製到 Remote Folder 做備份
- 'Copy file to remote folder from local folder
- File.Copy(sourcepath + "\" + filename, copypath + "\" + filename, True)
- 'Control retry parameter
- Dim Retry As Boolean = True
- Dim retrytimes As Integer = 0
- '這裡要先確認今天勾選的是上傳到 NAS 還是 FTP
- 'Check upload to NAS or FTP
- If checkbox = False Then
- While Retry
- '防止上傳有一些問題,做了一個重新上傳的感覺
- '上傳的檔案是備份的資料夾取得的
- Try
- 'Upload to NAS, copy file from remote folder, upload done delete loacal file
- File.Copy(copypath + "\" + filename, uptonas + "\" + filename, True)
- System.Threading.Thread.Sleep(100)
- Retry = False
- Catch ex As Exception
- 'Upload to NAS Error, do retry upload file and count retry times
- If retrytimes <= 10 Then
- Retry = True
- retrytimes += 1
- 'Console.WriteLine("Upload to NAS was Error, do retry upload, retry times : " & retrytimes)
- ErrorList.AddLog(Now.ToString("yyyyMMddHHmmss _ ") + "Upload to NAS was Error, do retry upload, retry times : " & retrytimes)
- System.Threading.Thread.Sleep(500)
- Else
- Retry = False
- retrytimes = 0
- 'Console.WriteLine("Retry upload to NAS times was arrived")
- 'When retry arrived then add upload error filename into list and error log list
- ErrorList.AddNas(filename)
- ErrorList.AddLog(Now.ToString("yyyyMMddHHmmss _ ") + "Retry upload to NAS times was arrived")
- End If
- End Try
- End While
- File.Delete(sourcepath + "\" + filename)
- '下面是上傳到 FTP 的部分
- '一樣做了重新上傳的機制
- Else
- While Retry
- Try
- 'Upload to FTP, copy file from remote folder, upload done delete loacal file
- File.Copy(copypath + "\" + filename, uptoftp + "\" + filename, True)
- System.Threading.Thread.Sleep(100)
- FTPupload.uploadtoftp()
- System.Threading.Thread.Sleep(100)
- Retry = False
- Catch ex As Exception
- 'Upload to FTP Error, do retry upload file and count retry times
- If retrytimes <= 10 Then
- Retry = True
- retrytimes += 1
- 'Console.WriteLine("Upload to FTP was Error, do retry upload, retry times : " & retrytimes)
- ErrorList.AddLog(Now.ToString("yyyyMMddHHmmss _ ") + "Upload to FTP was Error, do retry upload, do retry upload, retry times : " & retrytimes)
- System.Threading.Thread.Sleep(500)
- Else
- Retry = False
- retrytimes = 0
- 'Console.WriteLine("Retry upload to FTP times was arrived")
- 'When retry arrived then add upload error filename into list and error log list
- ErrorList.AddFtp(filename)
- ErrorList.AddLog(Now.ToString("yyyyMMddHHmmss _ ") + "Retry upload to FTP times was arrived")
- End If
- End Try
- End While
- File.Delete(sourcepath + "\" + filename)
- End If
- Catch ex As Exception
- ErrorList.AddLog(Now.ToString("yyyyMMddHHmmss _ ") + "Copy to reomte folder failure")
- End Try
- End Sub
- End Class
- '在每次嘗試重新上傳的 While 結束之後就會刪除原始檔案(來自 Local Folder的檔案)
-------------------------------------------------------------------------------------------------------------------------
今天分享的內容就是當我監控程式監控到有檔案產生,
那我就要把這個被監控的資料夾產生的檔案備份出來,並且上傳,
當執行備份的時候當然是要先把來源檔案先備份到備份資料夾裡面,
接這這裡的順序我是利用備份資料夾的檔案來做上傳,
上傳也要檢查今天要上傳的方法是哪裡,
再去針對選擇的地方做上傳資料,為了確保上傳資料會成功,
所以加入一些重新上傳的防護措施,結束之後就把來源檔案刪除~
感謝各位的收看!後續我們下集再見了!!!
也感謝各位先進的教學文章!
底下就是這個
1. 上報 練習 _ 選擇資料夾
2. 上報 練習 _ 寫一個 .XML 資料
3. 上報 練習 _ 監控資料夾
4. 上報 練習 _ 複製檔案
5. 上報 練習 _ FTP Server 檔案上傳
6. 上報 練習 _ 上傳錯誤紀錄
7. 上報 練習 _ 使用 BackgroundWorker 再次上傳
8. 上報 練習 _ Log 紀錄
9. 上報 練習 _ 完整程式碼分享
沒有留言:
張貼留言