2012-04-20 67 views
0

我在這裏有一個小問題。我有一個應用程序通過電子郵件發送一個文件列表名稱,通知用戶這些文件已超過一定的大小限制。但現在我希望它不僅發送文件名稱,而且還發送文件大小。我設法將文件大小附加到電子郵件。但我的問題是它展示的方式。我希望它顯示如下:通過電子郵件並排發送兩個列表?

THE FILES : 

filename1 = filesize1 
filename2 = filesize2 
filename3 = filesize3 
.. 
.. 
.. 

HAS REACHED ITS LIMITS! 

但在電子郵件我目前的顯示格式是這樣的:

THE FILES : 

filename1 
filename2 
filename3 
filesize1 
filesize2 
filesize3 

HAS REACHED ITS LIMITS! 

現在我不知道如何來改變顯示格式,如第一個。任何幫助將不勝感激。 這裏是我的代碼片段:

private void Form1_Load(object sender, EventArgs e) 
    { 
     count = 0; 
     timer = new Timer(); 
     timer.Interval = 1000; 
     timer.Tick += new EventHandler(timer1_Tick); 
     timer.Start(); 
     System.Collections.Generic.List<string> files = new List<string>(); 
     System.Collections.Generic.List<string> files1 = new List<string>(); 
     //List<string> s1 = System.IO.Directory.GetFiles(@"C:\Documents and Settings\Administrator\Desktop\test", "*.*", SearchOption.AllDirectories).ToList<string>(); 
     List<string> s1 = System.IO.Directory.GetFiles(@"F:\gdimaging\data", "*.*", SearchOption.AllDirectories).ToList<string>(); 
     s1.AddRange(System.IO.Directory.GetFiles(@"F:\hios\DATA", "*.*", SearchOption.AllDirectories).ToList<string>()); 
     s1.AddRange(System.IO.Directory.GetFiles(@"F:\imgviewer\data", "*.*", SearchOption.AllDirectories).ToList<string>()); 
     s1.AddRange(System.IO.Directory.GetFiles(@"F:\newcnas\data", "*.*", SearchOption.AllDirectories).ToList<string>()); 
     s1.AddRange(System.IO.Directory.GetFiles(@"F:\newpod\data", "*.*", SearchOption.AllDirectories).ToList<string>()); 
     s1.AddRange(System.IO.Directory.GetFiles(@"F:\OMS\data", "*.*", SearchOption.AllDirectories).ToList<string>()); 
     s1.AddRange(System.IO.Directory.GetFiles(@"F:\WEBIMG", "*.*", SearchOption.AllDirectories).ToList<string>()); 

     dt.Columns.Add("File_Name"); 
     dt.Columns.Add("File_Type"); 
     dt.Columns.Add("File_Size"); 
     dt.Columns.Add("Create_Date"); 

     foreach (string s in s1) 
     { 
      try 
      { 
       FileInfo info = new FileInfo(s); 
       FileSystemInfo sysInfo = new FileInfo(s); 
       dr = dt.NewRow(); 
       dr["File_Name"] = sysInfo.Name; 
       dr["File_Type"] = sysInfo.Extension; 
       dr["File_Size"] = (info.Length/1024).ToString(); 
       dr["Create_Date"] = sysInfo.CreationTime.Date.ToString("dd/MM/yyyy"); 
       dt.Rows.Add(dr); 


       if ((info.Length/1024) > 1500000) 
       { 
        MyFiles = new Dictionary<string, string>(); 
        //files.Add(sysInfo.Name.ToString()); 
        //files1.Add(info.Length.ToString()); 
        //arr = string.Join("<br/>", files.ToArray()); 
        //arr1 = string.Join("<br/>", files1.ToArray()); 
        MyFiles.Add(sysInfo.Name.ToString(), info.Length.ToString()); 
       } 

       if (dt.Rows.Count > 0) 
       { 
        dataGridView1.DataSource = dt; 
       } 
      } 
      catch (UnauthorizedAccessException ex) 
      { 
       MessageBox.Show("Error : " + ex.Message); 
       continue; 
      } 
     } 

     if (arr != null) 
     { 
      ///Basic Email message 
      MailMessage mailMessage = new MailMessage(); 
      // Email to send to 
      mailMessage.To.Add(new MailAddress("[email protected]")); 
      mailMessage.To.Add(new MailAddress("[email protected]")); 
      //set subject 
      mailMessage.Subject = "FILE SIZE WARNING MESSAGE"; 
      //set body 
      //mailMessage.Body = "THE FILES : <br/><br/>" + arr + arr1 + "<br/><br/> HAS REACH ITS SIZE LIMIT!!"; 
      mailMessage.Body = "THE FILES : <br/><br/>"; 
      foreach (string key in MyFiles.Keys) 
      { 
       mailMessage.Body += key + " = " + MyFiles[key] + "<br/>"; 
       mailMessage.Body += "<br/> HAS REACHED ITS SIZE LIMIT!"; 
      } 
      mailMessage.IsBodyHtml = true; 
      mailMessage.From = new MailAddress("************", "Shahrul Nizam"); 
      //Identify the credentials to login to the gmail account 
      string sendEmailsFrom = "**********"; 
      string sendEmailsFromPassword = "**********"; 
      NetworkCredential cred = new NetworkCredential(sendEmailsFrom, sendEmailsFromPassword); 
      SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587); 
      mailClient.EnableSsl = true; 
      mailClient.DeliveryMethod = SmtpDeliveryMethod.Network; 
      mailClient.UseDefaultCredentials = false; 
      //mailClient.Timeout = 20000; 
      mailClient.UseDefaultCredentials = true; 
      mailClient.Credentials = cred; 
      mailClient.Send(mailMessage); 
      //MessageBox.Show("Email Notification Sent!"); 
      //MessageBox.Show(fileList.ToString() + "overlimit!!"); 
     } 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
      count++; 
      if (count == 600) 
      { 
       count = 0; 
       timer.Stop(); 
       Application.Restart(); 
      } 
    } 
