网上现在有Delphi的例子和C++的吧?
谁在C#中实现过?VB.NET也行.或者谁在.NET下使用过ICopyHook::CopyCallback 
如何使用,给段代码也行.下面是一个例子-但不是.NET的.
http://dev.csdn.net/develop/article/22/22347.shtm

解决方案 »

  1.   

    private void Form1_Load(object sender, System.EventArgs e)
    {
    FileSystemWatcher p = new FileSystemWatcher("d:\\");
    p.EnableRaisingEvents =  true;
    p.IncludeSubdirectories =  true;
    p.Changed+=new FileSystemEventHandler(p_Changed);
    p.Created+=new FileSystemEventHandler(p_Created);
    p.Deleted+=new FileSystemEventHandler(p_Deleted);

    } private void p_Changed(object sender, FileSystemEventArgs e)
    {
    string s = e.Name;

    this.listBox1.Items.Add(s+"文件被更改于 "+DateTime.Now);
    } private void p_Created(object sender, FileSystemEventArgs e)
    {
    string s = e.Name;

    this.listBox1.Items.Add(s+"文件被创建于 "+DateTime.Now);
    } private void p_Deleted(object sender, FileSystemEventArgs e)
    {
    string s = e.Name;

    this.listBox1.Items.Add(s+"文件被删除于 "+DateTime.Now);
    }
      

  2.   

    能监视的只有文件创建,修改,删除,重名名,属性修改
    至于如何判断正在修改和已经修改完成,这个FileSystemWatcher做不到
    一般是开始修改前随便设置一个属性,修改完再设置回来,用这个属性作为标志位判断
      

  3.   

    Sunmast(速马/Maybe I'm Amazed)
    问题是,我用这个函数监控其他程序生产的文件,
    所以没法设置标志,有没有其他方法?
      

  4.   

    那只能尝试打开它
    如果外部程序还没写完,那文件必然还被占用,那么你的打开文件的代码会抛出异常
    用try/catch对判断以前看到过一个列举占用某个文件的所有进程的例子,巨复杂,我也忘了~
      

  5.   

    兄弟你看看这个监控的例子
    http://www.gxgl.com/?action=soft&module=show&id=52
      

  6.   

    using System;
    using System.IO ;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace FileSystemWatcher
    {
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.IO.FileSystemWatcher fileSystemWatcher1;
    private System.Windows.Forms.TextBox txtDir;
    private System.Windows.Forms.Button btnSetPath;
    private System.Windows.Forms.TextBox txtInfo;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent(); //
    // TODO: Add any constructor code after InitializeComponent call
    //
    } /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (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.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
    this.txtDir = new System.Windows.Forms.TextBox();
    this.btnSetPath = new System.Windows.Forms.Button();
    this.txtInfo = new System.Windows.Forms.TextBox();
    ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit();
    this.SuspendLayout();
    // 
    // fileSystemWatcher1
    // 
    this.fileSystemWatcher1.EnableRaisingEvents = true;
    this.fileSystemWatcher1.SynchronizingObject = this;
    this.fileSystemWatcher1.Changed += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Changed);
    this.fileSystemWatcher1.Created += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Created);
    this.fileSystemWatcher1.Renamed += new System.IO.RenamedEventHandler(this.fileSystemWatcher1_Renamed);
    this.fileSystemWatcher1.Deleted += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Deleted);
    // 
    // txtDir
    // 
    this.txtDir.Location = new System.Drawing.Point(40, 24);
    this.txtDir.Name = "txtDir";
    this.txtDir.Size = new System.Drawing.Size(208, 21);
    this.txtDir.TabIndex = 0;
    this.txtDir.Text = "";
    // 
    // btnSetPath
    // 
    this.btnSetPath.Location = new System.Drawing.Point(112, 64);
    this.btnSetPath.Name = "btnSetPath";
    this.btnSetPath.TabIndex = 1;
    this.btnSetPath.Text = "设置路径";
    this.btnSetPath.Click += new System.EventHandler(this.btnSetPath_Click);
    // 
    // txtInfo
    // 
    this.txtInfo.Location = new System.Drawing.Point(40, 104);
    this.txtInfo.Multiline = true;
    this.txtInfo.Name = "txtInfo";
    this.txtInfo.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
    this.txtInfo.Size = new System.Drawing.Size(208, 176);
    this.txtInfo.TabIndex = 2;
    this.txtInfo.Text = "";
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 293);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.txtInfo,
      this.btnSetPath,
      this.txtDir});
    this.Name = "Form1";
    this.Text = "文件系统监视器";

    ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).EndInit();
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void btnSetPath_Click(object sender, System.EventArgs e)
    {
    fileSystemWatcher1.Path=txtDir.Text;
    } private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
    {
    txtInfo.Text += "ChangeType :: " + e.ChangeType.ToString() + "\nFullPath ::" + e.FullPath.ToString() + "\n\n"; } protected void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)

    txtInfo.Text += "ChangeType :: " + e.ChangeType.ToString() + "\nFullPath ::" + e.FullPath.ToString() + "\n\n";


     
      
     
     protected void fileSystemWatcher1_Deleted(object sender, FileSystemEventArgs e)

      txtInfo.Text += "ChangeType :: " + e.ChangeType.ToString() + "\nFullPath ::" + e.FullPath.ToString() + "\n\n";

    protected void fileSystemWatcher1_Renamed(object sender, RenamedEventArgs e)
    {
    txtInfo.Text += "ChangeType :: " + e.ChangeType.ToString() + "\nFullPath ::" + e.FullPath.ToString() + "\nOld FileName :: "+ e.OldName.ToString() + "\n\n";
    } }
    }
      

  7.   

    FileSystemWatcher 不太强,只有简单的几个功能
      

  8.   

    看来只能这样了,我这两天这用它写服务呢,
    看了一个CSDN的例子,结果照做还是不行。
    网上到有些好用的例子,可惜我想要的那部分他们都给省略了。
    谁知道如何写服务的安装文件???
    哪写类或者事件,需要单独安装注册?
    //
    在初始化安装时发生异常:
    System.IO.FileNotFoundException:找不到文件或程序集名称“Documents”,或找不到它
    的一个依赖项。。
    //
    谁遇到过,怎么解决?大家别急,等我把服务写完了,就过来结贴。还希望大家多多帮忙啊?
      

  9.   

    Sunmast(速马/Maybe I'm Amazed) 快来帮忙啊。在初始化安装时发生异常:
    System.IO.FileNotFoundException:找不到文件或程序集名称“Documents”,或找不到它
    的一个依赖项。。
    安装文件也做了,该设置的也设置了,里面没用什么东西,只在Start时用了上面提到的文件监控,其他都没有用。
    我用空项目做了一个服务,什么代码也没写。安装也做了,还是提示上面的问题。
    ?????????????????
      

  10.   

    但是你没说清楚问题嗯什么叫"服务的安装文件"
    在做一个Windows服务程序?还是安装程序?和你这个帖子的问题有什么关系?
      

  11.   

    Windows服务不都有安装文件吗?
    如果你写过Windows服务的话应该很清楚吧?
    我当初问这个问题就是想写这个文件监控的服务....我把文件监控的代码加入到 服务的的 Onstart 过程中。网上有有个监控文件夹的例子,他是把文件夹使用的(删除,添加,修改)频率记下来
    我用InstallUtil.exe 安装服务成功了, 但是我稍修改一下就不行了,
    我又自己写了一个,里面没了一点代码,然后再安装就出现上面的问题了。
      

  12.   

    为什么Windows服务的程序一定要安装文件
    生成exe文件后,用InstallUtil注册不就可以了么你把示例的代码改错了,又提供不了详细的错误信息,又看不到你的代码,别人如何知道错在哪里?
    还不如先好好研究一下Windows服务程序的结构(用VS.NET的模版新建个程序),再加入自己的代码,出错了再来问
      

  13.   

    这里还有个例子,所有相关内容几乎都已经涉及了:
    http://dev.csdn.net/article/20/20794.shtm
      

  14.   

    大斑竹也是个夜猫子啊。我刚从国外的网站找了个,牛人写的
    http://www.gotdotnet.com/Community/UserSamples/Download.aspx?SampleGuid=ACC5EAD1-10C5-4526-ADA3-7C7B7D64E25D