当前位置:编程学习 > C#/ASP.NET >>

关于文件监控FileSystemWatcher使用 以及文件传输 Sockets使用

在使用FileSystemWatcher 过程中. 如何避免一次粘贴文件过多导致触发过快. 

FileSystemWatcher watcher = new FileSystemWatcher();
            if (!Directory.Exists(path)) Console.WriteLine("监控路径不存在.");
            watcher.Path = path;
            watcher.Filter = filter;
            watcher.Created += new FileSystemEventHandler(OnProcess);
            watcher.Deleted += new FileSystemEventHandler(OnProcess);
            watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess
                                  | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
            watcher.IncludeSubdirectories = true;
            watcher.EnableRaisingEvents = true;


以及在sockets大文件传输中线程调用文件  触发 文件已经被另外一个线程使用 异常?

 //创建一个文件对象   
            FileInfo EzoneFile = new FileInfo(CurrentPath);
            //打开文件流   
            FileStream EzoneStream = new FileStream(CurrentPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            //包的大小   
            int PacketSize = int.Parse(EzoneFile.Length.ToString());
            //包的数量   
            int PacketCount = (int)(EzoneStream.Length / ((long)PacketSize));
            //最后一个包的大小   
            int LastDataPacket = (int)(EzoneStream.Length - ((long)(PacketSize * PacketCount)));
            
            //指向远程服务端节点   
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ServerIP), PortNum);
            //创建套接字   
            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //连接到发送端   
            client.Connect(ipep);
--------------------编程问答-------------------- 第二个,你肯定是多个文件流访问同一个文件了 --------------------编程问答--------------------

private static void WatcherStrat(string path, string filter)
        {
            FileSystemWatcher watcher = new FileSystemWatcher();
            if (!Directory.Exists(path)) Console.WriteLine("监控路径不存在.");
            watcher.Path = path;
            watcher.Filter = filter;
            watcher.Created += new FileSystemEventHandler(OnProcess);
            watcher.Deleted += new FileSystemEventHandler(OnProcess);
            watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
            watcher.IncludeSubdirectories = true;
            watcher.EnableRaisingEvents = true;
        }

        private static void OnProcess(object source, FileSystemEventArgs e)
        {
         
            if (e.ChangeType == WatcherChangeTypes.Created)
            {
                OnCreated(source, e);
            }
            else if (e.ChangeType == WatcherChangeTypes.Deleted)
            {
                OnDeleted(source, e);
            }
        }

        private static void OnCreated(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("文件新建事件处理逻辑 {0}  {1}  {2} ", e.ChangeType, e.FullPath, e.Name);
            CurrentPath = e.FullPath;
            Send();
        }
        private static void OnDeleted(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("文件删除事件处理逻辑 {0}  {1}  {2}", e.ChangeType, e.FullPath, e.Name);
        }


        private static void Send()
        {
            //ThreadPool.QueueUserWorkItem(new WaitCallback(StartSend), fullpath);
            Thread thread = new Thread(new ThreadStart(StartSend));
//就是这里开启了一个线程去传输文件
            thread.Start();
            //thread.Join();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filepath"></param>
        /// <param name="serverip"></param>
        /// <param name="port">端口号</param>
        private static void StartSend()
        {
            //创建一个文件对象   
            FileInfo EzoneFile = new FileInfo(CurrentPath);
            //打开文件流   
            FileStream EzoneStream = new FileStream(CurrentPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//这里报错. 文件已被另外一个线程引用
            //包的大小   
            int PacketSize = int.Parse(EzoneFile.Length.ToString());
            //包的数量   
            int PacketCount = (int)(EzoneStream.Length / ((long)PacketSize));
            //最后一个包的大小   
            int LastDataPacket = (int)(EzoneStream.Length - ((long)(PacketSize * PacketCount)));
            
            //指向远程服务端节点   
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ServerIP), PortNum);
            //创建套接字   
            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //连接到发送端   
            client.Connect(ipep);
            
            //获得客户端节点对象   
            IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
            //发送[文件名]到服务端   
            TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(EzoneFile.Name));
            //发送[文件目录]
            TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(EzoneFile.FullName));
            //发送[改变目录] --项目所用数据
            TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(EzoneFile.FullName.Replace(FilePath, "")));
            //发送[包的大小]到服务端   
            TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(PacketSize.ToString()));
            //发送[包的总数量]到服务端   
            TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(PacketCount.ToString()));
            //发送[最后一个包的大小]到服务端  
            TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(LastDataPacket.ToString()));

            //数据包   
            byte[] data = new byte[PacketSize];
            //开始循环发送数据包   
            for (int i = 0; i < PacketCount; i++)
            {
                //从文件流读取数据并填充数据包   
                EzoneStream.Read(data, 0, data.Length);
                //发送数据包   
                TransferFiles.SendVarData(client, data);
            }

            //如果还有多余的数据包,则应该发送完毕!   
            if (LastDataPacket != 0)
            {
                data = new byte[LastDataPacket];
                EzoneStream.Read(data, 0, data.Length);
                TransferFiles.SendVarData(client, data);
            }

            //关闭套接字   
            client.Close();
            EzoneStream.Close();
        }
--------------------编程问答-------------------- 我想, 接受的server端 应该跟这里无关吧.
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,