大家好,我是个初学者,我刚输入的文件监控窗体程序有两个错误,不知道怎么解决,哪位有空提示下,程序如下:
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;namespace FileWhatchTest
{
public partial class Form1 : Form
{
private FileSystemWatcher watcher;
private delegate void FileWatchDelegate(string newFile);
public Form1()
{
InitializeComponent();
this.watcher = new System.IO.FileSystemWatcher();
this.watcher.Renamed += new System.IO.RenamedEventHandler (this.OnRenamed);
this.watcher.Changed += new System.IO.FileSystemEventHandler (this.OnChanged);
this.watcher.Created += new System.IO.FileSystemEventHandler(this.OnCreate);
this.watcher.Deleted += new System.IO.FileSystemEventHandler(this.OnDelete);
DirectoryInfo aDir = new DirectoryInfo(@"c:\FileWatchTest");
if (!aDir.Exists)
{
aDir.Create();
}
}
public void OnChanged(object soure, FileSystemEventArgs e)
{
try
{
StreamWriter sw = new StreamWriter(@"c:\fileWatchTest\test5.txt", true);
sw.WriteLine("File{0} {1}", e.FullPath, e.ChangeType.ToString());
sw.Close();
this.BeginInvoke(new FileWatchDelegate(FileWatch)//错误地方//, "Wrote delete event to log");
}
catch (IOException)
{
lbWatch.Items.Add("文件改变失败".ToString());
}
} public void OnRenamed(object soure, RenamedEventArgs e)
{
try
{
StreamWriter sw = new StreamWriter(@"c:\fileWatchTest\test5.txt", true);
sw.WriteLine("File Rename from{0}to{1}", e.OldName, e.FullPath);
sw.Close();
this.BeginInvoke(new FileWatchDelegate(FileWatch)//错误地方//, "Wrote delete event to log");
}
catch (IOException)
{
lbWatch.Items.Add("文件重命名失败".ToString());
}
}
public void OnDelete(object soure, FileSystemEventArgs e)
{
try
{
StreamWriter sw = new StreamWriter(@"c:\fileWatchTest\test5.txt", true);
sw.WriteLine("File{0} delete", e.FullPath);
sw.Close();
this.BeginInvoke(new FileWatchDelegate(FileWatch)//错误地方//, "Wrote delete event to log");
}
catch (IOException)
{
lbWatch.Items.Add("文件删除失败".ToString());
}
} public void OnCreate(object soure, FileSystemEventArgs e)
{
try
{
StreamWriter sw = new StreamWriter(@"c:\fileWatchTest\test5.txt", true);
sw.WriteLine("File{0} create", e.FullPath );
sw.Close();
this.BeginInvoke(new FileWatchDelegate(FileWatch)//错误地方//, "Wrote delete event to log");
}
catch (IOException)
{
lbWatch.Items.Add("文件创建失败".ToString());
}
} private void btnBrowse_Click(object sender, EventArgs e)
{
if (fOpen.ShowDialog() == DialogResult.OK)
{
tbLocation//错误地方//.Text = fOpen.FileName;
btnWatch.Enabled = true;
}
} private void btnWatch_Click(object sender, EventArgs e)
{
watcher.Path = Path.GetDirectoryName(tbLocation//错误地方//.Text);
watcher.Filter = Path.GetFileName(tbLocation错误地方//.Text);
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size;
lbWatch .Items .Add ("监测到".ToString ()+ tbLocation//错误地方//.Text);
watcher .EnableRaisingEvents =true ;
} private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
系统说:Error 2 The name 'FileWatch' does not exist in the current context D:\My Documents\Visual Studio 2008\Projects\FileWatchTest\FileWatchTest\Form1.cs 57 56 FileWatchTest
不知道怎么解决,希望高手们指点。
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;namespace FileWhatchTest
{
public partial class Form1 : Form
{
private FileSystemWatcher watcher;
private delegate void FileWatchDelegate(string newFile);
public Form1()
{
InitializeComponent();
this.watcher = new System.IO.FileSystemWatcher();
this.watcher.Renamed += new System.IO.RenamedEventHandler (this.OnRenamed);
this.watcher.Changed += new System.IO.FileSystemEventHandler (this.OnChanged);
this.watcher.Created += new System.IO.FileSystemEventHandler(this.OnCreate);
this.watcher.Deleted += new System.IO.FileSystemEventHandler(this.OnDelete);
DirectoryInfo aDir = new DirectoryInfo(@"c:\FileWatchTest");
if (!aDir.Exists)
{
aDir.Create();
}
}
public void OnChanged(object soure, FileSystemEventArgs e)
{
try
{
StreamWriter sw = new StreamWriter(@"c:\fileWatchTest\test5.txt", true);
sw.WriteLine("File{0} {1}", e.FullPath, e.ChangeType.ToString());
sw.Close();
this.BeginInvoke(new FileWatchDelegate(FileWatch)//错误地方//, "Wrote delete event to log");
}
catch (IOException)
{
lbWatch.Items.Add("文件改变失败".ToString());
}
} public void OnRenamed(object soure, RenamedEventArgs e)
{
try
{
StreamWriter sw = new StreamWriter(@"c:\fileWatchTest\test5.txt", true);
sw.WriteLine("File Rename from{0}to{1}", e.OldName, e.FullPath);
sw.Close();
this.BeginInvoke(new FileWatchDelegate(FileWatch)//错误地方//, "Wrote delete event to log");
}
catch (IOException)
{
lbWatch.Items.Add("文件重命名失败".ToString());
}
}
public void OnDelete(object soure, FileSystemEventArgs e)
{
try
{
StreamWriter sw = new StreamWriter(@"c:\fileWatchTest\test5.txt", true);
sw.WriteLine("File{0} delete", e.FullPath);
sw.Close();
this.BeginInvoke(new FileWatchDelegate(FileWatch)//错误地方//, "Wrote delete event to log");
}
catch (IOException)
{
lbWatch.Items.Add("文件删除失败".ToString());
}
} public void OnCreate(object soure, FileSystemEventArgs e)
{
try
{
StreamWriter sw = new StreamWriter(@"c:\fileWatchTest\test5.txt", true);
sw.WriteLine("File{0} create", e.FullPath );
sw.Close();
this.BeginInvoke(new FileWatchDelegate(FileWatch)//错误地方//, "Wrote delete event to log");
}
catch (IOException)
{
lbWatch.Items.Add("文件创建失败".ToString());
}
} private void btnBrowse_Click(object sender, EventArgs e)
{
if (fOpen.ShowDialog() == DialogResult.OK)
{
tbLocation//错误地方//.Text = fOpen.FileName;
btnWatch.Enabled = true;
}
} private void btnWatch_Click(object sender, EventArgs e)
{
watcher.Path = Path.GetDirectoryName(tbLocation//错误地方//.Text);
watcher.Filter = Path.GetFileName(tbLocation错误地方//.Text);
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size;
lbWatch .Items .Add ("监测到".ToString ()+ tbLocation//错误地方//.Text);
watcher .EnableRaisingEvents =true ;
} private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
系统说:Error 2 The name 'FileWatch' does not exist in the current context D:\My Documents\Visual Studio 2008\Projects\FileWatchTest\FileWatchTest\Form1.cs 57 56 FileWatchTest
不知道怎么解决,希望高手们指点。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货