c# - 目录和子目录路径不起作用
问题描述
我有以下代码,我遇到的问题是它只查看根目录而不是根目录中的子文件夹,我该如何更改它以便它也会查看子目录。
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();
}
解决方案
这是递归列出目录中所有文件的代码。
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);
}
}
你只需要添加一些东西来保存或打印信息。
推荐阅读
- swift - 喜欢大标题和 RefreshControl 不能正常工作
- php - Nginx & php-fpm - 主机无法访问
- javascript - HTML/CSS:如何为不同类型的文章制作“文章”的多个“版本”
- arrays - 按条件更新 MongoDB 中的数组
- segmentation-fault - 在 fortran 中发送具有可分配数组的 MPI 派生类型的 Seg 错误
- gem5 - 在 gem5 上模拟 sample.py 时出错:“AttributeError: Not allowed to set pio on 'SimObjectVector'”
- delphi - 列表
当 T 是事件处理程序时崩溃? - vuejs2 - Vue2 在编辑视图中填充输入
- jquery - 跨多个页面序列化 DataTables 中的选定复选框
- java - Comparator 如何在内部与 ArrayList 一起工作?(如何跟踪工作于 Arraylist 的比较器)