CommandHandler;Fields#regionFieldsprivatestringfileName;privateFileSystemWatcherwatcher;privateSystem." />

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

C#中利用FileSystemWatcher對單個文件內容的監視

系統 2735 0

在項目中需要用 FileSystemWatcher對單個文件內容進行監視,文件中每添加一行則需要Fire相應的事件,代碼如下

??? public ? class ?CommandWatcher
????
{


????????
public ? event ?EventHandler < CommandEventArgs > ?CommandHandler;
??????

????????
Fields ?Fields


????????
public ?CommandWatcher( string ?cmdfile)
????????
{
????????????
this .fileName? = ?cmdfile;
????????????isChangeImmediate?
= ? true ;

?????????
????????}




????????
????????
//
????????
// ?Summary:
????????
// ?????Raises?the?System.IO.FileSystemWatcher.Changed?event.
????????
//
????????
// ?Parameters:
????????
// ???e:
????????
// ?????A?System.IO.FileSystemEventArgs?that?contains?the?event?data.
???????? protected ? void ?OnChanged( object ?sender,?FileSystemEventArgs?e)???
????????
{
????????????ReloadFile();
????????}



????
????????
//
????????
// ?Summary:
????????
// ?????Raises?the?System.IO.FileSystemWatcher.Error?event.
????????
//
????????
// ?Parameters:
????????
// ???e:
????????
// ?????An?System.IO.ErrorEventArgs?that?contains?the?event?data.
???????? protected ? void ?OnError( object ?sender,?ErrorEventArgs?e)
????????
{

????????}


????????
//
????????
// ?Summary:
????????
// ?????Raises?the?System.IO.FileSystemWatcher.Renamed?event.
????????
//
????????
// ?Parameters:
????????
// ???e:
????????
// ?????A?System.IO.RenamedEventArgs?that?contains?the?event?data.
???????? protected ? void ?OnRenamed( object ?sender,?RenamedEventArgs?e)
????????
{

????????}




????????
/// ? <summary>
????????
/// ?Hook?up?for?the?currently?selected?monitoring?method.
????????
/// ? </summary>

???????? public ? void ?Start()
????????
{
????????????
if ?(isChangeImmediate)
????????????
{
????????????????
// ?stop?timer?if?running
????????????????ClearTimer();
????????????????
// ?If?a?file?is?set?and?the?watcher?has?not?been?set?up?yet?then?create?it
???????????????? if ?( null ? != ?fileName? && ? null ? == ?watcher)
????????????????
{
????????????????????
string ?path? = ?Path.GetDirectoryName(fileName);
????????????????????
string ?baseName? = ?Path.GetFileName(fileName);
????????????????????
// ?Use?the?FileSystemWatcher?class?to?detect?changes?to?the?log?file?immediately
????????????????????
// ?We?must?watch?for?Changed,?Created,?Deleted,?and?Renamed?events?to?cover?all?cases
????????????????????watcher? = ? new ?System.IO.FileSystemWatcher(path,?baseName);
????????????????????FileSystemEventHandler?handler?
= ? new ?FileSystemEventHandler(OnChanged);
????????????????????watcher.Changed?
+= ?handler;
????????????????????watcher.Created?
+= ?handler;
????????????????????watcher.Deleted?
+= ?handler;
????????????????????watcher.Renamed?
+= ?OnRenamed;
????????????????????
// ?Without?setting?EnableRaisingEvents?nothing?happens
????????????????????watcher.EnableRaisingEvents? = ? true ;
????????????????}

????????????}

????????????
else
????????????
{
????????????????
// ?On?a?timer?so?clear?the?watcher?if?previously?set?up
????????????????ClearWatcher();
????????????????
int ?numSeconds? = ? 5 ;
???????????????
????????????????
if ?( null ? != ?timer)
????????????????
{
????????????????????
// ?Timer?is?already?running?so?just?make?sure?the?interval?is?correct
???????????????????? if ?(timer.Interval? != ? 1000 ? * ?numSeconds)
????????????????????
{
????????????????????????timer.Interval?
= ? 1000 ? * ?numSeconds;
????????????????????}

????????????????}

????????????????
else
????????????????
{
????????????????????
// ?Fire?up?a?timer?if?the?user?entered?a?valid?time?interval
???????????????????? if ?( 0 ? != ?numSeconds)
????????????????????
{
????????????????????????timer?
= ? new ?System.Windows.Forms.Timer();
????????????????????????timer.Interval?
= ? 1000 ? * ?numSeconds;
????????????????????????timer.Tick?
+= ?timerTick;
????????????????????????timer.Start();
????????????????????}

????????????????}

????????????}

????????????
this .ReloadFile();
????????}


????????
void ?timerTick( object ?sender,?EventArgs?e)
????????
{
????????????UpdateBasedOnFileTime();
????????}



????????
/// ? <summary>
????????
/// ?Update?the?log?file?if?the?modified?timestamp?has?changed?since?the?last?time?the?file?was?read.
????????
/// ? </summary>

???????? private ? void ?UpdateBasedOnFileTime()
????????
{
????????????
if ?( null ? != ?fileName)
????????????
{
????????????????
// ?This?returns?"12:00?midnight,?January?1,?1601?A.D."?if?the?file?does?not?exist
????????????????DateTime?newLastModifiedTime? = ?File.GetLastWriteTime(fileName);
????????????????
if ?((newLastModifiedTime.Year? < ? 1620 ? && ?newLastModifiedTime? != ?lastModifiedTime)
????????????????????
|| ?newLastModifiedTime? > ?lastModifiedTime)
????????????????
{
????????????????????
// OnLogFileChanged();
????????????????????ReloadFile();
????????????????}

????????????}

????????}

????????
/// ? <summary>
????????
/// ?Read?the?log?file.
????????
/// ? </summary>
????????
/// ? <remarks> If?the?user?has?some?text?selected?then?keep?that?text?selected. </remarks>

???????? private ? void ?ReloadFile()
????????
{
????????????
if ?(reloadingFile)? return ;? // ?already?busy?doing?this
????????????reloadingFile? = ? true ;
????????????
try
????????????
{
????????????????
if ?( null ? == ?fileName)
????????????????
{
???????????????????
// ?textBoxContents.Text?=?"";
????????????????}

????????????????
else
????????????????
{
????????????????????
string ?newFileLines? = ? "" ;
????????????????????lastModifiedTime?
= ?File.GetLastWriteTime(fileName);? // ?Remember?when?we?last?read?in?this?file
???????????????????? long ?newLength? = ? 0 ;?? // ?will?be?set?properly?later?if?all?is?well
???????????????????? bool ?fileExists? = ?File.Exists(fileName);
????????????????????
bool ?needToClear? = ? ! fileExists;
????????????????????
if ?(fileExists)
????????????????????
{
????????????????????????
// ?It?is?possible?the?file?will?be?in?use?when?we?read?it?(especially?if?using?Immediate?mode)
????????????????????????
// ?So,?we?will?try?up?to?5?times?to?access?the?file
???????????????????????? int ?count? = ? 0 ;
????????????????????????
bool ?success? = ? false ;
????????????????????????
while ?(count? < ? 5 ? && ? ! success)
????????????????????????
{
????????????????????????????
try
????????????????????????????
{
????????????????????????????????
// ?Open?with?the?least?amount?of?locking?possible
???????????????????????????????? using ?(FileStream?stream? = ?File.Open(fileName,?FileMode.Open,?FileAccess.ReadWrite,?FileShare.ReadWrite))
????????????????????????????????
{
????????????????????????????????????newLength?
= ?stream.Length;
????????????????????????????????????
if ?(newLength? >= ?lastFileSize)
????????????????????????????????????
{
????????????????????????????????????????stream.Position?
= ?lastFileSize;?? // ?Only?read?in?new?lines?added
????????????????????????????????????}

????????????????????????????????????
else
????????????????????????????????????
{
????????????????????????????????????????needToClear?
= ? true ;
????????????????????????????????????}

????????????????????????????????????
using ?(StreamReader?reader? = ? new ?StreamReader(stream))
????????????????????????????????????
{
????????????????????????????????????????newFileLines?
= ?reader.ReadToEnd();

????????????????????????????????????????
if ?(newFileLines? != ? string .Empty)
????????????????????????????????????????
{
????????????????????????????????????????????
if ?( this .CommandHandler? != ? null )
????????????????????????????????????????????????
this .CommandHandler( this ,? new ?CommandEventArgs(newFileLines));
????????????????????????????????????????}

??????????????????????????????????????
????????????????????????????????????????
// string?[]tmp?=?newFileLines.Split(seprater,?StringSplitOptions.RemoveEmptyEntries);

????????????????????????????????????}

????????????????????????????????}

????????????????????????????????success?
= ? true ;
????????????????????????????}

????????????????????????????
catch ?(IOException)
????????????????????????????
{
????????????????????????????????System.Threading.Thread.Sleep(
50 );? // ?Give?a?little?while?to?free?up?file
????????????????????????????}

????????????????????????????
++ count;
????????????????????????}

????????????????????}

????????????????????
// ?Remember?the?current?file?length?so?we?can?read?only?newly?added?log?lines?the?next?time?the?log?file?is?read
????????????????????lastFileSize? = ?newLength;
????????????????????
if (lastFileSize? > ? 1024 ? * ? 1024 ? * ? 2 )
????????????????????
{
????????????????????????File.Delete(fileName);
????????????????????????File.Create(fileName);
????????????????????}
???????????????????
???????????????
????????????????????
if ?( 0 ? != ?newFileLines.Length)
????????????????????
{
????????????????????????
// ?See?if?this?log?file?has?proper?cr/lf?and?if?not?convert
???????????????????????? int ?lastCr? = ?newFileLines.LastIndexOf( ' \n ' );
????????????????????????
if ?( - 1 ? != ?lastCr? && ? 0 ? < ?lastCr)
????????????????????????
{
????????????????????????????
if ?(newFileLines[lastCr? - ? 1 ]? != ? ' \r ' )
????????????????????????????
{
????????????????????????????????
// ?OK,?this?file?only?has?Cr?and?we?need?to?convert?to?CrLf
????????????????????????????????newFileLines? = ?newFileLines.Replace( " \n " ,? " \r\n " );
????????????????????????????}

????????????????????????}

????????????????????}

????????????????????
???????????????????
????????????????}

????????????}

????????????
finally
????????????
{
????????????????
// ?Put?clearing?this?flag?in?a?finally?so?we?know?it?will?be?cleared
????????????????reloadingFile? = ? false ;
????????????}

????????}



????????
/// ? <summary>
????????
/// ?Cleanup?the? <see?cref="Timer"/>
????????
/// ? </summary>

???????? private ? void ?ClearTimer()
????????
{
????????????
if ?( null ? != ?timer)
????????????
{
????????????????timer.Tick?
-= ?timerTick;
????????????????timer.Dispose();
????????????????timer?
= ? null ;
????????????}

????????}


????????
/// ? <summary>
????????
/// ?Cleanup?the? <see?cref="FileSystemWatcher"/>
????????
/// ? </summary>

???????? private ? void ?ClearWatcher()
????????
{
????????????
if ?( null ? != ?watcher)
????????????
{
????????????????FileSystemEventHandler?handler?
= ? new ?FileSystemEventHandler(OnChanged);
????????????????watcher.Changed?
-= ?handler;
????????????????watcher.Created?
-= ?handler;
????????????????watcher.Deleted?
-= ?handler;
????????????????watcher.Renamed?
-= ?OnRenamed;
????????????????watcher.Dispose();
????????????????watcher?
= ? null ;
????????????}

????????}




????}

