C#监测文件夹内文件操作

直接用控件,代码如下:

public  static  FileSystemWatcher watcher = new  FileSystemWatcher();

接下来就是为对watcher 的事件捕获并进行相应处理,代码如下:

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

加入下面的代码,能有效地避免程序被误认为是非法操作。

[PermissionSetAttribute(SecurityAction.Demand, Name  = " FullTrust " )]

开机启动

private  void  AutoRun_CheckedChanged( object  sender, EventArgs e)
{
     if  (AutoRun.Checked) //开机自动启动
     {
         try
         {
             RegistryKey runKey = Registry.LocalMachine.OpenSubKey( @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run" , true );
             runKey.SetValue( "文件夹监测.exe" , System.Windows.Forms.Application.ExecutablePath);
             runKey.Close();
         }
         catch  (IOException)
         {
             return  ;
         }
     }
     else   //不开机自动启动注册表信息删除
     {
         RegistryKey software = Registry.LocalMachine.OpenSubKey( @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run" , true );
         string [] aimnames = software.GetValueNames();          
         foreach  ( string  aimKey in  aimnames)
         {
             if  (aimKey.Equals( "文件夹监测.exe" ))
             {
                 software.DeleteValue( "文件夹监测.exe" ) ;
                 software.Close();
                 break ;
             }
         }
     }
}
原文链接:http://www.cnblogs.com/ATree/archive/2011/05/09/net-auto-FileSystemWatcher.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值