检查文件是否存在 [英] Check if File exists

查看:73
本文介绍了检查文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。


我有这个问题,我看看文件是否存在,如果不等待

直到它被创建,如果它存在,我需要打开它。我执行以下操作:

for(;;)


{


if(fl.Exists)


{


StreamReader sr = fl.OpenText();


StreamWriter sw = new StreamWriter (FILE_NAME);


字符串行;


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


{


if(!line.Contains(" Pagina))


sw.WriteLine(线路);


}


sr.Close();


sw.Close( );


}


}


问题在于它在检查时如果文件存在,则cpu加载为
100%。


有没有办法每秒检查一次?比如等待1秒他们

再次检查?


或任何其他方法???


Thx ..


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..


推荐答案

里卡多,


你可以放要休眠的线程,或者有一个计时器每隔一段时间触发一次,但这是一个很昂贵的操作。


查看FileSystemWatcher类。您可以监视目录以进行

更改,当有更改时,将通知您的代码。它比循环和检查文件的存在要少得多。相反,你会在有变化时通知
,如果是你要查找的文件,那么你可以处理它。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- < a href =mailto:mv*@spam.guard.caspershouse.com> mv*@spam.guard.caspershouse.com


" Ricardo Luceac" < RL ***** @ gmail.com>在消息中写道

新闻:eK ************** @ TK2MSFTNGP11.phx.gbl ...
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...
大家好。

我遇到了这个问题,我看看文件是否存在,如果不等待它创建,如果它存在,我需要打开它。我做了
以下:

for(;;)

{

if(fl.Exists)
< StreamReader sr = fl.OpenText();

StreamWriter sw = new StreamWriter(FILE_NAME);

字符串行; <同时((line = sr.ReadLine())!= null)

如果(!line.Contains(" Pagina) ))

sw.WriteLine(line);

}
sr.Close();

sw。关闭();

}


问题是,当它检查文件是否存在时,cpu加载是

有没有办法每秒检查一次?喜欢等待1秒
他们再检查一下?

或任何其他方法???

Thx ..

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..



你好,


我看到的问题是循环不断循环而没有

停止,这就是cpu为100%的原因。

在for循环结束前插入:


Thread.sleep代码(1000); // 1000 = 1秒


使用此方法线程将等待一秒钟,然后再次检查

文件


-

问候,

DavidHernándezDíez

MCDBA MCSD vs6& .NET

DCE5 .Net1.1& DCE2 .NET 2.0

Ricardo Luceac写道:
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:
大家好。

我有这个问题,我看看文件是否存在,如果不等待,直到它被创建,如果存在,我需要打开它。我执行以下操作:

for(;;)

{

if(fl.Exists)

{

StreamReader sr = fl.OpenText();

StreamWriter sw = new StreamWriter(FILE_NAME);

字符串行;

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

如果(!line.Contains(" Pagina"))

sw.WriteLine(line);


sr.Close();

sw.Close();

}


问题是当它检查文件是否存在时,cpu加载是否在100中%。

有没有办法每秒检查一次?比如等待1秒他们再次检查一下?

或者其他任何方法???

Thx ..

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..



没有c#的说法...


而fileexists = false ....

{

//什么都不做......

}




Ricardo Luceac < RL ***** @ gmail.com>在消息中写道

新闻:eK ************** @ TK2MSFTNGP11.phx.gbl ...
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...
大家好。

我遇到了这个问题,我看看文件是否存在,如果不等待它创建,如果它存在,我需要打开它。我做了
以下:

for(;;)

{

if(fl.Exists)
< StreamReader sr = fl.OpenText();

StreamWriter sw = new StreamWriter(FILE_NAME);

字符串行; <同时((line = sr.ReadLine())!= null)

如果(!line.Contains(" Pagina) ))

sw.WriteLine(line);

}
sr.Close();

sw。关闭();

}


问题是,当它检查文件是否存在时,cpu加载是

有没有办法每秒检查一次?喜欢等待1秒
他们再检查一下?

或任何其他方法???

Thx ..

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..



这篇关于检查文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