public void TestCopy() { var tempLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("file22.ext").ToString(); var tempDestLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("file22-1.ext").ToString(); Assert.IsFalse(File.Exists(tempLongPathFilename)); File.Copy(filePath, tempLongPathFilename); try { Assert.IsTrue(File.Exists(tempLongPathFilename)); File.Move(tempLongPathFilename, tempDestLongPathFilename); try { Assert.IsFalse(File.Exists(tempLongPathFilename)); Assert.IsTrue(File.Exists(tempDestLongPathFilename)); } finally { File.Delete(tempDestLongPathFilename); } } finally { if (File.Exists(tempLongPathFilename)) { File.Delete(tempLongPathFilename); } } }
public void TestGetAccessControlSections() { var filename = Util.CreateNewFile(uncDirectory); try { var security = File.GetAccessControl(filename, 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 { File.Delete(filename); } }
public void SetUp() { directory = TestContext.CurrentContext.TestDirectory.Combine("subdir"); Directory.CreateDirectory(directory); try { uncDirectory = UncHelper.GetUncFromPath(directory); filePath = new StringBuilder(directory).Append(@"\").Append(Filename).ToString(); uncFilePath = UncHelper.GetUncFromPath(filePath); using (var writer = System.IO.File.CreateText(filePath)) { writer.WriteLine("test"); } Debug.Assert(File.Exists(uncFilePath)); } catch (Exception) { if (Directory.Exists(directory)) { Directory.Delete(directory, true); } throw; } }
public void TestReplaceIgnoreMergeWithInvalidBackupPath() { var tempLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("filename.ext").ToString(); var tempBackupLongPathFilename = new StringBuilder(uncDirectory).Append(@"\gibberish\").Append("backup").ToString(); using (var fileStream = File.Create(tempLongPathFilename)) { fileStream.WriteByte(42); } var tempLongPathFilename2 = new StringBuilder(uncDirectory).Append(@"\").Append("filename2.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename2)) { fileStream.WriteByte(52); } try { const Boolean ignoreMetadataErrors = true; Assert.Throws <IOException>(() => File.Replace(tempLongPathFilename, tempLongPathFilename2, tempBackupLongPathFilename, ignoreMetadataErrors)); } finally { if (File.Exists(tempLongPathFilename)) { File.Delete(tempLongPathFilename); } File.Delete(tempLongPathFilename2); Assert.Throws <DirectoryNotFoundException>(() => File.Delete(tempBackupLongPathFilename)); } }
public void TestGetRecursiveFilesWithSubsetSearch() { var tempLongPathFilename = longPathDirectory.Combine(Path.GetRandomFileName()); tempLongPathFilename.CreateDirectory(); try { Assert.IsTrue(tempLongPathFilename.Exists()); var newEmptyFile1 = Util.CreateNewEmptyFile(tempLongPathFilename, "A-file"); var newEmptyFile2 = Util.CreateNewEmptyFile(tempLongPathFilename, "B-file"); try { var randomFileName = newEmptyFile1.GetFileName(); var di = new DirectoryInfo(longPathDirectory); var files = di.GetFiles("A*", SearchOption.AllDirectories).ToArray(); Assert.AreEqual(1, files.Length); Assert.IsTrue(files.Any(f => f.Name == newEmptyFile1.GetFileName() && f.DirectoryName == newEmptyFile1.GetDirectoryName())); Assert.IsFalse(files.Any(f => f.Name == newEmptyFile2.GetFileName() && f.DirectoryName == newEmptyFile2.GetDirectoryName())); Assert.IsFalse(files.Any(f => f.Name == Filename.GetFileName() && f.DirectoryName == Filename.GetDirectoryName())); } finally { File.Delete(newEmptyFile1); File.Delete(newEmptyFile2); } } finally { const Boolean recursive = true; Directory.Delete(tempLongPathFilename, recursive); } }
public void TestDeleteDirectory_JunctionPoint() { var targetFolder = rootTestDir.Combine("ADirectory"); var junctionPoint = rootTestDir.Combine("SymLink"); targetFolder.CreateDirectory(); try { var targetFile = targetFolder.Combine("AFile"); File.Create(targetFile).Close(); try { JunctionPoint.Create(junctionPoint, targetFolder, overwrite: false); Assert.IsTrue(File.Exists(targetFolder.Combine("AFile")), "File should be accessible."); Assert.IsTrue(File.Exists(junctionPoint.Combine("AFile")), "File should be accessible via the junction point."); Directory.Delete(junctionPoint, false); Assert.IsTrue(File.Exists(targetFolder.Combine("AFile")), "File should be accessible."); Assert.IsFalse(JunctionPoint.Exists(junctionPoint), "Junction point should not exist now."); Assert.IsTrue(!File.Exists(junctionPoint.Combine("AFile")), "File should not be accessible via the junction point."); } finally { File.Delete(targetFile); } } finally { Directory.Delete(targetFolder); } }
public void TestReplace() { var tempLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("filename.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename)) { fileStream.WriteByte(42); } var tempLongPathFilename2 = new StringBuilder(uncDirectory).Append(@"\").Append("filename2.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename2)) { fileStream.WriteByte(52); } try { File.Replace(tempLongPathFilename, tempLongPathFilename2, null); using (var fileStream = File.OpenRead(tempLongPathFilename2)) { Assert.AreEqual(42, fileStream.ReadByte()); } Assert.IsFalse(File.Exists(tempLongPathFilename)); } finally { File.Delete(tempLongPathFilename2); } }
public void TestReplaceIgnoreMerge() { var tempLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("filename.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename)) { fileStream.WriteByte(42); } var tempLongPathFilename2 = new StringBuilder(uncDirectory).Append(@"\").Append("filename2.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename2)) { fileStream.WriteByte(52); } try { const Boolean ignoreMetadataErrors = true; File.Replace(tempLongPathFilename, tempLongPathFilename2, null, ignoreMetadataErrors); 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 TestMoveTo() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file21.ext").ToString(); var tempDestLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file21-1.ext").ToString(); Assert.IsFalse(File.Exists(tempLongPathFilename)); File.Copy(longPathFilename, tempLongPathFilename); try { Assert.IsTrue(File.Exists(tempLongPathFilename)); var fi = new FileInfo(tempLongPathFilename); fi.MoveTo(tempDestLongPathFilename); try { Assert.IsFalse(File.Exists(tempLongPathFilename)); Assert.IsTrue(File.Exists(tempDestLongPathFilename)); } finally { File.Delete(tempDestLongPathFilename); } } finally { if (File.Exists(tempLongPathFilename)) { File.Delete(tempLongPathFilename); } } }
public void TestDecrypt() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString(); try { using (var s = File.Create(tempLongPathFilename, 200)) { } var preAttrib = File.GetAttributes(tempLongPathFilename); Assert.AreEqual(( FileAttributes )0, preAttrib & FileAttributes.Encrypted); var fi = new FileInfo(tempLongPathFilename); fi.Encrypt(); var postAttrib = File.GetAttributes(tempLongPathFilename); Assert.AreEqual(FileAttributes.Encrypted, postAttrib & FileAttributes.Encrypted); fi.Decrypt(); postAttrib = File.GetAttributes(tempLongPathFilename); Assert.AreEqual(( FileAttributes )0, postAttrib & FileAttributes.Encrypted); } finally { File.Delete(tempLongPathFilename); } }
public void TestCreateTextAndWrite() { Assert.IsTrue(longPathDirectory.Exists()); String tempLongPathFilename; do { tempLongPathFilename = longPathDirectory.Combine(Path.GetRandomFileName()); } while (File.Exists(tempLongPathFilename)); Assert.IsFalse(File.Exists(tempLongPathFilename)); const String fileText = "test"; using (var writer = File.CreateText(tempLongPathFilename)) { writer.WriteLine(fileText); } try { Assert.IsTrue(File.Exists(tempLongPathFilename)); var fileInfo = new FileInfo(tempLongPathFilename); Assert.AreEqual(fileText.Length + Environment.NewLine.Length, fileInfo.Length); } finally { 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 TestOpenWithAccessNonExistent() { var tempLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append(Path.GetRandomFileName()).ToString(); Assert.Throws <FileNotFoundException>(() => { using (File.Open(tempLongPathFilename, FileMode.Open, FileAccess.Read)) { } }); }
public void TestReplaceWithNulls() { var filename = Util.CreateNewFile(uncDirectory); try { Assert.Throws <ArgumentNullException>(() => File.Replace(null, null, null)); } finally { File.Delete(filename); } }
public void TestLengthWithBadPath() { var filename = Util.CreateNewFile(longPathDirectory); FileInfo fi = null; try { Assert.Throws <FileNotFoundException>(() => fi = new FileInfo(filename)); } catch { File.Delete(filename); } }
public void TestSetAccessControl() { var filename = Util.CreateNewFile(uncDirectory); try { var security = new FileSecurity(); File.SetAccessControl(filename, security); } finally { File.Delete(filename); } }
public void TestReplaceIgnoreMergeNullDestination() { const Boolean ignoreMetadataErrors = true; var filename = Util.CreateNewFile(uncDirectory); try { Assert.Throws <ArgumentNullException>(() => File.Replace(filePath, null, null, ignoreMetadataErrors)); } finally { File.Delete(filename); } }
public void TestCreateWithFileSecurity() { var tempLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("filename.ext").ToString(); using (var s = File.Create(tempLongPathFilename, 200, FileOptions.DeleteOnClose, new FileSecurity())) { s.WriteByte(42); s.Seek(0, SeekOrigin.Begin); Assert.AreEqual(42, s.ReadByte()); } Assert.IsFalse(File.Exists(tempLongPathFilename)); }
public void TestAppendAllText() { var filename = Util.CreateNewFile(longPathDirectory); try { File.AppendAllText(filename, "test"); Assert.AreEqual("beginning of file" + Environment.NewLine + "test", File.ReadAllText(filename)); } finally { File.Delete(filename); } }
public void TestAppendAllTextEncoding() { var filename = Util.CreateNewFileUnicode(uncDirectory); try { File.AppendAllText(filename, "test", Encoding.Unicode); Assert.AreEqual("beginning of file" + Environment.NewLine + "test", File.ReadAllText(filename, Encoding.Unicode)); } finally { File.Delete(filename); } }
public void TestInUseMove() { const Boolean recursive = true; #if SHORT_SOURCE var tempPathFilename1 = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), System.IO.Path.GetRandomFileName()); System.IO.Directory.CreateDirectory(tempPathFilename1); Assert.IsTrue(System.IO.Directory.Exists(Path.GetFullPath(tempPathFilename1))); var tempPathFilename2 = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), System.IO.Path.GetRandomFileName()); System.IO.Directory.CreateDirectory(tempPathFilename2); Assert.IsTrue(System.IO.Directory.Exists(System.IO.Path.GetFullPath(tempPathFilename2))); try { using ( var writer = System.IO.File.CreateText(System.IO.Path.Combine(tempPathFilename2, "TestInUseMove"))) { string destinationPath = System.IO.Path.GetFullPath(System.IO.Path.Combine(tempPathFilename1, System.IO.Path.GetFileName(tempPathFilename2))); System.IO.Directory.Move(tempPathFilename2, destinationPath); Assert.IsTrue(System.IO.Directory.Exists(System.IO.Path.GetFullPath(tempPathFilename1))); Assert.IsFalse(System.IO.Directory.Exists(System.IO.Path.GetFullPath(tempPathFilename2))); Assert.IsTrue(System.IO.Directory.Exists(destinationPath)); } } catch (Exception e) { throw; } finally { Directory.Delete(tempPathFilename1, recursive); Directory.Delete(tempPathFilename2, recursive); } #endif var tempLongPathFilename1 = longPathDirectory.Combine(Pri.LongPath.Path.GetRandomFileName()); tempLongPathFilename1.CreateDirectory(); Assert.IsTrue(tempLongPathFilename1.GetFullPath().Exists()); var tempLongPathFilename2 = longPathDirectory.Combine(Pri.LongPath.Path.GetRandomFileName()); tempLongPathFilename2.CreateDirectory(); Assert.IsTrue(tempLongPathFilename2.GetFullPath().Exists()); try { using (var writer = File.CreateText(tempLongPathFilename2.Combine("TestInUseMove"))) { var destinationPath = tempLongPathFilename1.Combine(tempLongPathFilename2.GetFileName()).GetFullPath(); Assert.Throws <IOException>(() => Directory.Move(tempLongPathFilename2, destinationPath)); } } finally { Directory.Delete(tempLongPathFilename1, recursive); Directory.Delete(tempLongPathFilename2, recursive); } }
public void TestGetIsReadOnly() { var filename = Util.CreateNewFile(longPathDirectory); try { var fi = new FileInfo(filename); Assert.IsTrue(fi.Exists); Assert.IsFalse(fi.IsReadOnly); } finally { File.Delete(filename); } }
public void TestSetAccessControl() { var filename = Util.CreateNewFile(longPathDirectory); try { var fi = new FileInfo(filename); var security = new FileSecurity(); fi.SetAccessControl(security); } 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 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 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 TestReplaceIgnoreMerge() { 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 { const Boolean ignoreMetadataErrors = true; var fi2 = fi.Replace(tempLongPathFilename2, null, ignoreMetadataErrors); 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 TestReplaceIgnoreMergeWithReadonlyBackupPath() { var tempLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("filename.ext").ToString(); var tempBackupPathName = new StringBuilder(uncDirectory).Append(@"\readonly").ToString(); var di = new DirectoryInfo(tempBackupPathName); di.Create(); var attr = di.Attributes; di.Attributes = attr | FileAttributes.ReadOnly; var tempBackupLongPathFilename = new StringBuilder(tempBackupPathName).Append(@"\").Append("backup").ToString(); using (var fileStream = File.Create(tempLongPathFilename)) { fileStream.WriteByte(42); } var tempLongPathFilename2 = new StringBuilder(uncDirectory).Append(@"\").Append("filename2.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename2)) { fileStream.WriteByte(52); } try { const Boolean ignoreMetadataErrors = true; File.Replace(tempLongPathFilename, tempLongPathFilename2, tempBackupLongPathFilename, ignoreMetadataErrors); using (var fileStream = File.OpenRead(tempLongPathFilename2)) { Assert.AreEqual(42, fileStream.ReadByte()); } Assert.IsFalse(File.Exists(tempLongPathFilename)); Assert.IsTrue(File.Exists(tempBackupLongPathFilename)); } finally { di.Attributes = attr; if (File.Exists(tempLongPathFilename)) { File.Delete(tempLongPathFilename); } File.Delete(tempLongPathFilename2); File.Delete(tempBackupLongPathFilename); if (tempBackupPathName.Exists()) { Pri.LongPath.Directory.Delete(tempBackupPathName); } } }
public void TestCopyWithoutOverwriteAndExistingFile() { var destLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("filename (Copy).ext").ToString(); File.Copy(filePath, destLongPathFilename); try { Assert.IsTrue(File.Exists(destLongPathFilename)); Assert.Throws <IOException>(() => File.Copy(filePath, destLongPathFilename)); } finally { File.Delete(destLongPathFilename); } }
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); } }