Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am attempting to copy some file and then send a email listing the files that were copied. I am still getting started in C#, but here is what I have so far. Any help is appreciated!

C#
static void Main(string[] args)
        {
          

            string dirpath = @"X:\";

            DateTime theDate = DateTime.Today;

            DirectoryInfo Dir = new DirectoryInfo(Path.Combine(@"C:\TeamABC\docs"));
            DirectoryInfo target = new DirectoryInfo(Path.Combine(dirpath));


            FileInfo[] fis = Dir.GetFiles("*.*", SearchOption.AllDirectories);
            foreach (FileInfo fi in fis)
            {
                if (fi.LastWriteTime.Date == theDate)
                    
                {
                    File.Copy(fi.FullName, target.FullName + @"\" + fi.Name, true);
                                                          
                }

               


            }
                      
            MailAddress to = new MailAddress("person1@abc.com");
           
            MailAdddress from = new MailAddress("fileupdater@teamABC.com");

              MailMessage mail = new MailMessage(from, to);

            MailAddress copy = new MailAddress("person2@teamABC.com");
            mail.CC.Add(copy);

            
            mail.Subject = "The Following Files have been updated.";
            

          

            //FileInfo[] fis = Dir.GetFiles("*.*", SearchOption.AllDirectories);
            foreach (FileInfo fi in fis)
            {
                if (fi.LastWriteTime.Date == theDate)
                {
                    File.Copy(fi.FullName, target.FullName + @"\" + fi.Name, true);
                    //var copies = (fi.LastWriteTime.Date == theDate);
                    mail.Body = (fi.Name) //this is where I'd like to store each file that was copied to a list or array ***
                    
               


                }




            }

            //mail.Body = ("");
            mail.IsBodyHtml = true;
   
        

            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;

            smtp.Credentials = new NetworkCredential(
                "person1@gmail.com", "Password");
            smtp.EnableSsl = true;
            Console.WriteLine("Sending email...");
            smtp.Send(mail);


        }


    }
    }
Posted
Comments
David_Wimbley 14-Jan-13 23:39pm    
Where exactly are you having issues? Or are you looking for tips to clean things up?
Vani Kulkarni 14-Jan-13 23:43pm    
What is the issue you are facing? Please elaborate.

1 solution

Im going to make the assumption (Dangerous in know) that i think i know what it is you are requesting.

The following will store all your files that you end up copying with your last write time constraint == today.
C#
StringBuilder copiedFiles = new StringBuilder();


Along with your file copy, after the file is copied the line below will add the full name of that file to the variable and will keep looping till all files are done making it suitable to use for email in your body.

C#
copiedFiles.Append(fi.FullName);


C#
DirectoryInfo directory = new DirectoryInfo(@"c:\temp");

            FileInfo[] fis = directory.GetFiles("*.*", SearchOption.AllDirectories);
            StringBuilder copiedFiles = new StringBuilder();
            foreach (FileInfo fi in fis)
            {
                if (fi.LastWriteTime.Date == DateTime.Today)
                {
                    //Run Code To Do FileCop
                    copiedFiles.Append(fi.FullName);
                }
            }

            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient(smptServer, portNumber);
            mail.From = new System.Net.Mail.MailAddress(sendFrom);

            if (sendTo.Contains(";"))
            {
                string[] emailAddress = sendTo.Split(';');

                foreach (string emailAddr in emailAddress)
                {
                    mail.To.Add(emailAddr);
                }
            }
            else
            {
                mail.To.Add(sendTo);
            }
            mail.Subject = subject;
            mail.Body = copiedFiles.ToString();
            mail.IsBodyHtml = true;

            SmtpServer.Port = portNumber;
            SmtpServer.Credentials = new System.Net.NetworkCredential(authUser, authPass);
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);
 
Share this answer
 
v2
Comments
Dustin Prevatt 15-Jan-13 22:41pm    
This is much closer to what I was trying to accomplish but now it seems to list all files even if the lastwritetime was not = datetime.today.

FileInfo[] fis = Dir.GetFiles("*.*", SearchOption.AllDirectories);
StringBuilder copiedFiles = new StringBuilder();

foreach (FileInfo fi in fis)
{
if (fi.LastWriteTime.Date == DateTime.Today)
{
copiedFiles.Append(fi.Name);// this lists all files
File.Copy(fi.FullName, target.FullName + @"\" + fi.Name, true);
Console.WriteLine(fi.Name);//This shows what I would expect to see.



}

copiedFiles.AppendLine(fi.Name);



}
David_Wimbley 15-Jan-13 22:53pm    
It lists everything no matter what because you have "copiedFiles.AppendLine(fi.Name);" outside of your If statement but inside your Foreach statement.

Sorry bout that, it was in the code snippet i submitted. Remove that line and it will/should work.
Dustin Prevatt 15-Jan-13 23:01pm    
That did it, but now its not separating the files 1 per line it just list them all together. Any idea how to fix this?
Dustin Prevatt 15-Jan-13 23:04pm    
Nevermind i got it changed the append to appendLine. Thank You so much for your help!
David_Wimbley 15-Jan-13 23:09pm    
Glad to see its working, happy to help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900