Hello,
I'm trying to develop a small application that saves some data in
simple text files. At startup I test if the file exists, if not I
create one, if it exists but is empty I put some dummy data in it. Here
is the code:
-----
// path = "/My Documents/test/test.txt" (directory test exists!)
...
// create if not exists
if(!File.Exists(path))
{
File.Create(path);
}
// test if empty
FileStream f = new FileStream(path);
if(f.ReadByte() < 0)
{
retVal = false;
}
f.Close();
...
-----
Sometimes I get an IOException in the line "FileStream f = ......".
Why???
Sometimes I don't get this exception but later in the program, when I
try to read from the file (that should exist AND have some dummy data
in it), I get an FileNotFoundException. And really -> there is no file!
Why????
When I debug through the program, the file is often created - so it
seems to me that it is a "timing"-problem. Does the OS have not enough
time to create the file?
Thanks for any Hints!
Stephan