我喜欢在form1中添加控件,其中一个事件处理程序为 watcher.Changed + = new FileSystemEventHandler(OnChanged);定义,是有可能例如列表框控件添加到Form1,但需要定义它如何在WinForm中触发事件时添加控件?
/*event added*/
private void btn_start_Click(object sender, EventArgs e)
{
string[] args = {this.txtfolder.Text};
if (args.Length != 1)
{
// Display the proper way to call the program.
Console.WriteLine("Usage: Invalid Operation");
return;
}
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[0];
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = this.txtfilter.Text;//"*.txt";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
// watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
//Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
// Form1 F ;
// ListBox lst = new ListBox();
//lst.Items.Add("File: " + e.FullPath + " " + e.ChangeType.ToString());
//f.lsttracker.Items.Add("File: " + e.FullPath + " " + e.ChangeType.ToString());
// F.controls.Add(lst);
是的,这是可能的。但每次事件发生时都会添加,这可能不是您想要的。很难说出你的代码是在展示什么。为什么所有这些东西都被注释掉了? – 2012-02-02 04:22:33
你试过了吗? (注意:是否有可能通常最终被降低的问题)。 – 2012-02-02 04:24:41
@selvannand尝试此示例。我认为这是做你正在做的事情:dl.dropbox.com/u/18919663/vs%20samples/TestingSandbox.zip – Corylulu 2012-02-02 20:48:05