首页 > 解决方案 > 目录和子目录路径不起作用

问题描述

我有以下代码,我遇到的问题是它只查看根目录而不是根目录中的子文件夹,我该如何更改它以便它也会查看子目录。

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();
}

标签: c#directoryconsole-applicationsubdirectorydocument-root

解决方案


这是递归列出目录中所有文件的代码。

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);
            }
        }

你只需要添加一些东西来保存或打印信息。


推荐阅读