alphaleonis / AlphaFS

AlphaFS is a .NET library providing more complete Win32 file system functionality to the .NET platform than the standard System.IO classes.
http://alphafs.alphaleonis.com/
MIT License
558 stars 99 forks source link

Exception: (2) The target file is a directory, not a file on DirectoryInfo.Delete(true) #490

Open LinkForce opened 5 years ago

LinkForce commented 5 years ago

Trying to delete a folder gives me the exception on the title. Using 2.2.6 version downloaded from nuget.

Stack trace:

   at Alphaleonis.Win32.Filesystem.FindFileSystemEntryInfo.VerifyInstanceType(WIN32_FIND_DATA win32FindData) in C:\Users\jjangli\Documents\GitHub\AlphaFS\AlphaFS\Filesystem\FindFileSystemEntryInfo.cs:line 428
   at Alphaleonis.Win32.Filesystem.FindFileSystemEntryInfo.Get[T]() in C:\Users\jjangli\Documents\GitHub\AlphaFS\AlphaFS\Filesystem\FindFileSystemEntryInfo.cs:line 549
   at Alphaleonis.Win32.Filesystem.FileSystemInfo.RefreshEntryInfo() in C:\Users\jjangli\Documents\GitHub\AlphaFS\AlphaFS\Filesystem\FileSystemInfo.cs:line 517
   at Alphaleonis.Win32.Filesystem.FileSystemInfo.get_EntryInfo() in C:\Users\jjangli\Documents\GitHub\AlphaFS\AlphaFS\Filesystem\FileSystemInfo.cs:line 373
   at Alphaleonis.Win32.Filesystem.DirectoryInfo.Delete(Boolean recursive) in C:\Users\jjangli\Documents\GitHub\AlphaFS\AlphaFS\Filesystem\DirectoryInfo Class\DirectoryInfo.Delete.cs:line 61
Yomodo commented 5 years ago

I could not reproduce using this test:

private void AlphaFS_DirectoryInfoTest(bool isNetwork)
  {
     using (var tempRoot = new TemporaryDirectory(isNetwork))
     {
        var folderSrc = tempRoot.CreateRecursiveTree(5);
        var dirInfo = new Alphaleonis.Win32.Filesystem.DirectoryInfo(folderSrc.FullName);

        Console.WriteLine("Input Directory Path: [{0}]", dirInfo.FullName);

        Assert.IsFalse(System.IO.File.Exists(dirInfo.FullName));

        Assert.IsTrue(System.IO.Directory.Exists(dirInfo.FullName));

        dirInfo.Delete(true);

        Assert.IsFalse(System.IO.Directory.Exists(dirInfo.FullName));
     }
  }

=== TEST LOCAL ===

Input Directory Path: [C:\Users\jjangli\AppData\Local\Temp\AlphaFS.TempRoot.ëkg15ëw2\Directory.f4s5sxbn]

=== TEST NETWORK ===

Input Directory Path: [\\DESKTOP-0A2DGD2\C$\Users\jjangli\AppData\Local\Temp\AlphaFS.TempRoot.üëïüwdvü\Directory.íttúdjyt]
Yomodo commented 5 years ago

The exception should also mention the path to the file, could you verify it is indeed a file?

LinkForce commented 5 years ago

Yes, it is definitely a Directory, and the directory is created by AlphaFS itself. My code is something like this:

var dir = new DirectoryInfo("C:\\Path\\to\\Directory");

if (dir.Exists)
    dir.Delete(true);

dir.Create();

//doing some work, creating subdirectories and files inside that folder and creating a zip 

Console.WriteLine("Deletando arquivos Físicos");
dir.Delete(true);

I rolled back my application to the AlphaFS version I was using before, 2.0.1, and the code now works again just fine.

I can't provide the actual code because it is proprietary code but any tests that you may want me to make and provide info I can gladly do for you.

Yomodo commented 5 years ago

Ok, could you try the following, to see if it makes any difference:

var dir = new DirectoryInfo("C:\\Path\\to\\Directory", Alphaleonis.Win32.Filesystem.PathFormat::FullPath);`

dir.Refresh();
dir.Delete(true, true);

Alphaleonis.Win32.Filesystem.Directory.Delete(dir.FullName, true); 
Alphaleonis.Win32.Filesystem.Directory.Delete(dir.FullName, true, true);
Yomodo commented 5 years ago

Just to be sure, is the path mentioned in the exception the same path passed to the DirectoryInfo constructor? ("C:\Path\to\Directory") or one of the created sub directories?

Also, does it happen every time, or occasionally?

LinkForce commented 5 years ago

Yes, it is the same path mentioned. And the exception happens every time.

Yomodo commented 5 years ago

I think it's solved. Could you try branch 490 to see if it fixes the issue?

raandree commented 3 years ago

I could reproduce this issue with the following steps:

PS D:\> (New-Object Alphaleonis.Win32.Filesystem.DirectoryInfo('D:\AlphaFsTest')).EnumerateFileSystemInfos()

    Directory: D:\AlphaFsTest

Mode    Inherits             LastWriteTime         Size(M) Name                                                                              
----    --------             -------------         ------- ----                                                                              
            True      20.04.2021     23:39               0 $New Text Document.txt                                                            

PS D:\> (New-Object Alphaleonis.Win32.Filesystem.DirectoryInfo('D:\')).EnumerateFileSystemInfos()

    Directory: D:\

Mode    Inherits             LastWriteTime         Size(M) Name                                                                              
----    --------             -------------         ------- ----                                                                              
            True      19.12.2020     22:45   <DIR>         $RECYCLE.BIN                                                                      
            True      20.04.2021     23:41   <DIR>         AlphaFsTest                                                                       
            True      20.04.2021     23:14   <DIR>         AutomatedLab-VMs                                                                  
            True      27.11.2020     09:12   <DIR>         AutomatedLab.OSS                                                                  

PS D:\> Move-Item '.\AlphaFsTest\$New Text Document.txt' -Destination d:\

PS D:\> (New-Object Alphaleonis.Win32.Filesystem.DirectoryInfo('D:\')).EnumerateFileSystemInfos()
An error occurred while enumerating through a collection: (3) The target directory is a file, not a directory: [D:].
At line:1 char:1
+ (New-Object Alphaleonis.Win32.Filesystem.DirectoryInfo('D:\')).Enumer ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Alphaleonis.Win...FileSystemInfo]:<Enumerate>d__84`1) [], RuntimeException
    + FullyQualifiedErrorId : BadEnumeration

After moving the file '$New Text Document.txt' to the root of drive D, I was no longer be able to read its content.

I have discovered the bug when working on raandree/NTFSSecurity#46.

@Yomodo, are you planning to release a new version with fixes or is AlphaFS abandoned?

itsho commented 2 years ago

I think it's already solved. I was able to reproduce it with 2.2.0 but not with 2.2.6 (current latest)