- 帖子
- 46
- 主題
- 18
- 精華
- 0
- 積分
- 67
- 點名
- 0
- 作業系統
- WINXP,win7
- 軟體版本
- office2007
- 閱讀權限
- 20
- 性別
- 男
- 來自
- 肚子裡
- 註冊時間
- 2010-5-22
- 最後登錄
- 2011-10-27
|
Dear 大大們,
小弟最近在寫一功能的VB。
其功能是能夠監控1資料夾和子目錄的檔案異動(新增、刪除、修改)的時間log記錄。
並且每有異動,就用Mail發出通知。
Mail 含log附件檔案,或將log的內容貼上郵件內文。
但小弟才剛學不久,對於迴圈if 這些觀念很部會...。
以下兩個程式如果單獨執行都沒有問題。
1個是mail的class,1個是監控目錄的class。
想請教大大如果兩個程式的功能結合一起。
應該要怎麼修改呢?
謝謝各位大大~- Public Class Mail001
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim To_Address As String = "webb@google.com"
- ' 設定SMTPserver
- Dim SMTP_Server As New Net.Mail.SmtpClient("google.smtp.com")
- Dim Mailmsg As New System.Net.Mail.MailMessage
- Dim MailBodyHtml As String
- ' 設定一個信件的附檔
- Dim mail_attachment As New System.Net.Mail.Attachment("C:\VB_Mail.rar")
- Mailmsg.IsBodyHtml = True ' 為html內容格式
- ' 此信的寄件人
- Mailmsg.From = New Net.Mail.MailAddress("webb@....com", "webb")
- Mailmsg.Subject = "test_webb"
- MailBodyHtml = "Mail Message First"
- MailBodyHtml &= "Mail Message Second"
- Mailmsg.Body = MailBodyHtml
- ' 把附檔加入到Mail
- Mailmsg.Attachments.Add(mail_attachment)
- ' 將字串裡的;分為多個收件人
- For Each MA As String In To_Address.Split(";")
- Mailmsg.To.Add(New Net.Mail.MailAddress(MA, "webbliu.world@gmail.com"))
- Next
- ' 密件副本收件人
- ' Mailmsg.Bcc.Add(New Net.Mail.MailAddress("webb@google.com"))
- ' Mailmsg.Priority = Net.Mail.MailPriority.Normal
- ' Smtp(Server) 的帳號與密碼
- SMTP_Server.Credentials = New System.Net.NetworkCredential("Liuxxx", "123456")
- SMTP_Server.Send(Mailmsg)
- MessageBox.Show("Message sent.")
複製代碼- 先從工具箱插入以下物件
- txt_watchpath ' TextBox (for folder path)
- btn_startwatch ' Button (start watching)
- btn_stop ' Button (stop watching)
- txt_folderactivity ' Textbox (folder activity)
- Imports System.IO
- Imports System.Diagnostics
- Public Class webb_000
- Public watchfolder As FileSystemWatcher
- Private Sub btn_startwatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_startwatch.Click
- watchfolder = New System.IO.FileSystemWatcher()
- 'this is the path we want to monitor
- watchfolder.Path = txt_watchpath.Text
- 'Add a list of Filter we want to specify
- 'make sure you use OR for each Filter as we need to
- 'all of those
- watchfolder.IncludeSubdirectories = True '監視子目錄
- watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
- watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
- IO.NotifyFilters.FileName
- watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
- IO.NotifyFilters.Attributes
- ' add the handler to each event
- AddHandler watchfolder.Changed, AddressOf logchange
- AddHandler watchfolder.Created, AddressOf logchange
- AddHandler watchfolder.Deleted, AddressOf logchange
- ' add the rename handler as the signature is different
- AddHandler watchfolder.Renamed, AddressOf logrename
- 'Set this property to true to start watching
- watchfolder.EnableRaisingEvents = True
- btn_startwatch.Enabled = False
- btn_stop.Enabled = True
- 'End of code for btn_start_click
- End Sub
- Private Sub txt_folderactivity_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_folderactivity.TextChanged
- End Sub
- Private Sub logchange(ByVal source As Object, ByVal e As _
- System.IO.FileSystemEventArgs)
- If e.ChangeType = IO.WatcherChangeTypes.Changed Then
- txt_folderactivity.Text &= "File " & e.FullPath & _
- "已修改" & Now & vbCrLf
- End If
- If e.ChangeType = IO.WatcherChangeTypes.Created Then
- txt_folderactivity.Text &= "File " & e.FullPath & _
- " has been created已建立" & Now & vbCrLf
- End If
- If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
- txt_folderactivity.Text &= "File " & e.FullPath & _
- " 已刪除" & Now & vbCrLf 'Now 就是目前日期時間
- End If
- End Sub
- Public Sub logrename(ByVal source As Object, ByVal e As _
- System.IO.RenamedEventArgs)
- txt_folderactivity.Text &= "File" & e.OldName & _
- " has been renamed to 已更名為" & e.Name & vbCrLf
- End Sub
- Private Sub btn_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_stop.Click
- ' Stop watching the folder
- watchfolder.EnableRaisingEvents = False
- btn_startwatch.Enabled = True
- btn_stop.Enabled = False
- End Sub
- End Class
複製代碼 |
|