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

Re: Cannot create files in .NET compact by Michael

Michael
Tue Mar 14 04:59:08 CST 2006

I develop my application with streamreader/writer. My log-file write
sometimes 5x/sec a line in this log-file. It works fine. No access and write
problems.


"wumpfreak" <wumpfreak@gmail.com> schrieb im Newsbeitrag
news:1142331658.395374.192030@i39g2000cwa.googlegroups.com...
> 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
>



Re: Cannot create files in .NET compact by Peter

Peter
Tue Mar 14 05:01:26 CST 2006

File.Create returns an open stream to the new file and you haven't closed
it - the file will be in use when you then create f. Either do something
like this

FileStream f = null;
if(!File.Exists(path))
{
f = File.Create(path);
}
else
{
f = new FileStream(path);
}

or close the returned filestream in your if block e.g.

if(!File.Exists(path))
{
FileStream fs = File.Create(path);
fs.Close();
}

Peter

--
Peter Foot
Windows Embedded MVP
www.peterfoot.net | www.inthehand.com

"wumpfreak" <wumpfreak@gmail.com> wrote in message
news:1142331658.395374.192030@i39g2000cwa.googlegroups.com...
> 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
>



Re: Cannot create files in .NET compact by wumpfreak

wumpfreak
Wed Mar 15 06:11:21 CST 2006

Peter, thank you for the hint. That was the problem. (beside another
stupid error...)