【问题标题】:directory and sub-directory path not working [duplicate]目录和子目录路径不起作用[重复]
【发布时间】:2018-07-18 05:19:17
【问题描述】:

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

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# directory console-application subdirectory document-root


    【解决方案1】:

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

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

    您只需添加一些内容即可保存或打印信息。

    【讨论】:

      猜你喜欢
      • 2020-11-28
      • 2017-01-18
      • 1970-01-01
      • 2014-11-26
      • 2014-06-21
      • 2020-09-08
      • 1970-01-01
      • 2020-11-18
      • 1970-01-01
      相关资源
      最近更新 更多