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); }
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); } }
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); } }
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); } }
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); } }
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); } } }
public void TestOpenOpen() { var fi = new FileInfo(filePath); using (var fileStream = fi.Open(FileMode.Open)) { Assert.IsNotNull(fileStream); } }
public void TestOpenReadReadsExistingData() { var fi = new FileInfo(filePath); using (var fileStream = fi.OpenRead()) { Assert.AreEqual('t', fileStream.ReadByte()); } }
public void TestOpenTextReadsExistingData() { var fi = new FileInfo(filePath); using (var streamReader = fi.OpenText()) { Assert.AreEqual("test", streamReader.ReadLine()); } }
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); }
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); }
public void TestOpenCreateNew() { var fi = new FileInfo(filePath); Assert.Throws <IOException>(() => { using (var fileStream = fi.Open(FileMode.CreateNew)) { Assert.IsNotNull(fileStream); } }); }
public void TestLengthWithBadPath() { var filename = Util.CreateNewFile(uncDirectory); FileInfo fi; try { Assert.Throws <FileNotFoundException>(() => fi = new FileInfo(filename)); } catch { Pri.LongPath.File.Delete(filename); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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(); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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(); } }
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; }
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); }); }