예제 #1
0
        public void StartMonitor()
        {
            foreach (string efi in this.mDoc.SearchFolders)
            {
                if (!Directory.Exists(efi)) //Does not exist
                {
                    continue;
                }

                if ((File.GetAttributes(efi) & FileAttributes.Directory) != (FileAttributes.Directory))  // not a folder
                {
                    continue;
                }


                FileSystemWatcher watcher = new FileSystemWatcher(efi);
                watcher.Changed += new FileSystemEventHandler(watcher_Changed);
                watcher.Created += new FileSystemEventHandler(watcher_Changed);
                watcher.Renamed += new RenamedEventHandler(watcher_Changed);
                //watcher.Deleted += new FileSystemEventHandler(watcher_Changed);
                //watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.CreationTime;
                watcher.IncludeSubdirectories = true;
                watcher.EnableRaisingEvents   = true;
                Watchers.Add(watcher);
                logger.Trace("Starting logger for {0}", efi);
            }
        }
예제 #2
0
        public void File_Encrypt()
        {
            Console.WriteLine("File.Encrypt()");

            // Create file and append text.
            var tempFile = Path.GetTempFileName();

            // Append text as UTF-8, default.
            File.AppendAllText(tempFile, UnitTestConstants.TextHelloWorld);

            var utf8             = NativeMethods.DefaultFileEncoding.BodyName.ToUpperInvariant();
            var readText8        = File.ReadAllText(tempFile);
            var actual           = File.GetAttributes(tempFile);
            var encryptionStatus = File.GetEncryptionStatus(tempFile);

            Console.WriteLine("\n\tCreated {0} file: [{1}]", utf8, tempFile);
            Console.WriteLine("\tContent: [{0}]", readText8);
            Console.WriteLine("\n\tFile.GetAttributes(): [{0}]", actual);
            Console.WriteLine("\tEncryption status   : [{0}]", encryptionStatus);

            var encryptOk = false;

            try
            {
                File.Encrypt(tempFile);
                encryptOk = true;
                actual    = File.GetAttributes(tempFile);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n\tCaught (unexpected) {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            encryptionStatus = File.GetEncryptionStatus(tempFile);
            Console.WriteLine("\n\tFile.Encrypt() (Should be True): [{0}]", encryptOk);
            Console.WriteLine("\tFile.GetAttributes()           : [{0}]", actual);
            Console.WriteLine("\tEncryption status              : [{0}]", encryptionStatus);

            var decryptOk = false;

            try
            {
                File.Decrypt(tempFile);
                decryptOk = true;
                actual    = File.GetAttributes(tempFile);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n\tCaught (unexpected) {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            var decryptionStatus = File.GetEncryptionStatus(tempFile);

            Console.WriteLine("\n\tFile.Decrypt() (Should be True): [{0}]:", decryptOk);
            Console.WriteLine("\tFile.GetAttributes()           : [{0}]", actual);
            Console.WriteLine("\tDecryption status              : [{0}]", decryptionStatus);

            Assert.IsTrue(encryptOk, "File should be encrypted.");
            Assert.IsTrue(encryptionStatus == FileEncryptionStatus.Encrypted, "File should be encrypted.");
            Assert.IsTrue(decryptOk, "File should be decrypted.");
            Assert.IsTrue(decryptionStatus == FileEncryptionStatus.Encryptable, "File should be decrypted.");

            File.Delete(tempFile, true);
            Assert.IsFalse(File.Exists(tempFile), "Cleanup failed: File should have been removed.");
        }
예제 #3
0
        private void DumpGetSetAttributes(bool isLocal)
        {
            Console.WriteLine("\n=== TEST {0} ===", isLocal ? UnitTestConstants.Local : UnitTestConstants.Network);
            var tmp      = Path.Combine(Path.GetTempPath(), "File.SetAttributes()-" + Path.GetRandomFileName());
            var tempPath = isLocal ? tmp : Path.LocalToUnc(tmp);
            var sys32    = isLocal ? UnitTestConstants.SysRoot32 : Path.LocalToUnc(UnitTestConstants.SysRoot32);

            Console.WriteLine("\nInput Path: [{0}]", sys32);

            // Just enumerate and compare attributes in folder: C:\Windows\System32
            foreach (var file in Directory.EnumerateFiles(sys32))
            {
                var actual   = File.GetAttributes(file);
                var expected = System.IO.File.GetAttributes(file);

                Assert.AreEqual(expected, actual, "AlphaFS != System.IO");
            }


            Console.WriteLine("\nInput Path: [{0}]", tempPath);

            // Create some folders and files.
            UnitTestConstants.CreateDirectoriesAndFiles(tempPath, 10, true);

            var apply = FileAttributes.Hidden | FileAttributes.Archive | FileAttributes.System | FileAttributes.ReadOnly;

            Console.WriteLine("\nSetAttributes(): [{0}]", apply);

            var allOk = true;
            var cnt   = 0;

            UnitTestConstants.StopWatcher(true);
            foreach (var file in Directory.EnumerateFiles(tempPath))
            {
                try
                {
                    File.SetAttributes(file, apply);

                    var actual   = File.GetAttributes(file);
                    var expected = System.IO.File.GetAttributes(file);

                    Console.WriteLine("\n\t#{0:000}\tFile     : [{1}]\n\t\tAlphaFS  : [{2}]\n\t\tSystem.IO: [{3}]", ++cnt, file, expected, actual);

                    if (cnt == 0)
                    {
                        Assert.Inconclusive("Nothing was enumerated, but it was expected.");
                    }

                    Assert.AreEqual(expected, actual, "AlphaFS != System.IO");
                }
                catch (Exception ex)
                {
                    allOk = false;
                    Console.WriteLine("\n\tCaught (unexpected) {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
                }
            }
            Console.WriteLine();
            Console.WriteLine(UnitTestConstants.Reporter());
            Assert.IsTrue(allOk);


            apply = FileAttributes.Normal;
            Console.WriteLine("\nSetAttributes(): [{0}]", apply);

            allOk = true;
            cnt   = 0;
            UnitTestConstants.StopWatcher(true);
            foreach (var file in Directory.EnumerateFiles(tempPath))
            {
                try
                {
                    File.SetAttributes(file, apply);

                    var actual   = File.GetAttributes(file);
                    var expected = System.IO.File.GetAttributes(file);

                    Console.WriteLine("\n\t#{0:000}\tFile     : [{1}]\n\t\tAlphaFS  : [{2}]\n\t\tSystem.IO: [{3}]", ++cnt, file, expected, actual);

                    if (cnt == 0)
                    {
                        Assert.Inconclusive("Nothing was enumerated, but it was expected.");
                    }

                    Assert.AreEqual(expected, actual, "AlphaFS != System.IO");
                }
                catch (Exception ex)
                {
                    allOk = false;
                    Console.WriteLine("\n\tCaught (unexpected) {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
                }
            }
            Console.WriteLine();
            Console.WriteLine(UnitTestConstants.Reporter());


            Directory.Delete(tempPath, true);
            Assert.IsFalse(Directory.Exists(tempPath), "Cleanup failed: Directory should have been removed.");
            Assert.IsTrue(allOk);
            Console.WriteLine();
        }
예제 #4
0
        private void DumpEnableDisableEncryption(bool isLocal)
        {
            Console.WriteLine("\n=== TEST {0} ===", isLocal ? UnitTestConstants.Local : UnitTestConstants.Network);
            var tempPath = Path.Combine(Path.GetTempPath(), "Directory.EnableDisableEncryption()-" + Path.GetRandomFileName());

            if (!isLocal)
            {
                tempPath = Path.LocalToUnc(tempPath);
            }

            Console.WriteLine("\nInput Directory Path: [{0}]", tempPath);

            const string disabled    = "Disable=0";
            const string enabled     = "Disable=1";
            var          lineDisable = string.Empty;
            var          deskTopIni  = Path.Combine(tempPath, "Desktop.ini");

            Directory.CreateDirectory(tempPath);
            var actual = File.GetAttributes(tempPath);


            var report = string.Empty;
            var action = false;

            try
            {
                UnitTestConstants.StopWatcher(true);
                Directory.EnableEncryption(tempPath);
                report = UnitTestConstants.Reporter(true);
                action = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n\tCaught (UNEXPECTED) {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            Assert.IsTrue(action, "Encryption should be True");


            // Read filestream contents, get the last line.
            using (var streamRead = File.OpenText(deskTopIni))
            {
                string line;
                while ((line = streamRead.ReadLine()) != null)
                {
                    lineDisable = line;
                }
            }
            action = lineDisable.Equals(disabled);
            Console.WriteLine("\nEnableEncryption() (Should be True): [{0}]", action);
            Console.WriteLine("File Desktop.ini contents: [{0}]\t{1}", lineDisable, report);
            Assert.IsTrue(action, "Encryption should be True");
            Assert.IsTrue(File.Exists(deskTopIni), "Desktop.ini should Exist");


            action = false;
            try
            {
                UnitTestConstants.StopWatcher(true);
                Directory.DisableEncryption(tempPath);
                report = UnitTestConstants.Reporter(true);
                action = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n\tCaught (UNEXPECTED) {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            Assert.IsTrue(action, "Encryption should be True");


            // Read filestream contents, get the last line.
            using (var streamRead = File.OpenText(deskTopIni))
            {
                string line;
                while ((line = streamRead.ReadLine()) != null)
                {
                    lineDisable = line;
                }
            }
            action = lineDisable.Equals(enabled);
            Console.WriteLine("\nDisableEncryption() (Should be True): [{0}]", action);
            Console.WriteLine("File Desktop.ini contents: [{0}]\t{1}", lineDisable, report);
            Assert.IsTrue(action, "Encryption should be True");
            Assert.IsTrue(File.Exists(deskTopIni), "Desktop.ini should Exist");


            Directory.Delete(tempPath, true);
            Assert.IsFalse(Directory.Exists(tempPath), "Cleanup failed: Directory should have been removed.");
            Console.WriteLine();
        }