c#使用filesystemwatcher监视文件系统的变化

家电维修 2023-07-16 19:17www.caominkang.com家电维修技术

复制代码 代码如下:
#region 监视文件夹的变化
            FileSystemWatcher atcher = ne FileSystemWatcher();
            atcher.Path = "f:\";
            atcher.NotifyFilter =//被监控的方面
               NotifyFilters.LastWrite |
               NotifyFilters.FileName |
               NotifyFilters.DirectoryName;

            // 订阅一些事件,当它被触发时(.(indos)底层触发它,我们不用管),执行我们的方法
            atcher.Changed += (object source, FileSystemEventArgs e) =>
            {
                Console.WriteLine("文件{0}已经被修改,修改类型{1}", e.FullPath, e.ChangeType.ToString());
            };
            atcher.Created += (object source, FileSystemEventArgs e) =>
            {
                Console.WriteLine("文件{0}被建立", e.FullPath);
            };
            atcher.Deleted += (object source, FileSystemEventArgs e) =>
            {
                Console.WriteLine("文件{0}已经被删除", e.FullPath);
            };
            atcher.Renamed += (object source, RenamedEventArgs e) =>
            {
                Console.WriteLine("文件{0}的名称已经从{1}变成了{2}", e.OldFullPath, e.OldName, e.Name);
            };

            // 为true表示开启FileSystemWatcher组件,反之我们的监控将不启作用
            atcher.EnableRaisingEvents = true;
            #endregion

,告诉大家一个 xor异或运算的使用技巧,就是它可以在不引入第三个变量的情况下,交替两个变量的值,你的变量可以是数值,也可以是字符,如果是字符,我们需要使用它的hashcode值进行xor运算。

复制代码 代码如下:
#region xor两个变量交换
            int a = 2;
            int b = 3;
            Console.WriteLine("a={0}", a);
            Console.WriteLine("b={0}", b);
            a = a ^ b ^ (b = a);
            Console.WriteLine("a={0}", a);
            Console.WriteLine("b={0}", b);
            #endregion

Copyright © 2016-2025 www.jianfeikang.com 建飞家电维修 版权所有 Power by