首页 > 解决方案 > 删除包含文件的目录

问题描述

我有一个目录,它是在执行应用程序后创建的。在使用应用程序的过程中,它填充了一些图片,它们是 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>();
    }`

标签: c#

解决方案


试试这个:

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
}

希望能帮助到你 :)


推荐阅读