我有一個C#應用程序與Windows窗體中的數據網格。我需要監視一個目錄(正在使用FileSystemWatcher)並使用目錄中的文件列表刷新數據網格。我不知道如何設置界面來做到這一點?從窗口形式的Load()調用monitorDirectory()似乎不起作用,因爲Load在應用程序中只調用一次。C#:更新datagrid
感謝
我有一個C#應用程序與Windows窗體中的數據網格。我需要監視一個目錄(正在使用FileSystemWatcher)並使用目錄中的文件列表刷新數據網格。我不知道如何設置界面來做到這一點?從窗口形式的Load()調用monitorDirectory()似乎不起作用,因爲Load在應用程序中只調用一次。C#:更新datagrid
感謝
您可以更新給onChanged或您的FileSystemWatcher的的OnRenamed事件處理程序中的網格。
以下鏈接中的示例是處理控制檯應用程序中的事件。 MSDN FileSystemWatcher Class
您可以收聽到來自FileSystemWatcher的對象事件一個很好的例子。 MSDN page發佈了一些關於如何做到這一點的建議。
本質上,在調用MonitorDirectory()之前,您應該訂閱FileSystemWatcher的Changed,Created,Deleted和Renamed事件。
全碼
添加Form1以項目
更換Form1.Designer.cs與
namespace Test
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(13, 13);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(240, 150);
this.dataGridView1.TabIndex = 0;
//
// fileSystemWatcher1
//
this.fileSystemWatcher1.EnableRaisingEvents = true;
this.fileSystemWatcher1.Path = "c:\\Temp";
this.fileSystemWatcher1.SynchronizingObject = this;
this.fileSystemWatcher1.Changed += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_CreatedDeletedChanged);
this.fileSystemWatcher1.Created += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_CreatedDeletedChanged);
this.fileSystemWatcher1.Deleted += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_CreatedDeletedChanged);
this.fileSystemWatcher1.Renamed += new System.IO.RenamedEventHandler(this.fileSystemWatcher1_Renamed);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(370, 301);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dataGridView1;
private System.IO.FileSystemWatcher fileSystemWatcher1;
}
}
與
using System;
using System.IO;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void PopulateGrid()
{
DirectoryInfo dir = new DirectoryInfo(fileSystemWatcher1.Path);
dataGridView1.DataSource = dir.GetFiles();
}
private void fileSystemWatcher1_CreatedDeletedChanged(object sender, FileSystemEventArgs e)
{
PopulateGrid();
}
private void fileSystemWatcher1_Renamed(object sender, RenamedEventArgs e)
{
PopulateGrid();
}
private void Form1_Load(object sender, EventArgs e)
{
PopulateGrid();
}
}
}
誰調用FileSystemWatcher的替換Form1.cs的?謝謝 – 2010-05-10 20:05:31
如果我理解這個問題。沒有人會稱之爲控制權,它會自行提出事件,讓您知道發生了什麼事情。因此,要測試上面的代碼,請在窗體中添加一個grid和fileSystemWatcher控件,然後將文件添加到您正在觀看的路徑中。 – Mike 2010-05-10 20:51:34