c# - 删除包含文件的目录
问题描述
我有一个目录,它是在执行应用程序后创建的。在使用应用程序的过程中,它填充了一些图片,它们是 ComboBox 的 ItemsSource。在关闭应用程序之前,我试图通过设置 new() 或 NULL 来清除 ItemsSource 并删除该目录。如果目录为空,则正常删除。但除此之外 - 什么都没有发生,也不例外。我究竟做错了什么?
创建目录并将路径值分配给“FeedBackScreenshotsPath”
`Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Screenshots"));
MailHelper.FeedBackScreenShotsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Screenshots");`
删除
`if (Directory.Exists(MailHelper.FeedBackScreenShotsPath))
{
Directory.Delete(MailHelper.FeedBackScreenShotsPath, true);
}`
关闭前的消息处理
`private void OnCloseProgramMessageReceived(CloseProgramMessage message)
{
Screenshots = null;
// or Screenshots.Clear();
// or Screenshots = new ObservableCollection<PictureWrapper>();
}`
解决方案
试试这个:
string [] dirs = System.IO.Directory.GetDirectories("C:\\Test\\");
string[] files = System.IO.Directory.GetFiles("C:\\Test\\");
if (dirs.Length == 0 && files.Length == 0)
{
// Is Empty
}
else
{
// Not Empty
}
希望能帮助到你 :)
推荐阅读
- angular - Angular HttpInterceptor 缓存未触发更改检测
- html - 我怎么能有这样的div?
- javascript - 有人可以解释如何确定我的父文件以解除状态吗?
- css - 相同宽度的 SVG 线条呈现不一致
- php - 无法从 PHP 中的 JSON 中删除特定的键和值
- swift - 访问 CollectionView 单元格内容(更改内部视图的宽度)
- php - 如何制作动态路由 PHP MVC
- python - Pandas:从具有多个字典列表的列中提取值并将值拆分为多个列
- php - 如何使用 Api-Platform 保存空数据?
- python - 使用 fsolve 和字符串方程