Ur Pocok
Ur Pocok

Reputation: 21

Directory is not removed for file system after Directory.delete

My question is related to this one

I am deleting a folder with Directory.Delete after copy+delete all the files from it -> I am checking it's root folder whether it containing any files -> if not the delete that root folder too.

if (!Directory.Exists(Archivepatz))
{
    Directory.CreateDirectory(Archivepatz);
    foreach (string files in MatrixArchiveFiles)
    {                                   
        string filetocopy = files.Split('\\').Last();
        File.Copy(files, Archivepatz + filetocopy);
        File.Delete(files);
    }
    Directory.Delete(xmlLogfilePathMatrix + MatrixArchiveDay + backslash + MatrixArchiveIP + backslash + MatrixArchiveDMC + backslash, true);
    DirectoryInfo dir2 = new DirectoryInfo(xmlLogfilePathMatrix + MatrixArchiveDay + backslash);    
    if (!dir2.EnumerateFiles("*", SearchOption.AllDirectories).Any())
    {
        if (Directory.Exists(xmlLogfilePathMatrix + MatrixArchiveDay + backslash))
        {
            Directory.Delete(xmlLogfilePathMatrix + MatrixArchiveDay + backslash, true);
        }
    }
}

It is working properly on my test environment (my laptop), but on the PC it should run the program has the issue like in the linked article -> It says access is denied to the directory I just deleted (the directory is visible also in the Windows explorer, but - like my program - also cannot access it because it does not exist in the file system).

This is what I have tried so far:

Added a break after the Directory.Delete to check what is the situation. Here I found that the deleted folder is still visible but not accessible in the Windows explorer.

Tried to add some wait time between the delete and the "if (!dir2." sections hoping it is enough to dispose the folder.

Tried this solution from the linked article: " Directory.Delete(tempFolder, true); while (Directory.Exists(tempFolder)) Thread.Sleep(0); " This resulted in a never ending loop.

Both machines has Windows 10 operating system.

Do you have any idea which Windows component/setting/policy whatever can cause this difference?

Thanks for your support!

Upvotes: 0

Views: 77

Answers (0)

Related Questions