I got an application where by it will monitor all the files in multiple directories. Whenever there is files that exceeds a certain size limit, it will send warning email notification. Currently, my program will send an email for each oversize file. Meaning if there is 20 files that is oversize, then it will send 20 emails. Can someone teach how to store all the file name within a single variable and send a single email which tells the list of files name that is oversized. Here is my source :

private void Form1_Load(object sender, EventArgs e){

        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");
        List<string> files = new List<string>();

        foreach (string s in s1) 
        {   
            try
            {   
                FileInfo info = new FileInfo(s);
                FileSystemInfo sysInfo = new FileInfo(s);
                dr = dt.NewRow();
                //System.Collections.Generic.List<string> nameList;
                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)
                {
                    files.Add(sysInfo.Name.ToString()); //Here is the part where I tried to store the list of files name.
                }

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

        }
        //fileList.Add(sysInfo.Name).ToString();
        //fileList.Add(sysInfo.Name);
        ///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 FILE HAS REACH ITS SIZE LIMIT!!";
        mailMessage.IsBodyHtml = true;
        mailMessage.From = new MailAddress("shahrul1509@gmail.com", "Shahrul Nizam");
        //Identify the credentials to login to the gmail account  
        string sendEmailsFrom = "shahrul1509@gmail.com";
        string sendEmailsFromPassword = "0137089732";
        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.Credentials = cred;
        mailClient.Send(mailMessage);
        //MessageBox.Show("Email Notification Sent!");
        ///MessageBox.Show(fileList.ToString() + "overlimit!!");
    }`

Recommended Answers

All 11 Replies

You can turn a List<string> into a single string if you first convert it to an array and the use string.Join().
Like:

   string.Join(",", lstOfFiles.ToArray());

sorry but I don't quite understand. Can you explain a bit more. Sorry.

To reprhase thines01's post:
convert your list of strings (aka files) into a string array (let's say, arr) using .ToArray(), combine all elements of this new array into a single string using string.Join, and assign that to a new string variable. thines01's post has the syntax.

Ok got it. Thanks for the advice. I'm still new in programming so I'm not sure about the full syntax but I'll try.

I've edited my code and I tried running it. The email sent doesn't contain the file name, instead it contains :

THE FILE System.Collections.Generic.List `1[System.String] HAS REACH IT LIMITS.

Here is my edited code. Please correct me if I'm wrong.

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>();
            string[] arr;


            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();
                    //System.Collections.Generic.List<string> nameList;
                    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)
                    {
                        files.Add(sysInfo.Name.ToString());
                        string.Join(",", files.ToArray()); //EDITED PART
                    }

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

            }

            ///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 FILE " + files + " HAS REACH 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.Credentials = cred;
            mailClient.Send(mailMessage);
            //MessageBox.Show("Email Notification Sent!");
            ///MessageBox.Show(fileList.ToString() + "overlimit!!");
        }

Are you supposed to send the list itself? Or merely a string representation of the list? If it's the latter, you should change a few things.

At line 43: You created a string, but you did not assign it to a string variable.
Additionally, the "," part decides how the list should like like. At present, each filename would be separated by a comma:

filename1,filename2,filename3,....

If you wanted to make it more like a list like:

filename1
filename2
filename3
...

then replace that comma with a newline/carriage return combo.

At line 67: simply replace files with the string variable you are supposed to use at line 43.

What I would like is to store all of the file name which is oversized within a string/list variables, and then send it through a single email.
Example of the email content is : THE FILE filename1,filename2,filename3.... HAS REACHED IT LIMITS.

Then follow my previous post (both lines 43 and 67), but disregard parts with the ",", and the newline/return combo.

Sorry I am a bit noob here but I'm quite new in programming. But I think I already declared a string variable in line 9. Then when I want to do arr.Join(",", files.ToArray()) , there is a syntax error. Do correct if I'm wrong.

Ok. First, since you used the syntax thines01 posted, there's no more need for an arr array. However, you could use arr as a string. So, you can remove those [] at line 9 now. Next, you're not supposed to replace string.join with arr.join. You are supposed to assign string.join to arr. arr = string.Join(",",files.ToArray());

ok got it. Thanks a lot man for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.