public void TestSetCreationTime() { var filename = Util.CreateNewFile(longPathDirectory); try { DateTime dateTime = DateTime.Now.AddDays(1); File.SetCreationTime(filename, dateTime); var fi = new FileInfo(filename); Assert.AreEqual(fi.CreationTime, dateTime); } finally { File.Delete(filename); } }
public void TestCopyWithoutOverwriteAndExistingFile() { var destLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename (Copy).ext").ToString(); File.Copy(longPathFilename, destLongPathFilename); try { Assert.IsTrue(File.Exists(destLongPathFilename)); Assert.Throws <IOException>(() => File.Copy(longPathFilename, destLongPathFilename)); } finally { File.Delete(destLongPathFilename); } }
public void TestWriteAllLinesWithEncodingEnumerable() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file26.ext").ToString(); File.WriteAllLines(tempLongPathFilename, new List <string> { "file26" }, Encoding.Unicode); try { Assert.AreEqual("file26" + Environment.NewLine, File.ReadAllText(tempLongPathFilename, Encoding.Unicode)); } finally { File.Delete(tempLongPathFilename); } }
public void TestSetLastAccessTimeUtc() { var filename = Util.CreateNewFile(longPathDirectory); try { DateTime dateTime = DateTime.UtcNow.AddDays(1); var fi = new FileInfo(filename); fi.LastAccessTimeUtc = dateTime; Assert.AreEqual(dateTime, File.GetLastAccessTimeUtc(filename)); } finally { File.Delete(filename); } }
public void TestOpenReadWithWrite() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file31.ext").ToString(); var fi = new FileInfo(tempLongPathFilename); try { using (var fileStream = fi.Open(FileMode.Append, FileAccess.Read)) { fileStream.WriteByte(43); } } finally { File.Delete(tempLongPathFilename); } }
public void TestReadAllBytes() { var tempLongPathFilename = new StringBuilder(longPathDirectory).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 TestCopyWithOverwrite() { var destLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename (Copy).ext").ToString(); File.Copy(longPathFilename, destLongPathFilename); try { Assert.IsTrue(File.Exists(destLongPathFilename)); File.Copy(longPathFilename, destLongPathFilename, true); Assert.AreEqual(File.ReadAllText(longPathFilename), File.ReadAllText(destLongPathFilename)); } finally { File.Delete(destLongPathFilename); } }
public void TestCopyToWithoutOverwriteAndExistingFile() { 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); } finally { File.Delete(destLongPathFilename); } }
public void TestReadAllTextEncoding() { var filename = new StringBuilder(longPathDirectory).Append(@"\").Append("file3.ext").ToString(); const string fileText = "test"; using (File.CreateText(filename)) { } try { File.WriteAllText(filename, fileText, Encoding.Unicode); Assert.AreEqual(fileText, File.ReadAllText(filename, Encoding.Unicode)); } finally { File.Delete(filename); } }
public void TestSetLastAccessTime() { var filename = Util.CreateNewFile(uncDirectory); try { DateTime dateTime = DateTime.Now.AddDays(1); var fi = new FileInfo(filename) { LastAccessTime = dateTime }; Assert.AreEqual(dateTime, File.GetLastAccessTime(filename)); } finally { File.Delete(filename); } }
public void TestCreate() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString(); try { using (var s = File.Create(tempLongPathFilename)) { s.WriteByte(42); s.Seek(0, SeekOrigin.Begin); Assert.AreEqual(42, s.ReadByte()); } } finally { File.Delete(tempLongPathFilename); } }
public void TestSetCreationTimeUtc() { var filename = Util.CreateNewFile(uncDirectory); try { DateTime dateTime = DateTime.UtcNow.AddDays(1); var fi = new FileInfo(filename) { CreationTimeUtc = dateTime }; Assert.AreEqual(dateTime, File.GetCreationTimeUtc(filename)); } finally { File.Delete(filename); } }
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(File.Exists(destLongPathFilename)); fi.CopyTo(destLongPathFilename, true); Assert.AreEqual(File.ReadAllText(filePath), File.ReadAllText(destLongPathFilename)); } finally { File.Delete(destLongPathFilename); } }
public void TestAppendText() { string filename = Util.CreateNewFile(longPathDirectory); try { using (var sw = File.AppendText(filename)) { sw.WriteLine("end of file"); } var lines = File.ReadLines(filename); Assert.IsTrue(new[] { "beginning of file", "end of file" }.SequenceEqual(lines)); } finally { File.Delete(filename); } }
public void TestLengthWithBadPath() { var filename = Util.CreateNewFile(longPathDirectory); Pri.LongPath.FileInfo fi; try { fi = new FileInfo(filename); } catch { File.Delete(filename); throw; } fi.Delete(); fi.Refresh(); var l = fi.Length; }
// clean all files and directories in iPath, then delete iPath dir... public static void DeleteDirectory(string iPath, string[] ignoreFiles = null) { if (iPath == null) { return; } if (Application.platform == RuntimePlatform.WindowsEditor) { iPath = iPath.Replace('/', '\\'); } if (!Directory.Exists(iPath)) { return; } // delete subDir string[] dirPaths = Directory.GetDirectories(iPath); for (int i = 0; i < dirPaths.Length; i++) { DeleteDirectory(dirPaths[i], ignoreFiles); } // delete files string[] filePaths = Directory.GetFiles(iPath); for (int i = 0; i < filePaths.Length; ++i) { string extension = Path.GetExtension(filePaths[i]); if (ignoreFiles != null && Array.IndexOf(ignoreFiles, extension) >= 0) { continue; } if (filePaths[i] != null) { File.Delete(filePaths[i]); } } // delete iPath dir... if (Directory.GetDirectories(iPath).Length == 0 && Directory.GetFiles(iPath).Length == 0) { Directory.Delete(iPath); } }
public void TestOpenWithAccess() { var tempLongPathFilename = new StringBuilder(longPathDirectory).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 TestReadAllTextWithEncoding() { var tempLongPathFilename = new StringBuilder(longPathDirectory).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 TestMove() { string sourceFilename = Util.CreateNewFile(longPathDirectory); string destFilename = Path.Combine(longPathDirectory, 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 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 { File.Delete(tempLongPathFilename); } }
public void TestOpenRead() { var tempLongPathFilename = new StringBuilder(longPathDirectory).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 TestEncrypt() { 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)); File.Encrypt(tempLongPathFilename); var postAttrib = File.GetAttributes(tempLongPathFilename); Assert.AreEqual(FileAttributes.Encrypted, (postAttrib & FileAttributes.Encrypted)); } finally { File.Delete(tempLongPathFilename); } }
public void TestOpenExisting() { var tempLongPathFilename = new StringBuilder(longPathDirectory).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); } }
static void CopyFiles(string source, string dest, bool move, bool includeArchives = true) { if (move) { MarkFolderWritable(source); } MarkFolderWritable(dest); Console.WriteLine("{0} {1} => {2}", move ? "Moving" : "Copying", source, dest); source = source.TrimEnd('\\'); dest = dest.TrimEnd('\\'); if (!Directory.Exists(dest)) { Directory.CreateDirectory(dest); } Directory.EnumerateDirectories(source, "*", SearchOption.AllDirectories).Select(d => d.Replace(source, dest)).ForEach(path => Directory.CreateDirectory(path)); foreach (var file in Directory.EnumerateFiles(source, "*", SearchOption.AllDirectories).Where(f => Path.GetExtension(f) != ".nfo" && !Regex.IsMatch(Path.GetFileName(f), "All.Collection.Upload|WareZ-Audio", RegexOptions.IgnoreCase)).ToArray()) { if (Path.GetExtension(file) == ".sfv") { continue; } //if (!includeArchives && Regex.IsMatch(Path.GetExtension(file), @"\.(rar|r\d+|zip|iso)")) if (!includeArchives && Path.GetDirectoryName(file) == source && Regex.IsMatch(Path.GetExtension(file), @"\.(rar|r\d+|zip)")) { continue; } var newFile = file.Replace(source, dest); if (move) { if (File.Exists(newFile)) { File.Delete(newFile); } File.Move(file, newFile); } else { File.Copy(file, newFile, true); } } }
public void TestLastWriteTime() { var filename = Util.CreateNewFile(longPathDirectory); try { var dateTime = DateTime.Now.AddDays(1); { var fiTemp = new FileInfo(filename) { LastWriteTime = dateTime }; } var fi = new FileInfo(filename); Assert.AreEqual(dateTime, fi.LastWriteTime); } finally { File.Delete(filename); } }
public void TestOpenWriteWritesCorrectly() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file31a.ext").ToString(); var fi = new FileInfo(tempLongPathFilename); try { using (var fileStream = fi.OpenWrite()) { fileStream.WriteByte(42); } using (var fileStream = fi.OpenRead()) { Assert.AreEqual(42, fileStream.ReadByte()); } } finally { File.Delete(tempLongPathFilename); } }
public void TestReadAllBytesOnHugeFile() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString(); using (var fileStream = File.Create(tempLongPathFilename)) { fileStream.Seek(((long)int.MaxValue) + 1, SeekOrigin.Begin); fileStream.WriteByte(42); } Assert.Throws <IOException>(() => { try { Assert.IsTrue(new byte[] { 42 }.SequenceEqual(File.ReadAllBytes(tempLongPathFilename))); } finally { File.Delete(tempLongPathFilename); } }); }
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 (Directory.Exists(longPathRoot)) { Directory.Delete(longPathRoot, true); } } }
public void TestOpenCreatesEmpty() { var tempLongPathFilename = Path.Combine(longPathDirectory, Path.GetRandomFileName()); try { using (var writer = File.CreateText(tempLongPathFilename)) { writer.WriteLine("test"); } var fi = new FileInfo(tempLongPathFilename); using (var fileStream = fi.Open(FileMode.Append, FileAccess.Read, FileShare.Read)) { Assert.AreEqual(-1, fileStream.ReadByte()); } } finally { File.Delete(tempLongPathFilename); } }
public void TestOpenHidden() { var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file25.ext").ToString(); var fi = new FileInfo(tempLongPathFilename); using (fi.Create()) { } try { File.SetAttributes(fi.FullName, File.GetAttributes(fi.FullName) | FileAttributes.Hidden); using (var fileStream = fi.Open(FileMode.Create)) { Assert.IsNotNull(fileStream); } } finally { File.Delete(tempLongPathFilename); } }