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 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 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 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 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(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 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 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 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 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 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 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 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 TestCopyWithOverwrite() { var destLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("filename (Copy).ext").ToString(); File.Copy(filePath, destLongPathFilename); try { Assert.IsTrue(File.Exists(destLongPathFilename)); File.Copy(filePath, destLongPathFilename, true); Assert.AreEqual(File.ReadAllText(filePath), File.ReadAllText(destLongPathFilename)); } finally { File.Delete(destLongPathFilename); } }
public void TestCopyToWithOverwrite() { 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)); fi.CopyTo(destLongPathFilename, true); Assert.AreEqual(File.ReadAllText(longPathFilename), File.ReadAllText(destLongPathFilename)); } finally { File.Delete(destLongPathFilename); } }
public void TestCreateText() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file20.ext").ToString(); var fi = new FileInfo(tempLongPathFilename); Assert.IsFalse(fi.Exists); using (fi.CreateText()) { } try { Assert.IsTrue(File.Exists(fi.FullName)); // don't use FileInfo.Exists, it caches existance } finally { fi.Delete(); } }
public void SetUp() { rootTestDir = TestContext.CurrentContext.TestDirectory; longPathDirectory = Util.MakeLongPath(rootTestDir); longPathRoot = longPathDirectory.Substring(0, TestContext.CurrentContext.TestDirectory.Length + 1 + longPathDirectory.Substring(rootTestDir.Length + 1).IndexOf('\\')); longPathDirectory.CreateDirectory(); Debug.Assert(longPathDirectory.Exists()); longPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append(Filename).ToString(); using (var writer = File.CreateText(longPathFilename)) { writer.WriteLine("test"); } Debug.Assert(File.Exists(longPathFilename)); }
public void TestMove() { var sourceFilename = Util.CreateNewFile(uncDirectory); var destFilename = uncDirectory.Combine(Path.GetRandomFileName()); File.Move(sourceFilename, destFilename); try { Assert.IsFalse(File.Exists(sourceFilename)); Assert.IsTrue(File.Exists(destFilename)); Assert.IsTrue(Util.VerifyContentsOfNewFile(destFilename)); } finally { if (File.Exists(destFilename)) { File.Delete(destFilename); } } }
public void Create_VerifyExists_GetTarget_Delete() { var targetFolder = this.tempFolder.Combine("ADirectory"); var junctionPoint = this.tempFolder.Combine("SymLink"); targetFolder.CreateDirectory(); try { File.Create(targetFolder.Combine("AFile")).Close(); try { // Verify behavior before junction point created. Assert.IsFalse(File.Exists(junctionPoint.Combine("AFile")), "File should not be located until junction point created."); Assert.IsFalse(JunctionPoint.Exists(junctionPoint), "Junction point not created yet."); // Create junction point and confirm its properties. JunctionPoint.Create(junctionPoint, targetFolder, overwrite: false); Assert.IsTrue(JunctionPoint.Exists(junctionPoint), "Junction point exists now."); Assert.AreEqual(targetFolder, JunctionPoint.GetTarget(junctionPoint)); Assert.IsTrue(File.Exists(junctionPoint.Combine("AFile")), "File should be accessible via the junction point."); // Delete junction point. JunctionPoint.Delete(junctionPoint); Assert.IsFalse(JunctionPoint.Exists(junctionPoint), "Junction point should not exist now."); Assert.IsFalse(File.Exists(junctionPoint.Combine("AFile")), "File should not be located after junction point deleted."); Assert.IsFalse(junctionPoint.Exists(), "Ensure directory was deleted too."); } finally { File.Delete(targetFolder.Combine("AFile")); } } finally { Directory.Delete(targetFolder); } }
public void TearDown() { try { if (File.Exists(longPathFilename)) { File.Delete(longPathFilename); } } catch (Exception e) { Trace.WriteLine("Exception {0} deleting \"longPathFilename\"", e.ToString()); throw; } finally { if (longPathRoot.Exists()) { Directory.Delete(longPathRoot, true); } } }
public void TearDown() { try { if (File.Exists(filePath)) { File.Delete(filePath); } } catch (Exception e) { Trace.WriteLine("Exception {0} deleting \"filePath\"", e.ToString()); throw; } finally { if (directory.Exists()) { Pri.LongPath.Directory.Delete(directory, true); } } }
public void TestCreateText() { var filename = new StringBuilder(uncDirectory).Append(@"\").Append("file3.ext").ToString(); const String fileText = "test"; using (var writer = File.CreateText(filename)) { writer.WriteLine(fileText); } try { Assert.IsTrue(File.Exists(filename)); using (var reader = File.OpenText(filename)) { var text = reader.ReadLine(); Assert.AreEqual(fileText, text); } } finally { File.Delete(filename); } }
public void TestAppendText() { var filename = new StringBuilder(longPathDirectory).Append(@"\").Append("file16.ext").ToString(); using (var writer = File.CreateText(filename)) { writer.Write("start"); } Assert.IsTrue(File.Exists(filename)); try { using (var writer = new FileInfo(filename).AppendText()) { writer.WriteLine("end"); } using (var reader = File.OpenText(filename)) { var text = reader.ReadLine(); Assert.AreEqual("startend", text); } } finally { File.Delete(filename); } }
public void CanCreateFileInfoWithLongPathFile() { String tempLongPathFilename; do { tempLongPathFilename = longPathDirectory.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.IsNotNull(fileInfo); // just to use fileInfo variable } finally { File.Delete(tempLongPathFilename); } }
public void TestExists() => Assert.IsTrue(File.Exists(longPathFilename));
public void TestExists() => Assert.IsTrue(File.Exists(filePath));