コード例 #1
0
        /// <summary>
        /// 用于启动查找文件的过程
        /// </summary>
        private void searchFiles(String Location, long fileLength)
        {
            DirectoryInfo   dir       = null;
            List <FileInfo> files     = null;
            FoundFile       foundFile = null;

            try
            {
                dir = new DirectoryInfo(Location);
                Dispatcher.Invoke(showInfo, Location);
                if (cts.IsCancellationRequested)
                {
                    //Dispatcher.Invoke(EnableSearchButton, true);
                    return;
                }
                //启动并行查找
                var query = (from file in dir.GetFiles("*.*", SearchOption.TopDirectoryOnly).AsParallel()
                             where file.Length >= fileLength
                             select file).WithCancellation(cts.Token);

                files = query.ToList();


                //更新显示
                if (files != null)
                {
                    foreach (var item in files)
                    {
                        //如果收到取消请求
                        if (cts.IsCancellationRequested)
                        {
                            //Dispatcher.Invoke(showInfo, "搜索已取消");
                            //Dispatcher.Invoke(EnableSearchButton, true);
                            return;
                        }
                        foundFile = new FoundFile()
                        {
                            Name     = item.Name,
                            Length   = item.Length,
                            Location = item.DirectoryName
                        };
                        Action <FoundFile> addFileDelegate = (file) =>
                        {
                            FoundFiles.Add(file);
                        };

                        Dispatcher.BeginInvoke(addFileDelegate, foundFile);
                    }
                }
                //递归查找每个子文件夹
                foreach (var directory in dir.GetDirectories())
                {
                    searchFiles(directory.FullName, fileLength);
                }
            }
            catch (UnauthorizedAccessException ex)
            {
                Dispatcher.Invoke(showInfo, dir.Name + "无权限访问");
            }
        }
コード例 #2
0
        /// <summary>
        /// 用于启动查找文件的过程
        /// </summary>
        private void searchFiles(String Location, long fileLength)
        {
            DirectoryInfo dir = null;
            List<FileInfo> files = null;
            FoundFile foundFile = null;
            try
            {
                dir = new DirectoryInfo(Location);
                Dispatcher.Invoke(showInfo, Location);
                if (cts.IsCancellationRequested)
                {
                    //Dispatcher.Invoke(EnableSearchButton, true);
                    return;
                }
                //启动并行查找
                var query = (from file in dir.GetFiles("*.*", SearchOption.TopDirectoryOnly).AsParallel()
                             where file.Length >= fileLength
                             select file).WithCancellation(cts.Token);

                files = query.ToList();

                //更新显示
                if (files != null)
                {
                    foreach (var item in files)
                    {
                        //如果收到取消请求
                        if (cts.IsCancellationRequested)
                        {
                            //Dispatcher.Invoke(showInfo, "搜索已取消");
                            //Dispatcher.Invoke(EnableSearchButton, true);
                            return;
                        }
                        foundFile = new FoundFile()
                        {
                            Name = item.Name,
                            Length = item.Length,
                            Location = item.DirectoryName
                        };
                        Action<FoundFile> addFileDelegate = (file) =>
                        {
                            FoundFiles.Add(file);
                        };

                        Dispatcher.BeginInvoke(addFileDelegate, foundFile);
                    }
                }
                //递归查找每个子文件夹
                foreach (var directory in dir.GetDirectories())
                {
                    searchFiles(directory.FullName, fileLength);
                }
            }
            catch (UnauthorizedAccessException ex)
            {
                Dispatcher.Invoke(showInfo, dir.Name + "无权限访问");

            }
        }