By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,665 Members | 1,952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,665 IT Pros & Developers. It's quick & easy.

Check if File exists

P: n/a
Hi all.

I'm having a problem with this, I have look if a file exists, if don't wait
till it is created and if it exists I need to open it. I do the following:
for (; ; )

{

if (fl.Exists)

{

StreamReader sr = fl.OpenText();

StreamWriter sw = new StreamWriter(FILE_NAME);

String line;

while ((line = sr.ReadLine()) != null)

{

if (!line.Contains("Pagina"))

sw.WriteLine(line);

}

sr.Close();

sw.Close();

}

}

The problem is that when it's checking if the file exists the cpu load is in
100%.

Is there a way to do that check once per second?? like an wait 1 second them
check it again??

Or any other method???

Thx..


Jan 22 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Ricardo,

You could just put the thread to sleep, or have a timer that fires every
minute, but this is an expensive operation.

Check out the FileSystemWatcher class. You can monitor a directory for
changes, and when there are changes, your code will be notified. It is less
intensive than looping and checking the existence of a file. Instead, you
will just be notified when there is a change, and if it is the file you are
looking for, then you can process it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ricardo Luceac" <rl*****@gmail.com> wrote in message
news:eK**************@TK2MSFTNGP11.phx.gbl...
Hi all.

I'm having a problem with this, I have look if a file exists, if don't
wait till it is created and if it exists I need to open it. I do the
following:
for (; ; )

{

if (fl.Exists)

{

StreamReader sr = fl.OpenText();

StreamWriter sw = new StreamWriter(FILE_NAME);

String line;

while ((line = sr.ReadLine()) != null)

{

if (!line.Contains("Pagina"))

sw.WriteLine(line);

}

sr.Close();

sw.Close();

}

}

The problem is that when it's checking if the file exists the cpu load is
in 100%.

Is there a way to do that check once per second?? like an wait 1 second
them check it again??

Or any other method???

Thx..


Jan 22 '06 #2

P: n/a
Hello,

The problem I see is that the loop is continuously looping without
stopping, that's why the cpu is at 100%.

Insert just before the end of the for loop:

Thread.Sleep(1000); //1000 = 1 second

With this method the thread will wait one second, before checking the
file again

--
Regards,
David Hernández Díez
MCDBA MCSD vs6 & .NET
DCE5 .Net1.1 & DCE2 .NET 2.0

Ricardo Luceac wrote:
Hi all.

I'm having a problem with this, I have look if a file exists, if don't wait
till it is created and if it exists I need to open it. I do the following:
for (; ; )

{

if (fl.Exists)

{

StreamReader sr = fl.OpenText();

StreamWriter sw = new StreamWriter(FILE_NAME);

String line;

while ((line = sr.ReadLine()) != null)

{

if (!line.Contains("Pagina"))

sw.WriteLine(line);

}

sr.Close();

sw.Close();

}

}

The problem is that when it's checking if the file exists the cpu load is in
100%.

Is there a way to do that check once per second?? like an wait 1 second them
check it again??

Or any other method???

Thx..


Jan 22 '06 #3

P: n/a
Is there no c# way of saying...

while fileexists=false....
{
// Do nothing...
}

?
"Ricardo Luceac" <rl*****@gmail.com> wrote in message
news:eK**************@TK2MSFTNGP11.phx.gbl...
Hi all.

I'm having a problem with this, I have look if a file exists, if don't
wait till it is created and if it exists I need to open it. I do the
following:
for (; ; )

{

if (fl.Exists)

{

StreamReader sr = fl.OpenText();

StreamWriter sw = new StreamWriter(FILE_NAME);

String line;

while ((line = sr.ReadLine()) != null)

{

if (!line.Contains("Pagina"))

sw.WriteLine(line);

}

sr.Close();

sw.Close();

}

}

The problem is that when it's checking if the file exists the cpu load is
in 100%.

Is there a way to do that check once per second?? like an wait 1 second
them check it again??

Or any other method???

Thx..


Jan 22 '06 #4

P: n/a
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
You could just put the thread to sleep, or have a timer that fires every
minute, but this is an expensive operation.

Check out the FileSystemWatcher class. You can monitor a directory for
changes, and when there are changes, your code will be notified. It is less
intensive than looping and checking the existence of a file. Instead, you
will just be notified when there is a change, and if it is the file you are
looking for, then you can process it.


