473,761 Members | 6,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(FI LE_NAME);

String line;

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

{

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

sw.WriteLine(li ne);

}

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 14401
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 FileSystemWatch er 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.co m

"Ricardo Luceac" <rl*****@gmail. com> wrote in message
news:eK******** ******@TK2MSFTN GP11.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(FI LE_NAME);

String line;

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

{

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

sw.WriteLine(li ne);

}

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(10 00); //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(FI LE_NAME);

String line;

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

{

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

sw.WriteLine(li ne);

}

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=fals e....
{
// Do nothing...
}

?
"Ricardo Luceac" <rl*****@gmail. com> wrote in message
news:eK******** ******@TK2MSFTN GP11.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(FI LE_NAME);

String line;

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

{

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

sw.WriteLine(li ne);

}

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.c om> 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 FileSystemWatch er 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
FileSystemWatch er 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.co m>
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*********@th elisters.co.uk> wrote:
Is there no c# way of saying...

while fileexists=fals e....
{
// 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.co m>
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.Pro cessMessages; which basically
refreshes the screen for any changes...
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Craig Lister <My*********@th elisters.co.uk> wrote:
Is there no c# way of saying...

while fileexists=fals e....
{
// 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.co m>
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 FileSystemWatch er gets you more immediate feedback -
but only when it works...

(While slightly more complicated, I quite like an approach of using
FileSystemWatch er and also polling once a minute. The overhead from
that polling is minimal, but we still get good response when the
FileSystemWatch er is working.)
and.. not sure how you do it in C#
yet, but in Delphi, maybe a Application.Pro cessMessages; 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.DoE vents, 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 FileSystemWatch er 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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard .caspershouse.c om> 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 FileSystemWatch er 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
FileSystemWatch er 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
34745
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
4518
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 "ENOENT", The file doesn't exist.
7
33469
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
2753
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 C:\test1.mdb. My problem occurs if there is already a C:\temp.mdb file. I don’t want to just delete this, as it could have been created separately by a user. In my code, I set a constant as follows: private const string tempMdb = @"C:\temp.mdb";
2
2453
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 if the path and or file exists in vb6 I would write if dir(strFileName & "\test.zip")) <>"" then msgbox("OK 1 ") else msgbox("OK 2 ")
5
11109
by: sword | last post by:
How can I check whether a file exists?
14
46243
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 certain name exists before the user creates a new file of that name. Thanks.
3
4168
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 String.Format("~/images/categories/{0}", sFileName); else if(File.Exists(String.Format("~/images/products/{0}", sFileName)) return String.Format("~/images/products/{0}", sFileName); else return "";
2
9843
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 the document headers, not the data. request.Credentials = System.Net.CredentialCache.DefaultCredentials; // This may throw a WebException:
0
9531
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.