Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
LinkForce opened this issue Aug 24, 2018 · 9 comments
Assignees
Labels

Comments

@LinkForce
Copy link

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
Copy link
Collaborator

Yomodo commented Aug 24, 2018

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
Copy link
Collaborator

Yomodo commented Aug 24, 2018

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

@LinkForce
Copy link
Author

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.

Repository owner deleted a comment from LinkForce Aug 25, 2018
@Yomodo
Copy link
Collaborator

Yomodo commented Aug 25, 2018

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 Yomodo reopened this Aug 25, 2018
@Yomodo
Copy link
Collaborator

Yomodo commented Aug 25, 2018

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
Copy link
Author

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

@Yomodo Yomodo self-assigned this Aug 28, 2018
@Yomodo
Copy link
Collaborator

Yomodo commented Sep 1, 2018

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

@raandree
Copy link

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
Copy link

itsho commented Jun 29, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants