FileInfo.Exists exhibits weird behavior

G

Guest

I am seeing something I don't understand. I am creating a new file that does not exist. After instantiating StreamWriter passing the file name then FileInfo.Exists is true. Great. If I access FileInfo.Exists before the StreamWriter it is false. Great. Now if I access FileInfo.Exists before and after StreamWriter it always returns false. Why????

See comments in code

using System
using System.IO

class Bu


static void Main(

FileInfo afileinfo = new FileInfo("list.txt")
try

// if you uncomment the next lin

//bool b = afileinfo.Exists

// then Console.WriteLine prints "list.txt does NOT exist!
// even though the file exists
// Also, if you display afileinfo.Exists in the
// debug Locals window it does the same thing

StreamWriter asw = new StreamWriter("list.txt")

if ( afileinfo.Exists
Console.WriteLine("list.txt exists!")
els
Console.WriteLine("list.txt does NOT exist!")

asw.WriteLine("line")
asw.WriteLine("line")
asw.Close()
}
catch (IOException aexception)

System.Console.WriteLine(aexception.Message)
throw new NotSupportedException()

finally

afileinfo.Delete()
}

}
 
J

Jon Skeet [C# MVP]

Rlzsmith said:
I am seeing something I don't understand. I am creating a new file
that does not exist. After instantiating StreamWriter passing the
file name then FileInfo.Exists is true. Great. If I access
FileInfo.Exists before the StreamWriter it is false. Great. Now if I
access FileInfo.Exists before and after StreamWriter it always
returns false. Why?????

I believe it's basically caching the old value. If you create a new
FileInfo instead, it should be fine.
 
M

mikeb

Rlzsmith said:
I am seeing something I don't understand. I am creating a new file that does not exist. After instantiating StreamWriter passing the file name then FileInfo.Exists is true. Great. If I access FileInfo.Exists before the StreamWriter it is false. Great. Now if I access FileInfo.Exists before and after StreamWriter it always returns false. Why?????

See comments in code:

using System;
using System.IO;

class Bug
{

static void Main()
{
FileInfo afileinfo = new FileInfo("list.txt");
try
{
// if you uncomment the next line

//bool b = afileinfo.Exists;

// then Console.WriteLine prints "list.txt does NOT exist!"
// even though the file exists.
// Also, if you display afileinfo.Exists in the
// debug Locals window it does the same thing.

StreamWriter asw = new StreamWriter("list.txt");

if ( afileinfo.Exists )
Console.WriteLine("list.txt exists!");
else
Console.WriteLine("list.txt does NOT exist!");

asw.WriteLine("line");
asw.WriteLine("line");
asw.Close();
}
catch (IOException aexception)
{
System.Console.WriteLine(aexception.Message);
throw new NotSupportedException();
}
finally
{
afileinfo.Delete();
}
}
}

The file attribute information is cached by the FileInfo object. If the
information on the filesystem changes, you'll need to call
afileinfo.Refresh() to get updated attribute information.
 
G

Guest

afileinfo.Refresh()

Ahh. Ok. So why did they not program it to do a refresh on subsequent calls to Exist or other file attributes

Thanks mikeb for the explanation.
 

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

Top