DirectoryInfo.Exists still returns False after directory created.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In the 1.1 framework, I've come across some peculiar behaviour in
DirectoryInfo.Exists. If you create a DirectoryInfo for a directory that
doesn't exist, DirectoryInfo.Exists correctly returns False. However, if you
then call DirectoryInfo.Create to create the directory, DirectoryInfo.Exists
still returns False even though the directory has been successfully created.
This is a little counterintuitive to say the least.

The following code:
string test = @"C:\test";

if (Directory.Exists(test))
{
Directory.Delete(test, true /*recursive*/);
}

Console.WriteLine("Directory.Exists(test): {0}",
Directory.Exists(test));

DirectoryInfo info = new DirectoryInfo(test);

Console.WriteLine("info.Exists: {0}", info.Exists);
Console.WriteLine("info.Create();");
info.Create();
Console.WriteLine("info.Exists: {0}", info.Exists);
Console.WriteLine("Directory.Exists(test): {0}",
Directory.Exists(test));

produces this output:
Directory.Exists(test): False
info.Exists: False
info.Create();
info.Exists: False
Directory.Exists(test): True

It seems to me that this is, well, wrong, isn't it?

Phil Rodgers
 
Phil Rodgers said:
In the 1.1 framework, I've come across some peculiar behaviour in
DirectoryInfo.Exists. If you create a DirectoryInfo for a directory that
doesn't exist, DirectoryInfo.Exists correctly returns False. However, if
you
then call DirectoryInfo.Create to create the directory,
DirectoryInfo.Exists
still returns False even though the directory has been successfully
created.
This is a little counterintuitive to say the least.

The following code:
string test = @"C:\test";

if (Directory.Exists(test))
{
Directory.Delete(test, true /*recursive*/);
}

Console.WriteLine("Directory.Exists(test): {0}",
Directory.Exists(test));

DirectoryInfo info = new DirectoryInfo(test);

Console.WriteLine("info.Exists: {0}", info.Exists);
Console.WriteLine("info.Create();");
info.Create();
Console.WriteLine("info.Exists: {0}", info.Exists);
Console.WriteLine("Directory.Exists(test): {0}",
Directory.Exists(test));

produces this output:
Directory.Exists(test): False
info.Exists: False
info.Create();
info.Exists: False
Directory.Exists(test): True

It seems to me that this is, well, wrong, isn't it?

Phil Rodgers

info.Exists returns the state at the time you created the instance of the
DirectoryInfo class. That means you have to create a new instance after you
created the directory.

Willy.
 
Hi Phil,

The DirectoryInfo class caches info and does not auto refresh. You must
explicitly call DirectoryInfo.Refresh() method.
DirectoryInfo class is provided to cache directory info to avoid load
it many times. If you does not reuse directory info, use Directory
class instead. Directory.CreateDirectory; Directory.Exists.

Thi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top