I have a little problem here. I got an application which sends a list of files name through email notifying the user that those files has exceeded a certain size limit. But now I want it to not only send the files name but also the files size. I manage to attach the file size to the email. But my problem is the way it displayed. I want it to displayed like :
THE FILES :
filename1 = filesize1
filename2 = filesize2
filename3 = filesize3
..
..
..
HAS REACHED ITS LIMITS!
But my current display format in the email is like :
THE FILES :
filename1
filename2
filename3
filesize1
filesize2
filesize3
HAS REACHED ITS LIMITS!
And now I don’t know how to change the display format like the first one. Any help would be greatly appreciated.
Here is my code snippet :
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("shahrul1509@yahoo.com"));
mailMessage.To.Add(new MailAddress("shahrul_kakashi90@hotmail.com"));
//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();
}
}
Change this:
to
The extra
<br/>at the last instance of the lists won’t matter because the last lines start with 2 breaks (I removed one).The suppositions I make:
This could be done better with a DICTIONARY (of type ) like so:
Note that by better I do NOT mean more memory efficient. By better I mean less error-prone. Technically you have a key-value pairing. That is a dictionary.