資源簡(jiǎn)介
試試監(jiān)視指定文件夾內(nèi)文件的變化
public WatcherTimer(FileSystemEventHandler watchHandler)
{
m_timer = new System.Threading.Timer(new TimerCallback(OnTimer), null, Timeout.Infinite, Timeout.Infinite);
fswHandler = watchHandler;
}
public WatcherTimer(FileSystemEventHandler watchHandler, int timerInterval)
{
m_timer = new System.Threading.Timer(new TimerCallback(OnTimer), null, Timeout.Infinite, Timeout.Infinite);
TimeoutMillis = timerInterval;
fswHandler = watchHandler;
}
public void OnFileChanged(object sender, FileSystemEventArgs e)
{
//MessageBox.Show("Created", "Create triggered");
Mutex mutex = new Mutex(false, "FSW");
mutex.WaitOne();
if (!files.Contains(e.Name))
{
files.Add(e.Name);
}
mutex.ReleaseMutex();
m_timer.Change(TimeoutMillis, Timeout.Infinite);
}
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
using?System.IO;
using?System.Threading;
using?System.Collections;
namespace?FileMonitor
{
????public?partial?class?Form1?:?Form
????{
????????private?int?TimeoutMillis?=?100;
????????System.IO.FileSystemWatcher?fsw?=?new?System.IO.FileSystemWatcher();
????????WatcherTimer?watcher?=?null;
????????public?Form1()
????????{
????????????InitializeComponent();
????????????watcher?=?new?WatcherTimer(fsw_Changed?TimeoutMillis);
????????}
????????private?void?fileWatcher_Created(object?sender?System.IO.FileSystemEventArgs?e)
????????{
????????????string?file?=?e.FullPath;
???????????
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-02-04?09:22??FileMonitor\
?????目錄???????????0??2017-01-25?15:44??FileMonitor\Backup\
?????文件????????3811??2008-07-04?13:13??FileMonitor\Backup\FileMonitor.csproj
?????文件?????????911??2008-07-04?02:29??FileMonitor\Backup\FileMonitor.sln
?????文件???????58368??2017-01-25?15:42??FileMonitor\Backup\FileMonitor.v11.suo
?????文件????????8296??2008-07-04?00:59??FileMonitor\Backup\Form1.Designer.cs
?????文件????????3847??2008-07-05?12:04??FileMonitor\Backup\Form1.cs
?????文件????????5814??2008-07-04?00:59??FileMonitor\Backup\Form1.resx
?????文件?????????503??2008-04-04?15:09??FileMonitor\Backup\Program.cs
?????目錄???????????0??2017-01-25?15:44??FileMonitor\Backup\Properties\
?????文件????????1434??2008-04-04?15:09??FileMonitor\Backup\Properties\AssemblyInfo.cs
?????文件????????2851??2008-04-04?15:09??FileMonitor\Backup\Properties\Resources.Designer.cs
?????文件????????5612??2008-04-04?15:09??FileMonitor\Backup\Properties\Resources.resx
?????文件????????1096??2008-04-04?15:09??FileMonitor\Backup\Properties\Settings.Designer.cs
?????文件?????????249??2008-04-04?15:09??FileMonitor\Backup\Properties\Settings.settings
?????文件????????2008??2008-07-05?12:05??FileMonitor\Backup\WatcherTimer.cs
?????文件????????4000??2017-01-25?15:44??FileMonitor\FileMonitor.csproj
?????文件????????1410??2017-02-04?09:46??FileMonitor\FileMonitor.sln
?????文件???????58368??2017-01-25?15:42??FileMonitor\FileMonitor.suo
?????文件??????143872??2017-02-04?13:08??FileMonitor\FileMonitor.v11.suo
?????文件????????8883??2017-02-04?09:46??FileMonitor\Form1.Designer.cs
?????文件????????3846??2017-02-04?09:46??FileMonitor\Form1.cs
?????文件????????5814??2017-02-04?09:46??FileMonitor\Form1.resx
?????文件?????????503??2008-04-04?15:09??FileMonitor\Program.cs
?????目錄???????????0??2017-01-25?15:41??FileMonitor\Properties\
?????文件????????1434??2008-04-04?15:09??FileMonitor\Properties\AssemblyInfo.cs
?????文件????????2862??2017-01-25?15:44??FileMonitor\Properties\Resources.Designer.cs
?????文件????????5612??2008-04-04?15:09??FileMonitor\Properties\Resources.resx
?????文件????????1111??2017-01-25?15:44??FileMonitor\Properties\Settings.Designer.cs
?????文件?????????249??2008-04-04?15:09??FileMonitor\Properties\Settings.settings
?????目錄???????????0??2017-02-04?10:52??FileMonitor\TestFileMonitor\
............此處省略64個(gè)文件信息
評(píng)論
共有 條評(píng)論