C#下监视文件改动

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Security;
using  System.Security.Permissions;
using  System.IO;
using  System.Data.SqlClient;
using  WNSS.InfoCollection;


namespace  WNSS
{
    
class FileWatcher
    
{
        
private FileWatcher()
        
{
            
throw new System.NotImplementedException();
        }



        [PermissionSet(SecurityAction.Demand, Name 
= "FullTrust")]
        
public static void StartWatcher(string Filepath, string strFilter, int flag)
        
{
            FileSystemWatcher watcher 
= new FileSystemWatcher();
            
if (flag == 0)
            
{


                watcher.Filter 
= strFilter;
                watcher.Path 
= Filepath;
                watcher.NotifyFilter 
= NotifyFilters.LastAccess | NotifyFilters.LastWrite
                       
| NotifyFilters.FileName | NotifyFilters.DirectoryName;

                
// Add event handlers.
                watcher.Changed += new FileSystemEventHandler(OnChanged);
                watcher.Created 
+= new FileSystemEventHandler(OnChanged);
                watcher.Deleted 
+= new FileSystemEventHandler(OnChanged);
                watcher.Renamed 
+= new RenamedEventHandler(OnRenamed);

                watcher.EnableRaisingEvents 
= true;
            }

            
else
            
{
                watcher.EnableRaisingEvents 
= false;
            }


            
//throw new System.NotImplementedException();
        }
//WatcherFile

        
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);
            WNSSDataSetTableAdapters.FileTableAdapter fileAdapter = new WNSS.WNSSDataSetTableAdapters.FileTableAdapter();

            fileAdapter.Insert(Client.testClientIP,
                DateTime.Now,
                e.ChangeType.ToString(),
                e.FullPath);

        }


        
private static void OnRenamed(object source, RenamedEventArgs e)
        
{
            
// Specify what is done when a file is renamed.
            
//Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
            WNSSDataSetTableAdapters.FileTableAdapter fileAdapter = new WNSS.WNSSDataSetTableAdapters.FileTableAdapter();

            fileAdapter.Insert(Client.testClientIP,
                DateTime.Now,
                
"Rename",
                e.OldFullPath 
+ "," + e.FullPath);


        }


        
public static void StopWatcher()
        
{
            
//watcher.EnableRaisingEvents = false;

            
//throw new System.NotImplementedException();
        }




    }
//class
}
// namesapce
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值