5

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.

Dan Novak
  • 634
  • 2
  • 7
  • 15
  • Could you define "doesn't work?" It's been a while since I worked with `DirectoryInfo` (I usually use the static `Directory` and `File` classes, which might work for you, if this doesn't). Does it throw an exception, or does it just not do anything? – Matthew Haugen Aug 14 '14 at 00:40
  • I'm going to use the age old cry - "It works on my machine!" I ran your code, it created the folder and deleted. I even tried putting files in the folder, it still worked without any errors, I also checked that the folder had indeed been deleted then recreated. I'd say its probably an issue with the files in the folder being locked by another process – Mick Aug 14 '14 at 00:51
  • @Mick, thanks. I'll try this again tomorrow after a reboot, there may have just been a glitch or a file held open as you suggest. I was confused because using the Directory.Create(pathName) and Directory.Delete(pathName, true) instead of FileInfo did work where FileInfo did not. It's good to know that it works for *someone* so this probably isn't some subtlety with FileInfo objeccts that I'm not getting. – Dan Novak Aug 14 '14 at 04:15
  • To figure out in more detail whether or not "they represent abstract paths and aren't necessarily connected to existing physical paths", consider looking at the reference source. Yes, it's going to be specific to some of Microsoft implementations, but most of the time, that all that most people care about. – chwarr Aug 14 '14 at 06:48

1 Answers1

4

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.

Jehof
  • 34,674
  • 10
  • 123
  • 155
  • Thanks. My problem turned out to be just a fluke, but your answer improved my understanding of the types, which was really what I was looking for. – Dan Novak Aug 15 '14 at 00:01
  • In my view, it is a bug from .NETthat directoryInfo.Delete() followed by directoryInfo.Create() doesn't work. It only works if a directoryInfo.Refresh() is between Delete and Create. – Peter Huber Jan 23 '17 at 17:26