Пример #1
0
        public void SetAccessControl()
        {
            string tempFile = Path.GetTempFileName();

            FileInfo     fi       = new FileInfo(tempFile);
            FileSecurity expected = fi.GetAccessControl(AccessControlSections.All);

            ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);

            Assert.IsNotNull(efi);

            FileSecurity fileSecurity = new FileSecurity();

            fileSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));

            efi.SetAccessControl(fileSecurity);

            Assert.AreNotEqual(expected.GetSecurityDescriptorBinaryForm(), efi.GetAccessControl().GetSecurityDescriptorBinaryForm());

            FileSecurity actualFileSecurity   = File.GetAccessControl(tempFile);
            AuthorizationRuleCollection rules = actualFileSecurity.GetAccessRules(true, true, typeof(NTAccount));

            foreach (AuthorizationRule rule in rules)
            {
                FileSystemAccessRule accessRule = (FileSystemAccessRule)rule;

                if (accessRule.IdentityReference.Value == "Everyone")
                {
                    Assert.IsTrue(accessRule.AccessControlType == AccessControlType.Allow);
                    Assert.IsTrue(accessRule.FileSystemRights == FileSystemRights.FullControl);
                }
            }

            fi.SetAccessControl(expected);
        }
Пример #2
0
        public void Properties5()
        {
            string tempFile = Path.GetTempFileName();

            ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);

            Assert.IsNotNull(efi);

            Assert.IsNotNull(efi.FileType);
            Assert.AreEqual(ExecutableType.Unknown, efi.ExecutableType);

            string        systemPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.System);
            DirectoryInfo di         = new DirectoryInfo(systemPath);

            tempFile = Path.Combine(di.Parent.FullName, "explorer.exe");
            efi      = new ExtendedFileInfo(tempFile);
            Assert.AreEqual("Application", efi.FileType);
            Assert.AreEqual(ExecutableType.Windows, efi.ExecutableType);

            tempFile = Path.Combine(Path.Combine(di.Parent.FullName, "system32"), "at.exe");
            efi      = new ExtendedFileInfo(tempFile);
            Assert.AreEqual(ExecutableType.Win32Console, efi.ExecutableType);

            tempFile = Path.Combine(Path.Combine(di.Parent.FullName, "system32"), "more.com");
            efi      = new ExtendedFileInfo(tempFile);
            Assert.AreEqual(ExecutableType.DOS, efi.ExecutableType);

            tempFile = Path.Combine(di.Parent.FullName, "system.ini");
            efi      = new ExtendedFileInfo(tempFile);
            Assert.AreEqual(ExecutableType.Unknown, efi.ExecutableType);
        }
Пример #3
0
        public void Properties()
        {
            string   tempFile = Path.GetTempFileName();
            FileInfo fi       = new FileInfo(tempFile);

            ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);

            Assert.IsNotNull(efi);

            Assert.AreEqual(efi.Name, fi.Name);
            Assert.AreEqual(efi.LastWriteTimUtc, fi.LastWriteTimeUtc);
            Assert.AreEqual(efi.LastWriteTime, fi.LastWriteTime);
            Assert.AreEqual(efi.LastAccessTimeUtc, fi.LastAccessTimeUtc);
            Assert.AreEqual(efi.LastAccessTime, fi.LastAccessTime);
            Assert.AreEqual(efi.FullName, fi.FullName);
            Assert.AreEqual(efi.Extension, fi.Extension);
            Assert.AreEqual(efi.Exists, fi.Exists);
            Assert.AreEqual(efi.CreationTime, fi.CreationTime);
            Assert.AreEqual(efi.CreateTimeUtc, fi.CreationTimeUtc);
            Assert.AreEqual(efi.Attributes, fi.Attributes);
            Assert.AreEqual(efi.Directory.FullName, fi.Directory.FullName);
            Assert.AreEqual(efi.DirectoryName, fi.DirectoryName);
            Assert.AreEqual(efi.IsReadOnly, fi.IsReadOnly);
            Assert.AreEqual(efi.Length, fi.Length);
        }
Пример #4
0
        public void Serialization()
        {
            string tempFile = Path.GetTempFileName();

            ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);

            Assert.IsNotNull(efi);
            byte[] data = null;

            using (MemoryStream stream = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(stream, efi);
                data = stream.ToArray();
            }

            Assert.IsNotNull(data);
            ConditionAssert.Greater(data.Length, 0);

            using (MemoryStream stream = new MemoryStream(data))
            {
                BinaryFormatter  bf   = new BinaryFormatter();
                ExtendedFileInfo efi2 = bf.Deserialize(stream) as ExtendedFileInfo;
                Assert.IsNotNull(efi2);
                Assert.AreEqual(efi.FullName, efi2.FullName);
            }
        }
Пример #5
0
        public byte[] GetFileBytes([InjectSource] ExtendedFileInfo file, long bytesCount = long.MaxValue, long offset = 0)
        {
            if (file == null)
            {
                throw new InjectSourceNullReferenceException(typeof(FileInfo));
            }

            using (var stream = file.OpenRead())
                using (var reader = new BinaryReader(stream))
                {
                    if (offset > 0)
                    {
                        stream.Seek(offset, SeekOrigin.Begin);
                    }

                    var toRead = bytesCount < stream.Length ? bytesCount : stream.Length;

                    var bytes = new byte[toRead];

                    for (var i = 0; i < toRead; ++i)
                    {
                        bytes[i] = reader.ReadByte();
                    }

                    return(bytes);
                }
        }
Пример #6
0
        public void Constructor()
        {
            try
            {
                ExtendedFileInfo efi = new ExtendedFileInfo(null);
            }
            catch (ArgumentNullException)
            {
                Assert.IsTrue(true);
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }

            try
            {
                ExtendedFileInfo efi = new ExtendedFileInfo(String.Empty);
            }
            catch (ArgumentException)
            {
                Assert.IsTrue(true);
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }
        }
Пример #7
0
        public string GetLinesContainingWord([InjectSource] ExtendedFileInfo file, string word)
        {
            if (file == null)
            {
                throw new InjectSourceNullReferenceException(typeof(ExtendedFileInfo));
            }

            using (var stream = new StreamReader(file.OpenRead()))
            {
                var lines = new List <string>();
                var line  = 1;
                while (!stream.EndOfStream)
                {
                    var strLine = stream.ReadLine();
                    if (strLine != null && strLine.Contains(word))
                    {
                        lines.Add(line.ToString());
                    }
                    line += 1;
                }

                var builder = new StringBuilder("(");

                for (int i = 0, j = lines.Count - 1; i < j; ++i)
                {
                    builder.Append(lines[i]);
                }

                builder.Append(lines[lines.Count]);
                builder.Append(')');

                return(builder.ToString());
            }
        }
Пример #8
0
        public void Replace3()
        {
            string tempFile   = Path.Combine(Path.GetTempPath(), "OriginalFile.txt");
            string tempFile2  = Path.Combine(Path.GetTempPath(), "NewFile.txt");
            string backupFile = Path.Combine(Path.GetTempPath(), "OriginalFileBackup.txt");
            string expected   = "This is a test.";

            File.Delete(tempFile);
            File.Delete(tempFile2);
            File.Delete(backupFile);

            File.AppendAllText(tempFile, expected);
            File.AppendAllText(tempFile2, String.Empty);

            // Move the contents of tempFile into tempFile2
            ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);

            efi.Replace(tempFile2, backupFile, true);

            Assert.IsTrue(File.Exists(backupFile));
            Assert.IsTrue(File.Exists(tempFile2));

            string actual = File.ReadAllText(tempFile2);

            Assert.AreEqual(expected, actual);
        }
Пример #9
0
        public long GetLengthOfFile([InjectSource] ExtendedFileInfo context, string unit = "b")
        {
            if (context == null)
            {
                throw new InjectSourceNullReferenceException(typeof(ExtendedFileInfo));
            }

            switch (unit.ToLowerInvariant())
            {
            case "b":
                return(context.Length);

            case "kb":
                return(Convert.ToInt64(context.Length / 1024f));

            case "mb":
                return(Convert.ToInt64(context.Length / 1024f / 1024f));

            case "gb":
                return(Convert.ToInt64(context.Length / 1024f / 1024f / 1024f));

            default:
                throw new NotSupportedException($"unsupported unit ({unit})");
            }
        }
Пример #10
0
        public long CountOfNotEmptyLines([InjectSource] ExtendedFileInfo context)
        {
            if (context == null)
            {
                throw new InjectSourceNullReferenceException(typeof(ExtendedFileInfo));
            }

            using (var stream = new StreamReader(context.OpenRead()))
            {
                var lines = 0;
                while (!stream.EndOfStream)
                {
                    var line = stream.ReadLine();

                    if (line == string.Empty)
                    {
                        continue;
                    }

                    lines += 1;
                }

                return(lines);
            }
        }
