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); }
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); }
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); }
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); } }
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); } }
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); } }
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()); } }
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); }
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})"); } }
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); } }
public string GetRelativePath([InjectSource] ExtendedFileInfo fileInfo) { if (fileInfo == null) { return(null); } return(fileInfo.FullName.Replace(fileInfo.ComputationRootDirectoryPath, string.Empty)); }
public string Sha256File([InjectSource] ExtendedFileInfo file) { if (file == null) { throw new InjectSourceNullReferenceException(typeof(ExtendedFileInfo)); } return(Sha256File(file.FileInfo)); }
public void Delete() { string tempFile = Path.GetTempFileName(); ExtendedFileInfo efi = new ExtendedFileInfo(tempFile); efi.Delete(); Assert.IsFalse(File.Exists(tempFile)); }
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)); }
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()); }
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(); }
public string Md5File([InjectSource] ExtendedFileInfo file) { if (file == null) { throw new InjectSourceNullReferenceException(typeof(ExtendedFileInfo)); } using (var stream = file.OpenRead()) { return(HashHelper.ComputeHash <MD5CryptoServiceProvider>(stream)); } }
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)); } }
public CompareDirectoriesResult( DirectoryInfo sourceRoot, ExtendedFileInfo sourceFile, DirectoryInfo destinationRoot, ExtendedFileInfo destinationFile, State state) { SourceRoot = sourceRoot; SourceFile = sourceFile; DestinationRoot = destinationRoot; DestinationFile = destinationFile; State = state; }
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]); }
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()); } }
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)); } }
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); }
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); } }
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()); }
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); }
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); }
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; }
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; }
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); } } }