Why won't a DirectoryInfo instance (re)create a folder after deleting it?

Dan Novak

I'm assuming .NET DirectoryInfo and FileInfo objects are similar to Java's java.io.File, i.e. they represent abstract paths and aren't necessarily connected to existing physical paths.

I can do what I'm trying to do (empty out a folder and create it if it doesn't exist) in a different way that works, but I'd like to understand why this does not:

using System.IO;

namespace TestWipeFolder
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var di = new DirectoryInfo(@"C:\foo\bar\baz");

            if (di.Exists)
            {
                di.Delete(true);
            }

            // This doesn't work.  C:\foo\bar is still there but it doesn't remake baz.
            di.Create();
        }
    }
}

UPDATE: I tried the same code after a reboot and it worked fine. I still want to know what the similarities are to Java File objects and whether deleting a folder a DirectoryInfo object references can screw things up, but that is on the back burner now.

Jehof

The DirectoryInfo class provides you the information of a directory at the time you create the DirectoryInfo instance.

If changes are made to the directory like delete, then the information is not reflected to your current instance. You need to call .Refresh() on the instance to update the state of the DirectoryInfo instance.

LinqPad Testcode:

var di = new DirectoryInfo(@"C:\foo\bar\baz");
di.Dump();

if (di.Exists){
  di.Exists.Dump();  // prints out true

  di.Delete(true);
  di.Exists.Dump();  // still prints out true

  di.Refresh();
  di.Exists.Dump();    // prints out false
}

di.Create();
di.Refresh();
di.Exists.Dump();    // prints out true

The similar classes to the java ones are System.IO.File and System.IO.Directory. Using this classes you will get the current state of the files and directories.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django 1.7 migrations won't recreate a dropped table, why?

From Dev

Options For Deleting A Folder, Which Won't Seem to Delete?

From Dev

After deleting android database getReadableDatabase() won't create a new database

From Dev

Xcode won't add "Embedded binary" after deleting "DerivedData"

From Dev

Data Grid won't change after deleting source file

From Dev

Can't recreate and schedule Timer gain after cancelling it. Why?

From Dev

Eclipse won't update after adding project to workspace folder

From Dev

Why won't the client receive new versions of this script in the public folder?

From Dev

Why won't dynamically adding a `__call__` method to an instance work?

From Dev

Why won't eclipse app instance run in https

From Dev

Why opening after deleting doesn't call the onupgradeneeded callback

From Dev

(Vue.js and Laravel) Array won't refresh after deleting item

From Dev

Initial creating then deleting a cookie works, but can't recreate it afterwards

From Dev

Why does file operations hangs after deleting folder on large NTFS volume

From Dev

Why won't this div appear after I click another div?

From Dev

Why won't my div reveal itself after Ajax call?

From Dev

apache won't restart after adding VirtualHost conf file, why not?

From Dev

Why won't header redirect work after login?

From Dev

Why won't this while loop exit after a "0" is entered?

From Dev

Why won't my user input field reset after submitting?

From Dev

Why won't the array update after subscribing to a service?

From Java

Why EC2 instance continues responding to a ping request after deleting the inbound security group rule?

From Dev

tmpfs — deleting files won't free the space

From Dev

Home folder won't open

From Java

DirectoryInfo.Exists returns true even after the folder has been deleted

From Dev

How to recreate a project in Android Studio after losing the .idea folder?

From Dev

How to recreate a project in Android Studio after losing the .idea folder?

From Dev

Why won't this die?

From Dev

Why won't it revolve?

Related Related

  1. 1

    Django 1.7 migrations won't recreate a dropped table, why?

  2. 2

    Options For Deleting A Folder, Which Won't Seem to Delete?

  3. 3

    After deleting android database getReadableDatabase() won't create a new database

  4. 4

    Xcode won't add "Embedded binary" after deleting "DerivedData"

  5. 5

    Data Grid won't change after deleting source file

  6. 6

    Can't recreate and schedule Timer gain after cancelling it. Why?

  7. 7

    Eclipse won't update after adding project to workspace folder

  8. 8

    Why won't the client receive new versions of this script in the public folder?

  9. 9

    Why won't dynamically adding a `__call__` method to an instance work?

  10. 10

    Why won't eclipse app instance run in https

  11. 11

    Why opening after deleting doesn't call the onupgradeneeded callback

  12. 12

    (Vue.js and Laravel) Array won't refresh after deleting item

  13. 13

    Initial creating then deleting a cookie works, but can't recreate it afterwards

  14. 14

    Why does file operations hangs after deleting folder on large NTFS volume

  15. 15

    Why won't this div appear after I click another div?

  16. 16

    Why won't my div reveal itself after Ajax call?

  17. 17

    apache won't restart after adding VirtualHost conf file, why not?

  18. 18

    Why won't header redirect work after login?

  19. 19

    Why won't this while loop exit after a "0" is entered?

  20. 20

    Why won't my user input field reset after submitting?

  21. 21

    Why won't the array update after subscribing to a service?

  22. 22

    Why EC2 instance continues responding to a ping request after deleting the inbound security group rule?

  23. 23

    tmpfs — deleting files won't free the space

  24. 24

    Home folder won't open

  25. 25

    DirectoryInfo.Exists returns true even after the folder has been deleted

  26. 26

    How to recreate a project in Android Studio after losing the .idea folder?

  27. 27

    How to recreate a project in Android Studio after losing the .idea folder?

  28. 28

    Why won't this die?

  29. 29

    Why won't it revolve?

HotTag

Archive