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 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 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 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 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 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 TestCreateWithBuffersize() { var tempLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("filename.ext").ToString(); try { using (var s = File.Create(tempLongPathFilename, 200)) { s.WriteByte(42); s.Seek(0, SeekOrigin.Begin); Assert.AreEqual(42, s.ReadByte()); } } finally { File.Delete(tempLongPathFilename); } }
public void TestOpenWithAccess() { var tempLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append(Path.GetRandomFileName()).ToString(); try { using (var s = File.Create(tempLongPathFilename)) { s.WriteByte(42); } using (File.Open(tempLongPathFilename, FileMode.Open, FileAccess.Read)) { } } finally { File.Delete(tempLongPathFilename); } }
public void TestReadAllBytesOnHugeFile() { var tempLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("filename.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename)) { fileStream.Seek(( Int64 )Int32.MaxValue + 1, SeekOrigin.Begin); fileStream.WriteByte(42); } try { Assert.Throws <IOException>(() => File.ReadAllBytes(tempLongPathFilename)); } finally { File.Delete(tempLongPathFilename); } }
public void TestReadAllBytes() { var tempLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("filename.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename)) { fileStream.WriteByte(42); } try { Assert.IsTrue(new Byte[] { 42 }.SequenceEqual(File.ReadAllBytes(tempLongPathFilename))); } finally { File.Delete(tempLongPathFilename); } }
public void TestOpenRead() { var tempLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("filename.ext").ToString(); try { using (var s = File.Create(tempLongPathFilename)) { s.WriteByte(42); } using (var stream = File.OpenRead(tempLongPathFilename)) { Assert.AreEqual(42, stream.ReadByte()); } } finally { File.Delete(tempLongPathFilename); } }
public void TestOpenExisting() { var tempLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("filename.ext").ToString(); try { using (var s = File.Create(tempLongPathFilename)) { s.WriteByte(42); } using (var stream = File.Open(tempLongPathFilename, FileMode.Open)) { Assert.IsNotNull(stream); } } finally { File.Delete(tempLongPathFilename); } }
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 GetTarget_CalledOnAFile() { File.Create(this.tempFolder.Combine("AFile")).Close(); Assert.Throws <IOException>(() => JunctionPoint.GetTarget(this.tempFolder.Combine("AFile")), "Path is not a junction point."); }
public void Exists_IsADirectory() { File.Create(this.tempFolder.Combine("AFile")).Close(); Assert.IsFalse(JunctionPoint.Exists(this.tempFolder.Combine("AFile"))); }