⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 12.2.txt

📁 《Microsoft Visual C# .NET 2003开发技巧大全》源代码
💻 TXT
字号:
Listing 12.2 The FileSystemWatcher Application
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace _11_FileWatcher
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox tbPath;
private System.Windows.Forms.Button btnBrowse;
private System.Windows.Forms.TextBox tbActivity;
private System.Windows.Forms.GroupBox Activity;
private System.Windows.Forms.Button btnGo;
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
private System.IO.FileSystemWatcher fileSystemWatcher1;
private System.Windows.Forms.CheckBox cbIncludeSubdirs;
private System.Windows.Forms.TextBox tbPattern;
private System.Windows.Forms.Label label1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
// fileSystemWatcher1
this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
this.fileSystemWatcher1.Deleted += new
System.IO.FileSystemEventHandler(
this.fileSystemWatcher1_Deleted);
this.fileSystemWatcher1.Renamed += new
System.IO.RenamedEventHandler(
this.fileSystemWatcher1_Renamed);
this.fileSystemWatcher1.Changed += new
System.IO.FileSystemEventHandler(
this.fileSystemWatcher1_Changed);
this.fileSystemWatcher1.Created += new
System.IO.FileSystemEventHandler(
this.fileSystemWatcher1_Created);
// remaining windows forms control code removed
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnBrowse_Click(object sender, System.EventArgs e)
{
// choose which folder to monitor
if( folderBrowserDialog1.ShowDialog() == DialogResult.OK )
tbPath.Text = folderBrowserDialog1.SelectedPath;
}
private void btnGo_Click(object sender, System.EventArgs e)
{
if( btnGo.Text == “Stop” )
{
// stop monitoring
fileSystemWatcher1.EnableRaisingEvents = false;
// change button text back to Go
btnGo.Text = “Go”;
}
else
{
// make sure path exists
if( Directory.Exists( tbPath.Text ) == false )
{
tbActivity.Text = “Directory \”” + tbPath.Text +
“\” does not exist.”;
return;
}
// set filesystemwatcher properties
fileSystemWatcher1.Path = tbPath.Text;
fileSystemWatcher1.Filter = tbPattern.Text;
fileSystemWatcher1.IncludeSubdirectories =
cbIncludeSubdirs.Checked;
// start monitoring
fileSystemWatcher1.EnableRaisingEvents = true;
btnGo.Text = “Stop”;
}
}
private void fileSystemWatcher1_Changed(object sender,
System.IO.FileSystemEventArgs e)
{
tbActivity.Text += “\r\nChanged: “ + e.FullPath;
}
private void fileSystemWatcher1_Created(object sender,
System.IO.FileSystemEventArgs e)
{
tbActivity.Text += “\r\nCreated: “ + e.FullPath;
}
private void fileSystemWatcher1_Deleted(object sender,
System.IO.FileSystemEventArgs e)
{
tbActivity.Text += “\r\nDeleted: “ + e.FullPath;
}
private void fileSystemWatcher1_Renamed(object sender,
System.IO.RenamedEventArgs e)
{
tbActivity.Text += “\r\nRenamed from “ + e.OldFullPath +
“ to “ + e.FullPath;
}
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -