Example #1
0
        public void TestDisplayPath()
        {
            var sfi = new System.IO.FileInfo(@"c:\Windows\notepad.exe");
            var fi  = new FileInfo(@"c:\Windows\notepad.exe");

            Assert.AreEqual(sfi.ToString(), fi.DisplayPath);
        }
Example #2
0
        public void TestGetAccessControlSections()
        {
            var filename = Util.CreateNewFile(uncDirectory);

            try {
                var fi       = new FileInfo(filename);
                var security = fi.GetAccessControl(AccessControlSections.Access);
                Assert.IsNotNull(security);
                Assert.AreEqual(typeof(FileSystemRights), security.AccessRightType);
                Assert.AreEqual(typeof(FileSystemAccessRule), security.AccessRuleType);
                Assert.AreEqual(typeof(FileSystemAuditRule), security.AuditRuleType);
                Assert.IsTrue(security.AreAccessRulesCanonical);
                Assert.IsTrue(security.AreAuditRulesCanonical);
                Assert.IsFalse(security.AreAccessRulesProtected);
                Assert.IsFalse(security.AreAuditRulesProtected);
                var securityGetAccessRules = security.GetAuditRules(true, true, typeof(NTAccount)).Cast <FileSystemAccessRule>();
                Assert.AreEqual(0, securityGetAccessRules.Count());
                var perm      = security.GetAccessRules(true, true, typeof(NTAccount));
                var ntAccount = new NTAccount(WindowsIdentity.GetCurrent().Name);
                var rule      = perm.Cast <FileSystemAccessRule>().SingleOrDefault(e => ntAccount == e.IdentityReference);
                Assert.IsNotNull(rule);
                Assert.IsTrue((rule.FileSystemRights & FileSystemRights.FullControl) != 0);
            }
            finally {
                Pri.LongPath.File.Delete(filename);
            }
        }
Example #3
0
        public void TestCreateTextAndWrite()
        {
            Assert.IsTrue(uncDirectory.Exists());
            String tempLongPathFilename;

            do
            {
                tempLongPathFilename = uncDirectory.Combine(Path.GetRandomFileName());
            } while (Pri.LongPath.File.Exists(tempLongPathFilename));

            Assert.IsFalse(Pri.LongPath.File.Exists(tempLongPathFilename));

            const String fileText = "test";

            using (var writer = Pri.LongPath.File.CreateText(tempLongPathFilename)) {
                writer.WriteLine(fileText);
            }

            try {
                Assert.IsTrue(Pri.LongPath.File.Exists(tempLongPathFilename));
                var fileInfo = new FileInfo(tempLongPathFilename);
                Assert.AreEqual(fileText.Length + Environment.NewLine.Length, fileInfo.Length);
            }
            finally {
                Pri.LongPath.File.Delete(tempLongPathFilename);
            }
        }
Example #4
0
        public void TestDecrypt()
        {
            var tempLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("filename.ext").ToString();

            try {
                using (var s = Pri.LongPath.File.Create(tempLongPathFilename, 200)) { }

                var preAttrib = Pri.LongPath.File.GetAttributes(tempLongPathFilename);
                Assert.AreEqual(( FileAttributes )0, preAttrib & FileAttributes.Encrypted);

                var fi = new FileInfo(tempLongPathFilename);
                fi.Encrypt();

                var postAttrib = Pri.LongPath.File.GetAttributes(tempLongPathFilename);
                Assert.AreEqual(FileAttributes.Encrypted, postAttrib & FileAttributes.Encrypted);

                fi.Decrypt();

                postAttrib = Pri.LongPath.File.GetAttributes(tempLongPathFilename);
                Assert.AreEqual(( FileAttributes )0, postAttrib & FileAttributes.Encrypted);
            }
            finally {
                Pri.LongPath.File.Delete(tempLongPathFilename);
            }
        }
Example #5
0
        public void FileInfoReturnsCorrectDirectoryForLongPathFile()
        {
            Assert.IsTrue(uncDirectory.Exists());
            String tempLongPathFilename;

            do
            {
                tempLongPathFilename = uncDirectory.Combine(Path.GetRandomFileName());
            } while (File.Exists(tempLongPathFilename));

            Assert.IsFalse(File.Exists(tempLongPathFilename));

            using (var writer = File.CreateText(tempLongPathFilename)) {
                writer.WriteLine("test");
            }

            try {
                Assert.IsTrue(File.Exists(tempLongPathFilename));
                var fileInfo = new FileInfo(tempLongPathFilename);
                Assert.AreEqual(uncDirectory, fileInfo.Directory.FullName);
                Assert.AreEqual(uncDirectory.GetFileName(), fileInfo.Directory.Name);
            }
            finally {
                File.Delete(tempLongPathFilename);
            }
        }
Example #6
0
        public void TestMoveTo()
        {
            var tempLongPathFilename     = new StringBuilder(uncDirectory).Append(@"\").Append("file21.ext").ToString();
            var tempDestLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("file21-1.ext").ToString();

            Assert.IsFalse(Pri.LongPath.File.Exists(tempLongPathFilename));
            Pri.LongPath.File.Copy(filePath, tempLongPathFilename);

            try {
                Assert.IsTrue(Pri.LongPath.File.Exists(tempLongPathFilename));

                var fi = new FileInfo(tempLongPathFilename);
                fi.MoveTo(tempDestLongPathFilename);

                try {
                    Assert.IsFalse(Pri.LongPath.File.Exists(tempLongPathFilename));
                    Assert.IsTrue(Pri.LongPath.File.Exists(tempDestLongPathFilename));
                }
                finally {
                    Pri.LongPath.File.Delete(tempDestLongPathFilename);
                }
            }
            finally {
                if (Pri.LongPath.File.Exists(tempLongPathFilename))
                {
                    Pri.LongPath.File.Delete(tempLongPathFilename);
                }
            }
        }
Example #7
0
        public void TestOpenOpen()
        {
            var fi = new FileInfo(filePath);

            using (var fileStream = fi.Open(FileMode.Open)) {
                Assert.IsNotNull(fileStream);
            }
        }
Example #8
0
        public void TestOpenReadReadsExistingData()
        {
            var fi = new FileInfo(filePath);

            using (var fileStream = fi.OpenRead()) {
                Assert.AreEqual('t', fileStream.ReadByte());
            }
        }
Example #9
0
        public void TestOpenTextReadsExistingData()
        {
            var fi = new FileInfo(filePath);

            using (var streamReader = fi.OpenText()) {
                Assert.AreEqual("test", streamReader.ReadLine());
            }
        }
Example #10
0
        public void TestSetLastAccessTimeMissingFile()
        {
            var filename = longPathDirectory.Combine("gibberish.ext");
            var dateTime = DateTime.Now.AddDays(1);
            var fi       = new FileInfo(filename);

            Assert.Throws <FileNotFoundException>(() => fi.LastAccessTime = dateTime);
        }
Example #11
0
        public void TestSetLastWriteTimeUtcMissingFile()
        {
            var filename = uncDirectory.Combine("gibberish.ext");
            var dateTime = DateTime.UtcNow.AddDays(1);
            var fi       = new FileInfo(filename);

            Assert.Throws <FileNotFoundException>(() => fi.LastWriteTimeUtc = dateTime);
        }
Example #12
0
        public void TestOpenCreateNew()
        {
            var fi = new FileInfo(filePath);

            Assert.Throws <IOException>(() => {
                using (var fileStream = fi.Open(FileMode.CreateNew)) {
                    Assert.IsNotNull(fileStream);
                }
            });
        }
Example #13
0
        public void TestLengthWithBadPath()
        {
            var      filename = Util.CreateNewFile(uncDirectory);
            FileInfo fi;

            try {
                Assert.Throws <FileNotFoundException>(() => fi = new FileInfo(filename));
            }
            catch {
                Pri.LongPath.File.Delete(filename);
            }
        }
Example #14
0
        public void TestGetIsReadOnly()
        {
            var filename = Util.CreateNewFile(uncDirectory);

            try {
                var fi = new FileInfo(filename);
                Assert.IsTrue(fi.Exists);
                Assert.IsFalse(fi.IsReadOnly);
            }
            finally {
                Pri.LongPath.File.Delete(filename);
            }
        }
Example #15
0
        public void TestGetLastAccessTime()
        {
            var filename = Util.CreateNewFile(longPathDirectory);

            try {
                var dateTime = File.GetLastAccessTime(filename);
                var fi       = new FileInfo(filename);
                Assert.AreEqual(fi.LastAccessTime, dateTime);
            }
            finally {
                File.Delete(filename);
            }
        }
Example #16
0
        public void TestSetAccessControl()
        {
            var filename = Util.CreateNewFile(uncDirectory);

            try {
                var fi       = new FileInfo(filename);
                var security = new FileSecurity();
                fi.SetAccessControl(security);
            }
            finally {
                Pri.LongPath.File.Delete(filename);
            }
        }
Example #17
0
        public void TestGetCreationTime()
        {
            var filename = Util.CreateNewFile(uncDirectory);

            try {
                var dateTime = filename.GetCreationTime();
                var fi       = new FileInfo(filename);
                Assert.AreEqual(fi.CreationTime, dateTime);
            }
            finally {
                File.Delete(filename);
            }
        }
Example #18
0
        public void TestGetLastWriteTimeUtc()
        {
            var filename = Util.CreateNewFile(uncDirectory);

            try {
                var dateTime = File.GetLastWriteTimeUtc(filename);
                var fi       = new FileInfo(filename);
                Assert.AreEqual(fi.LastWriteTimeUtc, dateTime);
            }
            finally {
                File.Delete(filename);
            }
        }
Example #19
0
        public void TestReplaceIgnoreMerge()
        {
            var tempLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("filename.ext").ToString();

            using (var fileStream = Pri.LongPath.File.Create(tempLongPathFilename)) {
                try {
                    fileStream.WriteByte(42);
                }
                catch (Exception) {
                    Pri.LongPath.File.Delete(tempLongPathFilename);

                    throw;
                }
            }

            var tempLongPathFilename2 = new StringBuilder(uncDirectory).Append(@"\").Append("filename2.ext").ToString();

            using (var fileStream = Pri.LongPath.File.Create(tempLongPathFilename2)) {
                try {
                    fileStream.WriteByte(52);
                }
                catch (Exception) {
                    Pri.LongPath.File.Delete(tempLongPathFilename2);

                    throw;
                }
            }

            var fi = new FileInfo(tempLongPathFilename);

            try {
                const Boolean ignoreMetadataErrors = true;
                var           fi2 = fi.Replace(tempLongPathFilename2, null, ignoreMetadataErrors);
                Assert.IsNotNull(fi2);
                Assert.AreEqual(tempLongPathFilename2, fi2.FullName);

                using (var fileStream = Pri.LongPath.File.OpenRead(tempLongPathFilename2)) {
                    Assert.AreEqual(42, fileStream.ReadByte());
                }

                Assert.IsFalse(Pri.LongPath.File.Exists(tempLongPathFilename));
            }
            finally {
                if (Pri.LongPath.File.Exists(tempLongPathFilename))
                {
                    Pri.LongPath.File.Delete(tempLongPathFilename);
                }

                Pri.LongPath.File.Delete(tempLongPathFilename2);
            }
        }
Example #20
0
        public void TestSetCreationTime()
        {
            var filename = Util.CreateNewFile(uncDirectory);

            try {
                var dateTime = DateTime.Now.AddDays(1);
                filename.SetCreationTime(dateTime);
                var fi = new FileInfo(filename);
                Assert.AreEqual(fi.CreationTime, dateTime);
            }
            finally {
                File.Delete(filename);
            }
        }
Example #21
0
        public void TestReplace()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString();

            using (var fileStream = File.Create(tempLongPathFilename)) {
                try {
                    fileStream.WriteByte(42);
                }
                catch (Exception) {
                    File.Delete(tempLongPathFilename);

                    throw;
                }
            }

            var tempLongPathFilename2 = new StringBuilder(longPathDirectory).Append(@"\").Append("filename2.ext").ToString();

            using (var fileStream = File.Create(tempLongPathFilename2)) {
                try {
                    fileStream.WriteByte(52);
                }
                catch (Exception) {
                    File.Delete(tempLongPathFilename2);

                    throw;
                }
            }

            var fi = new FileInfo(tempLongPathFilename);

            try {
                var fi2 = fi.Replace(tempLongPathFilename2, null);
                Assert.IsNotNull(fi2);
                Assert.AreEqual(tempLongPathFilename2, fi2.FullName);

                using (var fileStream = File.OpenRead(tempLongPathFilename2)) {
                    Assert.AreEqual(42, fileStream.ReadByte());
                }

                Assert.IsFalse(File.Exists(tempLongPathFilename));
            }
            finally {
                if (File.Exists(tempLongPathFilename))
                {
                    File.Delete(tempLongPathFilename);
                }

                File.Delete(tempLongPathFilename2);
            }
        }
Example #22
0
        public void TestSetLastWriteTimeUtc()
        {
            var filename = Util.CreateNewFile(uncDirectory);

            try {
                var dateTime = DateTime.UtcNow.AddDays(1);
                File.SetLastWriteTimeUtc(filename, dateTime);
                var fi = new FileInfo(filename);
                Assert.AreEqual(fi.LastWriteTimeUtc, dateTime);
            }
            finally {
                File.Delete(filename);
            }
        }
Example #23
0
        public void TestSetIsReadOnly()
        {
            var filename = Util.CreateNewFile(uncDirectory);
            var fi       = new FileInfo(filename);

            try {
                fi.IsReadOnly = true;
                Assert.IsTrue(fi.IsReadOnly);
            }
            finally {
                fi.IsReadOnly = false;
                fi.Delete();
            }
        }
Example #24
0
        public void TestCopyToWithoutOverwriteAndExistingFile()
        {
            var fi = new FileInfo(filePath);
            var destLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("filename (Copy).ext").ToString();

            fi.CopyTo(destLongPathFilename);

            try {
                Assert.IsTrue(Pri.LongPath.File.Exists(destLongPathFilename));
                Assert.Throws <IOException>(() => fi.CopyTo(destLongPathFilename));
            }
            finally {
                Pri.LongPath.File.Delete(destLongPathFilename);
            }
        }
Example #25
0
        public void TestOpenReadWithWrite()
        {
            var tempLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("file31.ext").ToString();
            var fi = new FileInfo(tempLongPathFilename);

            try {
                Assert.Throws <NotSupportedException>(() => {
                    using (var fileStream = fi.Open(FileMode.Append, FileAccess.Read)) {
                        fileStream.WriteByte(43);
                    }
                });
            }
            finally {
                Pri.LongPath.File.Delete(tempLongPathFilename);
            }
        }
Example #26
0
        public void TestCopyToWithOverwrite()
        {
            var fi = new FileInfo(filePath);
            var destLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("filename (Copy).ext").ToString();

            fi.CopyTo(destLongPathFilename);

            try {
                Assert.IsTrue(Pri.LongPath.File.Exists(destLongPathFilename));
                fi.CopyTo(destLongPathFilename, true);
                Assert.AreEqual(Pri.LongPath.File.ReadAllText(filePath), Pri.LongPath.File.ReadAllText(destLongPathFilename));
            }
            finally {
                Pri.LongPath.File.Delete(destLongPathFilename);
            }
        }
Example #27
0
        public void TestCopyToWithoutOverwrite()
        {
            var fi = new FileInfo(longPathFilename);
            var destLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename (Copy).ext").ToString();

            fi.CopyTo(destLongPathFilename);

            try {
                Assert.IsTrue(File.Exists(destLongPathFilename));

                Assert.AreEqual(File.ReadAllText(longPathFilename), File.ReadAllText(destLongPathFilename));
            }
            finally {
                File.Delete(destLongPathFilename);
            }
        }
Example #28
0
        public void TestReadAllTextWithEncoding()
        {
            var tempLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("file26.ext").ToString();
            var fi = new FileInfo(tempLongPathFilename);

            try {
                using (var streamWriter = File.CreateText(tempLongPathFilename, Encoding.Unicode)) {
                    streamWriter.WriteLine("file26");
                }

                Assert.AreEqual("file26" + Environment.NewLine, File.ReadAllText(fi.FullName, Encoding.Unicode));
            }
            finally {
                File.Delete(tempLongPathFilename);
            }
        }
Example #29
0
        public void TestCreateText()
        {
            var tempLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("file20.ext").ToString();
            var fi = new FileInfo(tempLongPathFilename);

            Assert.IsFalse(fi.Exists);

            using (fi.CreateText()) { }

            try {
                Assert.IsTrue(Pri.LongPath.File.Exists(fi.FullName));                     // don't use FileInfo.Exists, it caches existance
            }
            finally {
                fi.Delete();
            }
        }
Example #30
0
        public void TestSetLastWriteTimeUtc()
        {
            var filename = Util.CreateNewFile(uncDirectory);

            try {
                var dateTime = DateTime.UtcNow.AddDays(1);

                var fi = new FileInfo(filename)
                {
                    LastWriteTimeUtc = dateTime
                };

                Assert.AreEqual(dateTime, Pri.LongPath.File.GetLastWriteTimeUtc(filename));
            }
            finally {
                Pri.LongPath.File.Delete(filename);
            }
        }
        /// <summary>
        /// Checks if the given fileinfo object passes the collections set of filters.
        /// </summary>
        /// <param name="file">FileInfo object to check against filters.</param>
        /// <returns></returns>
        public static bool FilePassesFilter(this FilterCollection collection, FileInfo file)
        {
            if (file == null)
                return false;

            // Check for filtered extensions.
            if (collection.FilteredExtensions.Contains(file.Extension.ToLower()))
                return false;

            // Check for filtered file names without extension.
            if (collection.FilteredStrings.Contains(file.Name.ToLower().Replace(file.Extension, "")))
                return false;

            // Check for filtered file name with extension.
            if (collection.FilteredStrings.Contains(file.Name.ToLower()))
                return false;

            // Check for filtered directories.
            var dirs = file.FullName.Split('\\');
            foreach (var dir in dirs)
            {
                if (collection.FilteredDirectories.Contains(dir.ToLower()))
                    return false;
            }

            // Check for filtered regex expressions.
            foreach (var regex in collection.FilteredRegex)
            {
                try
                {
                    if (Regex.IsMatch(file.FullName, regex))
                        return false;
                }
                // Incase invalid regex causes exception.
                catch
                {

                }
            }

            return true;
        }
Example #32
0
        private async void btnKill_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_path))
            {
                SetStatus("No folder selected.");
                return;
            }

            SetStatus("Deleting. Please wait...");

            await Task.Run(() =>
              {
                  int deleted = 0;
                  var allFolders = Directory.EnumerateDirectories(_path, "*", System.IO.SearchOption.AllDirectories).Reverse().ToArray();
                  foreach (var folder in allFolders)
                  {
                      SetFilePath(folder);
                      SetStatus("Folders deleted: " + Convert.ToString(++deleted));

                      if (Directory.Exists(folder))
                      {
                          try
                          {
                              Directory.Delete(folder, true);
                          }
                          catch
                          {
                              foreach (var file in Directory.GetFiles(folder))
                              {
                                  var fi = new FileInfo(file);
                                  fi.IsReadOnly = false;
                              }

                              try
                              {
                                  Directory.Delete(folder, true);
                              }
                              catch
                              {
                                  SetStatus("Could not delete: " + folder);
                              }
                          }
                      }
                  }
                  try
                  {
                      if (Directory.Exists(_path))
                          Directory.Delete(_path, true);

                      SetStatus("Folder Deleted.");
                      SetFilePath(string.Empty);
                      return;
                  }
                  catch(Exception ex)
                  {
                      if (Directory.Exists(_path))
                      {
                          SetStatus("Could not delete all folders. You may need to delete the top folder manually.");
                      }
                  }

                  SetFilePath(_path);
              });
        }