C#中利用FileSystemWatcher對單個文件內容的監視


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲欧美自拍另类图片色 | 久久一区精品 | 国产精品国偷自产在线 | 小视频在线看 | 范丞丞星座| 一级黄色片欧美 | 久久精品免费 | 超碰免费在线观看 | 狠狠澡夜夜澡人人爽 | 2015小明看日韩成人免费视频 | 亚欧免费视频一区二区三区 | 久久免费福利 | 久草www | 日本精品人妻无码免费大全 | 亚洲播播| 国产高清毛片 | 午夜色a大片在线观看免费 龙珠z在线观看 | 欧美精品一区二区三区在线 | 久久99国产综合精品 | 91中文字幕在线 | 中文字幕在线观看第一页 | 日本一区二区三区免费观看 | 久操中文在线 | 能看的av网站 | 四虎影业 | av国产片| 精品国产一区二区三区久久 | 高清国产福利 | 亚洲男人天堂2021 | 韩国在线精品福利视频在线观看 | 亚洲AV久久无码精品九号 | 曰批全过程40分钟免费视频多人 | 欧美综合一区 | 国产99久久精品一区二区永久免费 | 6080伦理久久亚洲精品 | 国产精品成人在线播放 | 黄色网址进入 | 91精品国产闺蜜国产在线 | 另类视频综合 | 成人国产欧美精品一区二区 | 日本一区二区三区免费观看 |