Re: error creating/writing to log file...strange problem!

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Sergey Bogdanov (sergey.bogdanov_at_gmail.com)
Date: 02/07/05


Date: Mon, 07 Feb 2005 13:40:31 +0200

The problem is that File.Create opens file and creates FileStream and in
the second line you are trying to open this file again. You may remove
your first line because if the file specified by a path does not exist
in File.AppendText, it will be created all the same.

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com

Milsnips wrote:
> this is the code i was using....
>
> If Not IO.File.Exists(AppPath & "\log.txt") Then IO.File.Create(AppPath &
> "\log.txt")
> Dim f As IO.StreamWriter = IO.File.AppendText(AppPath & "\log.txt")
>
>
> it crashes on the second line.
>
> regards,
> Paul
>
> "Doug Forster" <doug_ZAPTHIS_AT_ZAPTHIS_TONIQ_DOT_CO_DOT_NZ> wrote in
> message news:OsDCNZiCFHA.1188@tk2msftngp13.phx.gbl...
>
>>Hi,
>>
>>You seem to think that there is a timing issue involved. This is most
>>unlikely. The file will have been locked because your code left it locked.
>>i.e. you failed to close the file. Though you have solved your problem you
>>need to show us some code if you want to know why you have solved it.
>>
>>Cheers
>>
>>Doug Forster
>>
>>"Milsnips" <milsnips@hotmail.com> wrote in message
>>news:e8hwVOfCFHA.1396@TK2MSFTNGP14.phx.gbl...
>>
>>>yes thats the problem.. i moved the code to create the log file at
>>>opening the app, and problem doesnt appear anymore.
>>>
>>>Is there anyway that i could make it wait until the process has unlocked
>>>the file and then continue?
>>>thanks,
>>>
>>>Paul
>>>"Peter Foot [MVP]" <feedback@nospam-inthehand.com> wrote in message
>>>news:uVQjlLfCFHA.3732@TK2MSFTNGP14.phx.gbl...
>>>
>>>>Can you post a code snippet showing the problem? The exception is most
>>>>likely if the file is still locked when you try to open it.
>>>>
>>>>Peter
>>>>
>>>>--
>>>>Peter Foot
>>>>Windows Embedded MVP
>>>>www.inthehand.com | www.opennetcf.org
>>>>
>>>>"Milsnips" <milsnips@hotmail.com> wrote in message
>>>>news:O34kAkeCFHA.2620@tk2msftngp13.phx.gbl...
>>>>
>>>>>i have a function that checks for a log file, if not exists then it
>>>>>creates
>>>>>it, then opens it and writes a error log.
>>>>>
>>>>>the problem is, first time it calls the event and the file does not
>>>>>exist,
>>>>>it creates it okay, but when it tries to open it straight after that,
>>>>>it
>>>>>throws an IOException.
>>>>>If i close the app, then re-open and add another log to the existing
>>>>>file,
>>>>>it works okay??
>>>>>
>>>>>any help appreciated,
>>>>>
>>>>>thanks,
>>>>>Paul
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>
>
>