473,230 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 software developers and data experts.

Check if File exists

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
10 14304
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: newcomer | last post by:
Is there a way to check if a file exists in Javascript? This is what I'm trying to do: if(thisfile.htm exists) do this else do that
10
by: Raymond | last post by:
Hi All: To find a file exists using the file name, I have tow routings on UNIX system. 1. access(2) 2. lstat(2) This tow function also can do. When the return value is "-1" and errno is...
7
by: andylcx | last post by:
Hi all: The c++ language can check whether the file exist or not. I am wondering how c language does this job? Thanks a lot! Andy
5
by: jez123456 | last post by:
Hi, I’ve written a c# program to compact certain msaccess databases. The way this works is to compact say C:\test1.mdb to C:\temp.mdb, then delete C:\test1.mdb and rename C:\temp.mdb as...
2
by: Adrian | last post by:
hi I'm writing, or trying to! an ASP page that checks if a file exists and if so transfers it. the use sends a query string with the name of the file, I have this bit working, but need to check...
5
by: sword | last post by:
How can I check whether a file exists?
14
by: John Salerno | last post by:
What is the best way to check if a file already exists in the current directory? I saw os.path.isfile(), but I'm not sure if that does more than what I need. I just want to check if a file of a...
3
by: trint | last post by:
How can I do this with my c# code with my website(because the file is there, but the code doesn't return it)?: if(File.Exists(String.Format("~/images/categories/{0}", sFileName)) return...
2
Manikgisl
by: Manikgisl | last post by:
HI. How to check File exists in Web Share C# try { WebRequest request = HttpWebRequest.Create("http://www.microsoft.com/NonExistantFile.aspx"); request.Method = "HEAD"; // Just get...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.