Board logo

標題: [發問] 監控目錄異動log & VB郵件程式 [打印本頁]

作者: webbliu    時間: 2011-2-21 19:54     標題: 監控目錄異動log & VB郵件程式

Dear 大大們,

小弟最近在寫一功能的VB。
其功能是能夠監控1資料夾和子目錄的檔案異動(新增、刪除、修改)的時間log記錄。
並且每有異動,就用Mail發出通知。
Mail 含log附件檔案,或將log的內容貼上郵件內文。

但小弟才剛學不久,對於迴圈if 這些觀念很部會...。
以下兩個程式如果單獨執行都沒有問題。
1個是mail的class,1個是監控目錄的class。
想請教大大如果兩個程式的功能結合一起。
應該要怎麼修改呢?

謝謝各位大大~
  1. Public Class Mail001


  2.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  3.         Dim To_Address As String = "[email protected]"

  4.         ' 設定SMTPserver
  5.         Dim SMTP_Server As New Net.Mail.SmtpClient("google.smtp.com")
  6.         Dim Mailmsg As New System.Net.Mail.MailMessage
  7.         Dim MailBodyHtml As String

  8.         ' 設定一個信件的附檔
  9.         Dim mail_attachment As New System.Net.Mail.Attachment("C:\VB_Mail.rar")

  10.         Mailmsg.IsBodyHtml = True ' 為html內容格式

  11.         ' 此信的寄件人
  12.         Mailmsg.From = New Net.Mail.MailAddress("[email protected]", "webb")
  13.         Mailmsg.Subject = "test_webb"
  14.         MailBodyHtml = "Mail Message First"
  15.         MailBodyHtml &= "Mail Message Second"
  16.         Mailmsg.Body = MailBodyHtml

  17.         ' 把附檔加入到Mail
  18.         Mailmsg.Attachments.Add(mail_attachment)

  19.         ' 將字串裡的;分為多個收件人
  20.         For Each MA As String In To_Address.Split(";")
  21.             Mailmsg.To.Add(New Net.Mail.MailAddress(MA, "[email protected]"))
  22.         Next

  23.         ' 密件副本收件人
  24.         ' Mailmsg.Bcc.Add(New Net.Mail.MailAddress("[email protected]"))
  25.         ' Mailmsg.Priority = Net.Mail.MailPriority.Normal

  26.         ' Smtp(Server) 的帳號與密碼
  27.         SMTP_Server.Credentials = New System.Net.NetworkCredential("Liuxxx", "123456")
  28.         SMTP_Server.Send(Mailmsg)
  29.         MessageBox.Show("Message sent.")
複製代碼
  1. 先從工具箱插入以下物件
  2. txt_watchpath           '      TextBox  (for folder path)
  3. btn_startwatch          '      Button   (start watching)
  4. btn_stop                '      Button   (stop watching)
  5. txt_folderactivity      '      Textbox  (folder activity)


  6. Imports System.IO
  7. Imports System.Diagnostics


  8. Public Class webb_000
  9.     Public watchfolder As FileSystemWatcher


  10.     Private Sub btn_startwatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_startwatch.Click

  11.         watchfolder = New System.IO.FileSystemWatcher()

  12.         'this is the path we want to monitor

  13.         watchfolder.Path = txt_watchpath.Text

  14.         'Add a list of Filter we want to specify

  15.         'make sure you use OR for each Filter as we need to

  16.         'all of those
  17.         watchfolder.IncludeSubdirectories = True '監視子目錄
  18.         watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
  19.         watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
  20.                                    IO.NotifyFilters.FileName
  21.         watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
  22.                                    IO.NotifyFilters.Attributes
  23.         ' add the handler to each event
  24.         AddHandler watchfolder.Changed, AddressOf logchange
  25.         AddHandler watchfolder.Created, AddressOf logchange
  26.         AddHandler watchfolder.Deleted, AddressOf logchange

  27.         ' add the rename handler as the signature is different

  28.         AddHandler watchfolder.Renamed, AddressOf logrename

  29.         'Set this property to true to start watching
  30.         watchfolder.EnableRaisingEvents = True

  31.         btn_startwatch.Enabled = False
  32.         btn_stop.Enabled = True

  33.         'End of code for btn_start_click
  34.     End Sub

  35.     Private Sub txt_folderactivity_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_folderactivity.TextChanged
  36.     End Sub
  37.     Private Sub logchange(ByVal source As Object, ByVal e As  _
  38.                      System.IO.FileSystemEventArgs)
  39.         If e.ChangeType = IO.WatcherChangeTypes.Changed Then
  40.             txt_folderactivity.Text &= "File " & e.FullPath & _
  41.                                     "已修改" & Now & vbCrLf
  42.         End If
  43.         If e.ChangeType = IO.WatcherChangeTypes.Created Then
  44.             txt_folderactivity.Text &= "File " & e.FullPath & _
  45.                                      " has been created已建立" & Now & vbCrLf
  46.         End If
  47.         If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
  48.             txt_folderactivity.Text &= "File " & e.FullPath & _
  49.                                     " 已刪除" & Now & vbCrLf    'Now 就是目前日期時間
  50.         End If
  51.     End Sub


  52.     Public Sub logrename(ByVal source As Object, ByVal e As  _
  53.                                 System.IO.RenamedEventArgs)
  54.         txt_folderactivity.Text &= "File" & e.OldName & _
  55.                       " has been renamed to 已更名為" & e.Name & vbCrLf
  56.     End Sub
  57.     Private Sub btn_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_stop.Click
  58.         ' Stop watching the folder

  59.         watchfolder.EnableRaisingEvents = False
  60.         btn_startwatch.Enabled = True
  61.         btn_stop.Enabled = False
  62.     End Sub
  63. End Class
複製代碼





歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)