Exemplo n.º 1
0
        private void OnCreated(object sender, FileSystemEventArgs e)
        {
            Trace.TraceInformation("{0} {1}", e.ChangeType, e.FullPath);

            if (Directory.Exists(e.FullPath))
            {
                Raise(new DirectoryCreatedEvent(Path.GetFileName(e.FullPath), new FileSystemEntryId(e.FullPath),
                                                new FileSystemEntryId(Path.GetDirectoryName(e.FullPath))));
            }
            else if (File.Exists(e.FullPath))
            {
                try
                {
                    var size = _fileSizeEstimator.Estimate(e.FullPath);
                    Raise(new FileCreatedEvent(Path.GetFileName(e.FullPath), new FileSystemEntryId(e.FullPath),
                                               new FileSystemEntryId(Path.GetDirectoryName(e.FullPath)), size));
                }
                catch (DirectoryNotFoundException)
                {
                    // do nothing
                }
            }
Exemplo n.º 2
0
        public IEnumerable <FileSystemEntryInfo> EnumerateFileSystemEntries(string path)
        {
            var queue = new Queue <DirectoryInfo>();
            var root  = new DirectoryInfo(path);

            queue.Enqueue(root);

            while (queue.Count > 0)
            {
                var current = queue.Dequeue();
                foreach (var fileSystemInfo in current.EnumerateFileSystemInfos())
                {
                    long   size        = 0;
                    var    entryExists = true;
                    string parent      = null;

                    switch (fileSystemInfo)
                    {
                    case FileInfo fileInfo:
                        try
                        {
                            size   = _fileSizeEstimator.Estimate(fileInfo.FullName);
                            parent = fileInfo.Directory?.FullName;
                        }
                        catch (DirectoryNotFoundException)
                        {
                            entryExists = false;
                        }
                        catch (FileNotFoundException)
                        {
                            entryExists = false;
                        }

                        if (entryExists)
                        {
                            yield return(new FileEntryInfo(fileInfo.Name,
                                                           new FileSystemEntryId(fileInfo.FullName),
                                                           new FileSystemEntryId(parent),
                                                           size));
                        }

                        break;

                    case DirectoryInfo directoryInfo:
                        try
                        {
                            parent = directoryInfo.Parent?.FullName;
                        }
                        catch (DirectoryNotFoundException)
                        {
                            entryExists = false;
                        }

                        if (entryExists)
                        {
                            yield return(new DirectoryEntryInfo(directoryInfo.Name,
                                                                new FileSystemEntryId(directoryInfo.FullName),
                                                                new FileSystemEntryId(parent)));
                        }

                        queue.Enqueue(directoryInfo);
                        break;
                    }
                }
            }
        }