PC Review


Reply
Thread Tools Rate Thread

DirectoryInfo.Exists still returns False after directory created.

 
 
=?Utf-8?B?UGhpbCBSb2RnZXJz?=
Guest
Posts: n/a
 
      24th Nov 2005
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
 
Reply With Quote
 
 
 
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      24th Nov 2005

"Phil Rodgers" <(E-Mail Removed)> wrote in message
news:4FA27F2B-4BB0-423E-82C9-(E-Mail Removed)...
> 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.




 
Reply With Quote
 
Truong Hong Thi
Guest
Posts: n/a
 
      25th Nov 2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File with nbsp chars in name returned from Directory.GetFiles() but File.Exists() returns false Samuel R. Neff Microsoft C# .NET 1 6th Oct 2008 08:04 AM
Directory.exists always FALSE Roger Odermatt Microsoft Dot NET Compact Framework 3 10th Jan 2008 03:37 PM
File.Exists(filePath) always returns false in release mode if filePath contains a white space snow Microsoft VB .NET 12 14th Feb 2007 06:17 AM
q; DirectoryInfo.Exists =?Utf-8?B?SklNLkgu?= Microsoft Dot NET Framework Forms 0 19th Sep 2006 01:33 AM
File.Exists always returns false =?Utf-8?B?SXRhbGlhbiBQZXRl?= Microsoft C# .NET 4 11th Sep 2006 07:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:19 PM.