我正在开发以观察文件更改,显示通知并将更改列表插入到listview中。如果更改excel文件的属性,我很难只想更改通知。现在如果更改excel属性,显示创建,删除,重命名通知。 有人可以向我推荐吗?
代码:
string filepath = C:\New Folder;
private void watcher_Changed(object sender, FileSystemEventArgs e)
{
sfilepath = e.FullPath.Substring(0, e.FullPath.Length - e.Name.Length - 1);
if (sfilepath == filepath)
{
FileInfo fileInfo = new FileInfo(e.FullPath);
DateTime lastWriteTime = fileInfo.LastWriteTime;
DateTime nowdt = DateTime.Now;
if (lastWriteTime == nowdt)
{
this.notifyIcon1.ShowBalloonTip(1, "File " + e.ChangeType, e.FullPath, ToolTipIcon.Info);
}
}
}
private void watcher_Created(object sender, FileSystemEventArgs e)
{
sfilepath = e.FullPath.Substring(0, e.FullPath.Length - e.Name.Length - 1);
if (sfilepath == filepath)
{
this.notifyIcon1.ShowBalloonTip(1, "File " + e.ChangeType, e.FullPath, ToolTipIcon.Info);
}
}
private void watcher_Deleted(object sender, FileSystemEventArgs e)
{
sfilepath = e.FullPath.Substring(0, e.FullPath.Length - e.Name.Length - 1);
if (sfilepath == filepath)
{
this.notifyIcon1.ShowBalloonTip(1, "File " + e.ChangeType, e.FullPath, ToolTipIcon.Info);
}
}
private void watcher_Renamed(object sender, RenamedEventArgs e)
{
sfilepath = e.FullPath.Substring(0, e.FullPath.Length - e.Name.Length - 1);
if (sfilepath == filepath)
{
this.notifyIcon1.ShowBalloonTip(1, "File Renamed", e.OldFullPath + " renamed to " + e.FullPath, ToolTipIcon.Info);
}
}
更改清单:
New Microsoft Excel ワークシート.xls 2011/10/10 11:00:15 Created C:\New Folder
B1F38000 2011/10/10 11:00:55 Created C:\New Folder
New Microsoft Excel ワークシート.xls~RF83f213.TMP 2011/10/10 11:01:16 Created C:\New Folder
New Microsoft Excel ワークシート.xls 2011/10/10 11:01:16 Deleted C:\New Folder
New Microsoft Excel ワークシート.xls 2011/10/10 11:01:16 Renamed C:\New Folder
New Microsoft Excel ワークシート.xls~RF83f213.TMP 2011/10/10 11:01:18 Deleted C:\New Folder
答案 0 :(得分:0)
您可以设置FileSystemWatcher
仅关注特定文件夹,因此对filepath
的检查是多余的。
否则,请按照您当前的方式执行操作,但请检查触发事件的文件的文件名,以查看它是否包含XLS
,XLSX
,XLSM
或任何文件上的其他Excel扩展。