0

Windows フォームのデータグリッドを持つ C# アプリケーションがあります。ディレクトリを監視し(FileSystemWatcherを使用しています)、ディレクトリ内のファイルのリストでデータグリッドを更新する必要があります。インターフェイスを設定する方法がわかりません。Windows フォームの Load() から monitorDirectory() を呼び出しても、Load はアプリケーションで 1 回しか呼び出されないため、機能しないようです。

ありがとう

4

4 に答える 4

1

FileSystemWatcher の OnChanged または OnRenamed イベント ハンドラー内でグリッドを更新できます。

以下のリンクの例は、コンソール アプリケーション内でイベントを処理しています。 MSDN FileSystemWatcher クラス

于 2010-05-10T19:36:13.127 に答える
0

FileSystemWatcher を使用したファイル変更の検出には、 FileSystemWatcherの使用方法の良い例があります。

于 2010-05-10T19:38:10.093 に答える
0

FileSystemWatcher オブジェクトからのイベントをリッスンできます。MSDN ページには、これを行う方法に関するいくつかの推奨事項が掲載されています。

基本的に、MonitorDirectory() を呼び出す直前に、FileSystemWatcher の Changed、Created、Deleted、および Renamed イベントをサブスクライブする必要があります。

于 2010-05-10T19:38:15.790 に答える
0

完全なコード

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;
}
}

Form1.cs を次のように置き換えます

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();
    }
}
}
于 2010-05-10T19:42:08.637 に答える