Search code examples
c#directoryconsole-applicationsubdirectorydocument-root

directory and sub-directory path not working


I have the following code, The problem i am having is its only looking at the root directory and not in the sub-folders within the root directory, how can i change it so it would look into the sub-directory's as well.

DirectoryInfo di = new DirectoryInfo(@"C:\Users\allens\Desktop\test");             
DateTime newdate = DateTime.Now.AddHours(-24);                                     

FileInfo[] SWADimg = di.GetFiles("*.jpg");                                        
if (SWADimg.Length == 0)                                                       
{
    var smtpClient = new SmtpClient
    {
        Host = "your.company.smtp.server",                                    
        UseDefaultCredentials = false,
        EnableSsl = true,
        Credentials = new NetworkCredential("account_to_use", "password")
    };

    var message = new MailMessage                                              
    {
        Subject = "SWAD Error",
        Body = "Swad has failed to sync images",
        IsBodyHtml = true,
        From = new MailAddress("//from email address needed")
    };

    message.To.Add(new MailAddress("ALL@jnb.com"));


    smtpClient.Send(message);
} else 
{
    Console.WriteLine("no files present");
    Console.ReadLine();
}

Solution

  • This is the code to recursively list all the files in a directory.

    public static void DirSearch(string strDir)
            {
                try
                {
                    foreach (string strDirectory in Directory.
                        GetDirectories(strDir))
                    {
                        //do something
                        DirSearch(strDirectory);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    

    you just have to add something to save or print the informations.