Пример #11
0
        public string GetRelativePath([InjectSource] ExtendedFileInfo fileInfo)
        {
            if (fileInfo == null)
            {
                return(null);
            }

            return(fileInfo.FullName.Replace(fileInfo.ComputationRootDirectoryPath, string.Empty));
        }
Пример #12
0
        public string Sha256File([InjectSource] ExtendedFileInfo file)
        {
            if (file == null)
            {
                throw new InjectSourceNullReferenceException(typeof(ExtendedFileInfo));
            }

            return(Sha256File(file.FileInfo));
        }
Пример #13
0
        public void Delete()
        {
            string tempFile = Path.GetTempFileName();

            ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);

            efi.Delete();

            Assert.IsFalse(File.Exists(tempFile));
        }
Пример #14
0
        public void CopyTo()
        {
            string tempFile  = Path.GetTempFileName();
            string tempFile2 = Path.GetFileNameWithoutExtension(tempFile) + "Copy" + Path.GetExtension(tempFile);

            ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);

            efi.CopyTo(tempFile2);

            Assert.IsTrue(File.Exists(tempFile2));
        }
Пример #15
0
        public void ToStringTest()
        {
            string tempFile = Path.GetTempFileName();

            FileInfo         fi  = new FileInfo(tempFile);
            ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);

            Assert.IsNotNull(efi);

            Assert.AreEqual(fi.ToString(), efi.ToString());
        }
Пример #16
0
 public SizeNode(FolderInfo fi)
     : this(
         fi.DisplayName, fi.FullPath, fi.State, fi.Size, ExtendedFileInfo.GetIconForFilename(fi.FullPath, true), fi.FullPath)
 {
     this.folderInfo = fi;
     directories     = null; // not loaded yet
     if (fi.State == ScanState.SCANNING || fi.State == ScanState.DONE)
     {
         FillInFiles();
     }
     BuildDummyNode();
 }
Пример #17
0
        public string Md5File([InjectSource] ExtendedFileInfo file)
        {
            if (file == null)
            {
                throw new InjectSourceNullReferenceException(typeof(ExtendedFileInfo));
            }

            using (var stream = file.OpenRead())
            {
                return(HashHelper.ComputeHash <MD5CryptoServiceProvider>(stream));
            }
        }
Пример #18
0
        public void Create()
        {
            string tempFile = Path.Combine(Path.GetTempPath(), "CreatedFile.txt");

            File.Delete(tempFile);

            ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);

            using (FileStream stream = efi.Create())
            {
                Assert.IsTrue(File.Exists(tempFile));
            }
        }
Пример #19
0
 public CompareDirectoriesResult(
     DirectoryInfo sourceRoot,
     ExtendedFileInfo sourceFile,
     DirectoryInfo destinationRoot,
     ExtendedFileInfo destinationFile,
     State state)
 {
     SourceRoot      = sourceRoot;
     SourceFile      = sourceFile;
     DestinationRoot = destinationRoot;
     DestinationFile = destinationFile;
     State           = state;
 }
Пример #20
0
 public static Icon GetIconForExtension(string extension)
 {
     if (!extensionIcons.ContainsKey(extension))
     {
         Icon ic = ExtendedFileInfo.GetExtensionIcon(extension, true);
         if (ic == null)
         {
             throw new NullReferenceException("Icon for extension: " + extension);
         }
         extensionIcons[extension] = ic;
     }
     return(extensionIcons[extension]);
 }
Пример #21
0
        public string GetFileContent([InjectSource] ExtendedFileInfo extendedFileInfo)
        {
            if (!extendedFileInfo.Exists)
            {
                return(null);
            }

            using (var file = extendedFileInfo.OpenRead())
                using (var fileReader = new StreamReader(file))
                {
                    return(fileReader.ReadToEnd());
                }
        }
Пример #22
0
        public bool HasContent([InjectSource] ExtendedFileInfo file, string pattern)
        {
            if (file == null)
            {
                throw new InjectSourceNullReferenceException(typeof(ExtendedFileInfo));
            }

            using (var stream = new StreamReader(file.OpenRead()))
            {
                var content = stream.ReadToEnd();
                return(Regex.IsMatch(content, pattern));
            }
        }