In my experience, however, it's hard if not impossible to get
FileSystemWatcher to work reliably. When trying to spot a file being
renamed (and then left alone) I tended to get no event when the first
file was renamed, then two (one for each file) when a second file was
renamed. Unfortunately I never managed to reproduce the problem in a
unit test, but it cropped up often enough in reality to add an extra
"just check every so often in case" test.

I seem to remember others hearing about FSW "dropping" events too...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 22 '06 #5

P: n/a
Craig Lister <My*********@thelisters.co.uk> wrote:
Is there no c# way of saying...

while fileexists=false....
{
// Do nothing...
}


Yes, there is. But how often do you check? Is your "do nothing" meant
to be a sleep, or did you really mean to go back to the test
immediately? If so, that's a tight loop which will absolutely *hammer*
the CPU until the file is created - and if whatever's creating that
file is CPU-bound, it could take a long time to arrive...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 22 '06 #6

P: n/a
Agreed. Aye, a sleep would be needed... and.. not sure how you do it in C#
yet, but in Delphi, maybe a Application.ProcessMessages; which basically
refreshes the screen for any changes...
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Craig Lister <My*********@thelisters.co.uk> wrote:
Is there no c# way of saying...

while fileexists=false....
{
// Do nothing...
}


Yes, there is. But how often do you check? Is your "do nothing" meant
to be a sleep, or did you really mean to go back to the test
immediately? If so, that's a tight loop which will absolutely *hammer*
the CPU until the file is created - and if whatever's creating that
file is CPU-bound, it could take a long time to arrive...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 23 '06 #7

P: n/a
Craig Lister wrote:
Agreed. Aye, a sleep would be needed...
Using Thread.Sleep. However, that brings up the question of "How
often"? Using a FileSystemWatcher gets you more immediate feedback -
but only when it works...

(While slightly more complicated, I quite like an approach of using
FileSystemWatcher and also polling once a minute. The overhead from
that polling is minimal, but we still get good response when the
FileSystemWatcher is working.)
and.. not sure how you do it in C#
yet, but in Delphi, maybe a Application.ProcessMessages; which basically
refreshes the screen for any changes...


You'd only need that if this code were running in the UI thread, which
I would hope it's not.
(The equivalent code is Application.DoEvents, which should be a real
rarity to see in C# code.)

Jon

Jan 23 '06 #8

P: n/a
Hi,


The problem is that when it's checking if the file exists the cpu load is
in 100%.
Why?
File.Exist should not be that resource intensive.
Is there a way to do that check once per second?? like an wait 1 second
them check it again??


I use a mix approach, I use FileSystemWatcher to know when the file is
created, but sometimes (very often) the file is still in use when I get this
event and therefore I cannot access it.
so for this I put the thread to sleep for a while and then I try again until
I can open it or a number of intent are done, in the latter an exception is
throw.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 23 '06 #9

P: n/a
Hi,

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
You could just put the thread to sleep, or have a timer that fires
every
minute, but this is an expensive operation.

Check out the FileSystemWatcher class. You can monitor a directory
for
changes, and when there are changes, your code will be notified. It is
less
intensive than looping and checking the existence of a file. Instead,
you
will just be notified when there is a change, and if it is the file you
are
looking for, then you can process it.
In my experience, however, it's hard if not impossible to get
FileSystemWatcher to work reliably.


Totally agree with you, sometimes you get more than one notification per
user single action, of course this is by design and there is nothing that
can be done to improve this :(

I seem to remember others hearing about FSW "dropping" events too...


I have never had this problem, quite the opposite , my problem is taht I
get more than one events.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 23 '06 #10

P: n/a
Hi,

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Craig Lister wrote:
Agreed. Aye, a sleep would be needed...


Using Thread.Sleep. However, that brings up the question of "How
often"? Using a FileSystemWatcher gets you more immediate feedback -
but only when it works...

(While slightly more complicated, I quite like an approach of using
FileSystemWatcher and also polling once a minute. The overhead from
that polling is minimal, but we still get good response when the
FileSystemWatcher is working.)


I do use this approach too, but with one addition too, as I said before
sometimes you get duplicated events for a single user action. so I also
check a timespan when handling the same event, this assure me that I'm
handling a different event.
Of course this works only if you can assure that an action will not happen
inside the timespan.

OP: do you control the creation of the file?

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 23 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.