先看个效果20130415.
C#监视文件夹,显示文件夹操作到listView上
代码实现,以前在学校生活写的,就几句代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace FileWatch
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private FileSystemWatcher watcher = new FileSystemWatcher();
private delegate void mydel(String str);//声明一个委托
private mydel MyDel;//声明一个全局的委托实例
/// <summary>
/// 选择文件夹并开始监视
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnChooseFolder_Click(object sender, EventArgs e)
{
FolderBrowserDialog fd = new FolderBrowserDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
txbFolder.Text = fd.SelectedPath;
FileSystemWatch(txbFolder.Text);
MyDel = new mydel(AddTolist);
}
}
/// <summary>
/// 增加到列表
/// </summary>
/// <param name="str"></param>
private void AddTolist(string str)
{
lstbResult.Items.Add(str);
}
/// <summary>
/// 获取对文件夹操作的信息
/// </summary>
/// <param name="str">信息字符串</param>
private void GetInfo(string str)
{
lstbResult.Invoke(MyDel, str);
}
#region 文件监视操作
private void FileSystemWatch(string path)
{
watcher.Path = path;
watcher.Created += new FileSystemEventHandler(OnCreate);
watcher.Deleted += new FileSystemEventHandler(OnDelete);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
watcher.IncludeSubdirectories = true;
watcher.EnableRaisingEvents = true;
}
private void OnChanged(object source, FileSystemEventArgs e)
{
GetInfo(e.Name + " 被 修改. " + DateTime.Now.ToShortTimeString());
}
private void OnCreate(object source, FileSystemEventArgs e)
{
GetInfo(e.Name + " 被 创建." + DateTime.Now.ToShortTimeString());
}
private void OnDelete(object source, FileSystemEventArgs e)
{
GetInfo(e.Name + " 被 删除." + DateTime.Now.ToShortTimeString());
}
private void OnRenamed(object source, FileSystemEventArgs e)
{
GetInfo(e.Name + " 被 重命名" + DateTime.Now.ToShortTimeString());
}
#endregion
}
}
java回调
public interface CallBack{
public void callBack();
}