Пример #23
0
        public void Properties3()
        {
            string tempFile = Path.GetTempFileName();

            ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);

            Assert.IsNotNull(efi);

            efi.CreationTime = new DateTime(2006, 1, 1);

            FileInfo fi = new FileInfo(tempFile);

            Assert.AreEqual(efi.CreationTime, fi.CreationTime);
        }
Пример #24
0
        public void OpenRead()
        {
            string tempFile = Path.GetTempFileName();

            ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);

            Assert.IsNotNull(efi);

            using (FileStream stream = efi.OpenRead())
            {
                Assert.IsTrue(stream.CanRead);
                Assert.IsFalse(stream.CanWrite);
            }
        }
Пример #25
0
        public void GetAccessControl1()
        {
            string   tempFile = Path.GetTempFileName();
            FileInfo fi       = new FileInfo(tempFile);

            ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);

            Assert.IsNotNull(efi);

            FileSecurity actual   = efi.GetAccessControl(AccessControlSections.All);
            FileSecurity expected = fi.GetAccessControl(AccessControlSections.All);

            CollectionAssert.AreEqual(expected.GetSecurityDescriptorBinaryForm(), actual.GetSecurityDescriptorBinaryForm());
        }
Пример #26
0
        public void FileOwner()
        {
            string tempFile = Path.GetTempFileName();

            FileInfo     fi       = new FileInfo(tempFile);
            FileSecurity fs       = fi.GetAccessControl(AccessControlSections.Owner);
            string       expected = fs.GetOwner(typeof(NTAccount)).ToString();

            ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);

            Assert.IsNotNull(efi);
            string actual = efi.FileOwner;

            Assert.AreEqual(expected, actual);
        }
Пример #27
0
        public void Encryption()
        {
            string tempFile = Path.GetTempFileName();

            ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);

            efi.Encrypt();
            efi.Refresh();

            Assert.IsTrue((efi.Attributes & FileAttributes.Encrypted) == FileAttributes.Encrypted);

            efi.Decrypt();
            efi.Refresh();
            Assert.IsFalse((efi.Attributes & FileAttributes.Encrypted) == FileAttributes.Encrypted);
        }
Пример #28
0
        public void Properties2()
        {
            string tempFile = Path.GetTempFileName();

            ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);

            Assert.IsNotNull(efi);

            efi.Attributes |= FileAttributes.ReadOnly;

            FileInfo fi = new FileInfo(tempFile);

            Assert.AreEqual(efi.Attributes, fi.Attributes);

            fi.Attributes &= ~FileAttributes.ReadOnly;
        }
Пример #29
0
        public void Properties4()
        {
            string tempFile = Path.GetTempFileName();

            ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);

            Assert.IsNotNull(efi);

            efi.IsReadOnly = true;

            FileInfo fi = new FileInfo(tempFile);

            Assert.IsTrue((fi.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);

            fi.Attributes &= ~FileAttributes.ReadOnly;
        }
Пример #30
0
        public void Open()
        {
            string tempFile = Path.GetTempFileName();

            ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);

            using (FileStream stream = efi.Open(FileMode.Open))
            {
                Assert.IsNotNull(stream);
                Assert.IsTrue(stream.Length == 0);
            }

            tempFile = Path.GetTempFileName();
            efi      = new ExtendedFileInfo(tempFile);
            using (FileStream stream = efi.Open(FileMode.Open, FileAccess.Read))
            {
                Assert.IsNotNull(stream);
                Assert.IsTrue(stream.Length == 0);
                Assert.IsTrue(stream.CanRead);
                Assert.IsFalse(stream.CanWrite);
            }

            tempFile = Path.GetTempFileName();
            efi      = new ExtendedFileInfo(tempFile);
            using (FileStream stream = efi.Open(FileMode.Open, FileAccess.Read, FileShare.None))
            {
                Assert.IsNotNull(stream);
                Assert.IsTrue(stream.Length == 0);
                Assert.IsTrue(stream.CanRead);
                Assert.IsFalse(stream.CanWrite);

                try
                {
                    File.Open(tempFile, FileMode.Open);
                }
                catch (IOException)
                {
                    Assert.IsTrue(true);
                }
                catch (Exception e)
                {
                    Assert.Fail(e.Message);
                }
            }
        }