Exemplo n.º 1
0
        public static string CompareFileSystemSnapshots(FileSystemSnapshot fsSnapshot1, FileSystemSnapshot fsSnapshot2)
        {
            // Determine new files
            Parallel.ForEach(fsSnapshot2.AllFSEntries, data =>
            {
                // Determine if any key file system entries have been added
                // Logic: Entry is present in Snapshot1 and not present in Snapshot2
                if (!fsSnapshot1.AllFSEntries.ContainsKey(data.Key))
                {
                    Console.WriteLine("New entry found: {0}", data.Key);
                }
            });

            // Determine removed files
            Parallel.ForEach(fsSnapshot1.AllFSEntries, data =>
            {
                // Determine if any key file system entries have been removed
                // Logic: Entry is present in Snapshot2 and not present in Snapshot1
                if (!fsSnapshot2.AllFSEntries.ContainsKey(data.Key))
                {
                    Console.WriteLine("Removed entry found: {0}", data.Key);
                }
            });

            // Determine modified files (size, modified date, access date)
            Parallel.ForEach(fsSnapshot2.AllFSEntries, data =>
            {
                // Determine if any key file system entries have been removed
                // Logic: Entry is present in Snapshot2 and not present in Snapshot1
                if (fsSnapshot1.AllFSEntries.ContainsKey(data.Key))
                {
                    FileInformation fi1 = fsSnapshot1.AllFSEntries[data.Key];
                    FileInformation fi2 = data.Value;
                    if (fi1.FileSize != fi2.FileSize)
                    {
                        Console.WriteLine("File size changed: {0}", fi2.FullPath);
                    }
                    if (fi1.LastAccessTime != fi2.LastAccessTime)
                    {
                        Console.WriteLine("Last access time changed: {0}", fi2.FullPath);
                    }
                    if (fi1.LastWriteTime != fi2.LastWriteTime)
                    {
                        Console.WriteLine("Last write time changed: {0}", fi2.FullPath);
                    }
                    if (fi1.FileAttribute != fi2.FileAttribute)
                    {
                        Console.WriteLine("File Attribute changed: {0}", fi2.FullPath);
                    }
                }
            });
            return("");
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine(".____    .__             ________  .__  _____  _____ ");
            Console.WriteLine("|    |   |__|__  __ ____ \\______ \\ |__|/ ____\\/ ____\\");
            Console.WriteLine("|    |   |  \\  \\/ // __ \\ |    |  \\|  \\   __\\   __\\ ");
            Console.WriteLine("|    |___|  |\\   /\\  ___/ |    `   \\  ||  |   |  |   ");
            Console.WriteLine("|_______ \\__| \\_/  \\___  >_______  /__||__|   |__|   ");
            Console.WriteLine("        \\/             \\/        \\/                  ");
            Console.WriteLine("                                  By Thomas Laurenson");
            Console.WriteLine("                                  thomaslaurenson.com\n");

            string path = null;

            // Parse command line arguments
            Parser.Default.ParseArguments <Options>(args)
            .WithParsed <Options>(options =>
            {
                path = options.TargetDirectory;
            })
            .WithNotParsed <Options>(errors =>
            {
                Environment.Exit(1);
            });

            Console.WriteLine(">>> Processing: {0}", path.ToString());

            // SNAPSHOT ONE:

            Console.WriteLine(">>> Press any key to take Snapshot1...");
            Console.ReadKey();

            Stopwatch watch = new Stopwatch();

            watch.Start();

            // Enumerate the file system
            FileSystemSnapshot fsSnapshot1 = new FileSystemSnapshot(path);

            while (!fsSnapshot1.GetFileSystemSnapshot(path))
            {
                Thread.Sleep(1000);
            }

            watch.Stop();
            Console.WriteLine("  > Time elapsed: {0}", watch.Elapsed);

            Console.WriteLine("  > Dir count: {0}", fsSnapshot1.AllDirs.Count);
            Console.WriteLine("  > Fis count: {0}", fsSnapshot1.AllFiles.Count);

            //foreach (KeyValuePair<string, FileSystemEnumerator.FileInformation> kvp in FileSystemEnumerator.filesD)
            //{
            //    Console.WriteLine(kvp.Key);
            //    Console.WriteLine(kvp.Value.FileSize);
            //    Console.WriteLine();
            //}

            // SNAPSHOT TWO:

            Console.WriteLine(">>> Press any key to take Snapshot2...");
            Console.ReadKey();

            watch.Start();

            // Enumerate the file system
            FileSystemSnapshot fsSnapshot2 = new FileSystemSnapshot(path);

            while (!fsSnapshot2.GetFileSystemSnapshot(path))
            {
                Thread.Sleep(1000);
            }

            watch.Stop();
            Console.WriteLine("  > Time elapsed: {0}", watch.Elapsed);

            Console.WriteLine("  > Dir count: {0}", fsSnapshot2.AllDirs.Count);
            Console.WriteLine("  > Fis count: {0}", fsSnapshot2.AllFiles.Count);

            // Compared the two file system snapshots
            FileSystemSnapshot.CompareFileSystemSnapshots(fsSnapshot1, fsSnapshot2);
        }