thank you

解决方案 »

  1.   

    我做得有个例子..当初我监视安装程序的.呵呵..
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace Example120_响应文件系统事件
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.IO.FileSystemWatcher fileSystemWatcher1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
    private System.Windows.Forms.TextBox txtpath;
    private System.Windows.Forms.TextBox txt1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
    this.txtpath = new System.Windows.Forms.TextBox();
    this.txt1 = new System.Windows.Forms.TextBox();
    ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit();
    this.SuspendLayout();
    // 
    // fileSystemWatcher1
    // 
    this.fileSystemWatcher1.EnableRaisingEvents = true;
    this.fileSystemWatcher1.IncludeSubdirectories = true;
    this.fileSystemWatcher1.NotifyFilter = ((System.IO.NotifyFilters)((System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.DirectoryName)));
    this.fileSystemWatcher1.Path = "c:\\";
    this.fileSystemWatcher1.SynchronizingObject = this;
    this.fileSystemWatcher1.Deleted += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Deleted);
    this.fileSystemWatcher1.Renamed += new System.IO.RenamedEventHandler(this.fileSystemWatcher1_Renamed);
    this.fileSystemWatcher1.Created += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Created);
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(392, 8);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(64, 24);
    this.button1.TabIndex = 4;
    this.button1.Text = "清除";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(304, 8);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(64, 24);
    this.button2.TabIndex = 5;
    this.button2.Text = "监视目录";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // folderBrowserDialog1
    // 
    this.folderBrowserDialog1.Description = "请选择要监视的目录";
    this.folderBrowserDialog1.SelectedPath = "C:\\";
    this.folderBrowserDialog1.ShowNewFolderButton = false;
    // 
    // txtpath
    // 
    this.txtpath.Location = new System.Drawing.Point(8, 8);
    this.txtpath.Name = "txtpath";
    this.txtpath.ReadOnly = true;
    this.txtpath.Size = new System.Drawing.Size(288, 21);
    this.txtpath.TabIndex = 7;
    this.txtpath.Text = "c:\\";
    // 
    // txt1
    // 
    this.txt1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
    | System.Windows.Forms.AnchorStyles.Left) 
    | System.Windows.Forms.AnchorStyles.Right)));
    this.txt1.Location = new System.Drawing.Point(0, 40);
    this.txt1.Multiline = true;
    this.txt1.Name = "txt1";
    this.txt1.ReadOnly = true;
    this.txt1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
    this.txt1.Size = new System.Drawing.Size(480, 424);
    this.txt1.TabIndex = 8;
    this.txt1.Text = "";
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(480, 462);
    this.Controls.Add(this.txt1);
    this.Controls.Add(this.txtpath);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "监视中.....";
    ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).EndInit();
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
    {
     
    txt1.Text+=e.FullPath.ToString()+ "已添加到你的目录中.\t";
     
    } private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
    {
    txt1.Text+=e.OldFullPath.ToString() +"已经被重命名为:"+e.Name.ToString()+"\t";   } private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
    {
     
    txt1.Text+=e.FullPath.ToString() +"已经被删除.\t";
     
    } private void button1_Click(object sender, System.EventArgs e)
    {
                txt1.Text="";  
    } private void button2_Click(object sender, System.EventArgs e)
    {
    folderBrowserDialog1.ShowDialog();
    txtpath.Text=folderBrowserDialog1.SelectedPath;
    fileSystemWatcher1.Path = folderBrowserDialog1.SelectedPath;
    }
    }
    }
      

  2.   

    FileSystemWatcher可以监听到文件变动。
    想确定某个文件当前是否正在被写入应该是不可能的,但可以通过打开这个文件来判断此文件是否被其它程序占用。