+0

你目前如何生成太大的文件列表? – sarnold 2012-04-20 02:30:52

+0

你走了。今晚我想不出更完整的答案,爲時已晚。請注意,如果有幫助,給予接受的答案是一個好主意 - >它會提高你的速度,而更多的人(很可能比我更好)會傾向於回答:) – 2012-04-20 02:51:09

回答

1

更改此:

/*step 1*/if ((info.Length/1024) > 1500000) 
       {//my size checking and storing it in an array 
        files.Add(sysInfo.Name.ToString()); 
        files1.Add(info.Length.ToString()); 
        arr = string.Join("<br/>", files.ToArray()); 
        arr1 = string.Join("<br/>", files1.ToArray()); 
       } 
/*step 2*/mailMessage.Body = "THE FILES : <br/><br/>" + arr + arr1 + "<br/><br/> HAS REACH ITS SIZE LIMIT!!"; 

/*step 1*/if ((info.Length/1024) > 1500000) 
       {//my size checking and storing it in an array 
        files.Add(sysInfo.Name.ToString()); 
        files1.Add(info.Length.ToString()); 
       } 
/*step2*/mailMessage.Body = "THE FILES : <br/><br/>"; 
for(int i=0; i<files.Count; i++) 
    mailMessage.Body += files[i] + " = " + files1[i] + "<br/>; 
mailMessage.Body += "<br/> HAS REACHED ITS SIZE LIMIT!!"; 

在列表的最後一個實例額外<br/>都不會有問題,因爲最後幾行從2個休息開始(我刪除了一個)。

的的假定我做:

  • 名單大小相同
  • 各指標在一個,對應於其他項目。

可以這樣做與(類型)的字典更好,像這樣:

/*step 1*/if ((info.Length/1024) > 1500000) 
       {//my size checking and storing it in an array 
        /* I suppose there is a Dictionary<string, string> MyFiles; 
        MyFiles = new Dictionary<string,string>(); instantiated like so*/ 
        MyFiles.add(sysInfo.Name.ToString(), info.Length.ToString()); 
       } 
       //attaching the array to the email body 
        /*step 2*/mailMessage.Body = "THE FILES : <br/><br/>"; 
        foreach(string key in MyFiles.Keys) 
         mailMessage.Body += key + " = " + MyFiles[key] + "<br/>"; 
        mailMessage.Body += "<br/> HAVE REACHED THEIR SIZE LIMIT!!"; 

注意,通過更好地我意味着更多的內存使用效率。更好的意思是不太容易出錯。從技術上講,你有一個鍵值對。這是一本字典。

+0

非常感謝@shingetsu!我想過使用for循環,但我不知道在哪裏放置循環。我仍然是編程新手,所以仍然需要學習很多:)。我已經嘗試了你的兩個解決方案,但只有第一個可行。第二個(字典)解決方案不適合我。我不知道我是否犯了什麼錯誤或什麼。字典不會返回任何值。 – 2012-04-20 03:19:19

+0

對於字典,用字典替換文件的所有出現。好吧。如果你想學習未來的編程,我建議你嘗試C(用於指針理解)和Common Lisp(對你有好處) – 2012-04-20 10:43:20

+0

C和common lisp ??理解。我會研究它。我仍然有很多東西需要學習,因爲我還是個新手。再次感謝@Shingetsu!:) – 2012-04-23 00:44:53