コード例 #1
0
ファイル: FileStatistic.cs プロジェクト: ckkwing/MediaCenter
        private StatisticItem GetFolderInfo(DirectoryInfo dirInfo)
        {
            StatisticItem result     = null;
            string        folderPath = string.Empty;

            try
            {
                if (dirInfo.IsNull() ||
                    //Hard driver will be ignored by Attributes
                    (dirInfo.Root.FullName != dirInfo.FullName &&
                     (LocalFolder.GetIsExcludeFolder(dirInfo.FullName) || LocalFolder.GetIsExcludeAttribute(dirInfo.Attributes))) ||
                    isCanceled)
                {
                    return(result);
                }
                folderPath = dirInfo.FullName;
            }
            catch (Exception ex)
            {
                LogHelper.DebugFormat("GetFiles exception:{0},path={1}", ex.Message, dirInfo.Name);
                return(result);
            }

            StatisticItem folderItem = null;

            lock (this.lockDicObj)
            {
                folderItem = GetCacheItem(folderPath, cacheDic, true);
            }

            if (!folderItem.IsNull() && folderItem.IsCompleted)
            {
                return(folderItem);
            }

            folderItem      = new StatisticItem();
            folderItem.Path = folderPath;
            AddToCache(folderItem);

            bool isFolderCanceled = GetFolderIsCanceled(folderPath);

            FileInfo[] files = null;
            try
            {
                files = dirInfo.GetFiles();
                if (!files.IsNullOrEmpty() &&
                    (folderItem.IsNull() || (!folderItem.IsNull() && !folderItem.IsCompleted)))
                {
                    foreach (var fi in files)
                    {
                        if (isCanceled || isFolderCanceled)
                        {
                            return(result);
                        }

                        if (fi.IsNull() || CheckIsFileIgnored(fi))
                        {
                            continue;
                        }

                        fileScannCount++;
                        folderItem.ItemCount++;
                        folderItem.Size += fi.Length;
                        if (ReportProgressForFolder(folderPath))
                        {
                            isFolderCanceled = GetFolderIsCanceled(folderPath);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.DebugFormat("GetFiles exception:{0}", ex.Message);
            }

            DirectoryInfo[] folders = null;
            try
            {
                folders = dirInfo.GetDirectories();
                if (!folders.IsNullOrEmpty())
                {
                    foreach (var f in folders)
                    {
                        try
                        {
                            ///Folder too long path
                            isFolderCanceled = GetFolderIsCanceled(f.FullName);
                            ///Check is cancel for every folder
                            if (isCanceled)
                            {
                                return(folderItem);
                            }
                            if (isFolderCanceled)
                            {
                                continue;
                            }
                            StatisticItem subItem = GetFolderInfo(f);
                        }
                        catch (Exception ex)
                        {
                            LogHelper.DebugFormat("GetDirectories exception:{0}", ex.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.DebugFormat("GetDirectories exception:{0}", ex.Message);
            }

            if (!folderItem.IsNull())
            {
                isFolderCanceled = GetAllFoldersIsCanceled(folderPath);
                if (!isFolderCanceled)
                {
                    folderItem.IsCompleted = true;
                }
                result = folderItem;
            }
            return(folderItem);
        }
コード例 #2
0
ファイル: FileStatistic.cs プロジェクト: ckkwing/MediaCenter
        public void Remove(bool isFolder, params string[] pathList)
        {
            if (pathList.IsNullOrEmpty())
            {
                return;
            }

            var lowerPaths = pathList.Where(item => !item.IsNullOrEmpty()).Select(item => item);

            foreach (var path in lowerPaths)
            {
                lock (lockListObj)
                {
                    var matchPaths = GetMatchPaths(path, pendingList, isFolder);
                    foreach (var item in matchPaths)
                    {
                        pendingList.Remove(item);
                    }

                    allScanPathDic[path] = false;
                    var keys = GetMatchPaths(path, allScanPathDic.Keys, isFolder);
                    foreach (var item in keys)
                    {
                        allScanPathDic[item] = false;
                        //LogHelper.DebugFormat("/----- FileStatistic.Remove() canceled path :{0}", item);
                    }
                }

                lock (lockDicObj)
                {
                    if (isFolder)
                    {
                        var keys = GetMatchPaths(path, currentScannedDic.Keys, isFolder);
                        foreach (var key in keys)
                        {
                            currentScannedDic.Remove(key);
                        }
                    }
                    else
                    {
                        string        folderPath = Path.GetDirectoryName(path);
                        StatisticItem folderItem = GetCacheItem(folderPath, cacheDic, true);
                        if (!folderItem.IsNull())
                        {
                            try
                            {
                                FileInfo fi = new FileInfo(path);
                                folderItem.ItemCount--;
                                folderItem.Size -= fi.Length;
                            }
                            catch (Exception ex)
                            {
                                LogHelper.DebugFormat("FileInfo exception:{0}", ex.Message);
                            }
                        }
                    }
                }
            }

            bool isEmpty = false;

            lock (lockListObj)
            {
                isEmpty = pendingList.IsNullOrEmpty();
            }
            RaiseScanProgressChanged(isEmpty);
        }