2011年7月16日 星期六

看門貓∼

自動偵測指定目錄,當偵測到新增檔案時將檔案透過網際網路傳回中心主機上。

一、偵測檔案
>
> //偵測事件(觸發動作:備份、上傳、刪除)
>             FileSystemWatcher fsw = new FileSystemWatcher(fpath);
>             fsw.IncludeSubdirectories = false;//不監控指定目錄內的子目錄
>             //fsw.Filter = "fer.txt"; //不支援使用多個篩選器,例如 "* txt|*.doc"。
>             fsw.Created += new FileSystemEventHandler(fsw_Created);
>             //fsw.Changed += new FileSystemEventHandler(fsw_Changed);
>             fsw.EnableRaisingEvents = true;
>             Console.Read();
>
> static void fsw_Created(object sender, FileSystemEventArgs e)
>         {
>             Console.WriteLine(SaveLog(string.Format("偵測到新增檔案:{0}", e.Name)));
>             DoJob();
>         }

二、FTP傳輸
> /// <summary>
>         /// FTP上傳
>         /// </summary>
>         /// <param name="fName">檔名</param>
>         /// <returns>成功/失敗</returns>
>         private static bool PutFile(string fName)
>         {
>             SaveLog(string.Format("上傳檔案:{0}", fName));
>             string ftpstr = string.Format("ftp://{0}:{1}/{2}{3}", ftp_url, ftp_port, ftp_path, fName);
>             //Create FTP request            
>             FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpstr);
>             request.Method = WebRequestMethods.Ftp.UploadFile;
>             request.Credentials = new NetworkCredential(ftp_id, ftp_pw);
>             request.UsePassive = true;
>             request.UseBinary = true;
>             request.KeepAlive = false;
>
>             //讀入檔案
>             FileStream stream = File.OpenRead(string.Format("{0}{1}", fpath, fName));
>             byte[] buffer = new byte[stream.Length];
>
>             stream.Read(buffer, 0, buffer.Length);
>             stream.Close();
>
>             //上傳
>             try
>             {
>                 Stream reqStream = request.GetRequestStream();
>                 reqStream.Write(buffer, 0, buffer.Length);
>                 reqStream.Close();
>             }
>             catch (Exception ex)
>             {
>                 string exs = string.Format("例外狀況:{0}, Ftp string:{1}", ex.ToString(), ftpstr);
>                 Console.WriteLine(exs);
>                 SaveLog(exs);
>                 SendMail(exs);
>                 return false;
>             }
>             FtpWebResponse response = (FtpWebResponse)request.GetResponse();
>             Console.WriteLine(SaveLog(string.Format("傳輸狀態:{0}", response.StatusDescription)));
>             request.Abort();
>             return true;
>         }
>

三、Email發送
>
> /// <summary>
>         /// 發送通知
>         /// </summary>
>         /// <param name="users">收件人</param>
>         /// <param name="exmsg">例外訊息</param>
>         /// <returns></returns>
>         private static bool SendMail(string _body)
>         {
>             SmtpClient MySmtp = new SmtpClient(ssmtp, ssmtp_port);
>             MailMessage Mms = new MailMessage();
>             //MySmtp.Credentials = new NetworkCredential(mail_id, mail_pw);
>             //MySmtp.EnableSsl = true;
>
>             Mms.From = new MailAddress(mail_src);
>             foreach (string _ms in mail_rec.Split(','))
>             {
>                 if (!string.IsNullOrEmpty(_ms)) Mms.To.Add(_ms);
>             }
>             Mms.Subject = mail_sub;
>             Mms.Body = _body;
>
>             try
>             {
>                 MySmtp.Send(Mms);
>                 Console.WriteLine(SaveLog(string.Format("派送訊息通知:{0}", Mms.To.ToString())));
>             }
>             catch (Exception ex)
>             {
>                 Console.WriteLine(SaveLog(string.Format("SMTP發生例外狀況:{0}", ex.ToString())));
>                 return false;
>             }
>             return true;
>         }

四、引用
> using System.IO;
> using System.Net;
> using System.Net.Mail;
> using System.Net.NetworkInformation;


--
看到、想到、說到、做到 
能夠填平大海的誓言,也比不上邁出一步的價值 
是以吾輩此生,再無任何誓言

沒有留言: