protected void DeleteOrRecycleFile(FileInfo file) { if (file == null) { return; } if (_tidyup.DeleteEmptyIsRecycle) { Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(file.FullName, Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin); } else { file.Delete(true); } }
private void TransferItem(USNJournalSyncLog syncLog, SyncMountpoint syncFrom) { try { syncLog.ActionStartDate = DateTime.Now; syncLog.ActionFinishDate = null; Singleton.Instance.Repository.Update(syncLog); bool successfull = false; if (syncLog.Action.GetType() == typeof(DeleteAction)) { var path = Path.Get(syncFrom.Path, syncLog.Action.RelativePath); logger.Info($"[{syncLog.Id}] [D] " + path); if (path.Exists) { var destinationPath = Path.Get(syncFrom.Path, ".proximaTemp.", "toDelete", syncLog.Id).FullPath; if (syncLog.Action.IsDirectory) { DirectoryInfo info = new DirectoryInfo(path.FullPath); info.MoveTo(destinationPath); info.Delete(true, true); } else { FileInfo info = new FileInfo(path.FullPath); info.MoveTo(destinationPath); info.Delete(); } } successfull = true; } else if (syncLog.Action.GetType() == typeof(RenameAction)) { var renameAction = syncLog.Action as RenameAction; logger.Info($"[{syncLog.Id}] [R] {renameAction.RenameFrom} to {renameAction.RelativePath}"); if (String.IsNullOrWhiteSpace(renameAction.RenameFrom)) { CopyFile(syncLog, syncFrom); } string pathFrom = Path.Get(syncFrom.Path, renameAction.RenameFrom).FullPath; string tempPath = Path.Get(syncFrom.Path, ".proximaTemp.", "toRename", syncLog.Id).FullPath; string pathTo = Path.Get(syncFrom.Path, renameAction.RelativePath).FullPath; if (syncLog.Action.IsDirectory) { new DirectoryInfo(pathFrom).MoveTo(tempPath, MoveOptions.None); new DirectoryInfo(tempPath).MoveTo(pathTo, MoveOptions.None); } else { Alphaleonis.Win32.Filesystem.File.Move(pathFrom, tempPath, MoveOptions.None); Alphaleonis.Win32.Filesystem.File.Move(tempPath, pathTo, MoveOptions.None); //new FileInfo(pathFrom).MoveTo(tempPath, MoveOptions.WriteThrough); //new FileInfo(tempPath).MoveTo(pathTo, MoveOptions.WriteThrough); } successfull = true; } else { successfull = CopyFile(syncLog, syncFrom); } syncLog.ActionFinishDate = DateTime.Now; syncLog.Successfull = successfull; Singleton.Instance.Repository.Update(syncLog); foreach (var error in Singleton.Instance.Repository.Many <Error>(f => f.SyncLog.Id == syncLog.Id)) { Singleton.Instance.Repository.Delete(error); } } catch (FileNotFoundException ex) { syncLog.RequiresManualIntervention = true; syncLog.ActionFinishDate = DateTime.Now; Singleton.Instance.Repository.Update(syncLog); logger.Error(ex, "Error on item " + syncLog.Id); Error error = new Error(); error.SyncLog = syncLog; error.Exception = ex; error.ItemId = syncLog.Id; error.Message = ex.Message; Singleton.Instance.Repository.Add(error); } catch (Exception e) { syncLog.ActionFinishDate = DateTime.Now; Singleton.Instance.Repository.Update(syncLog); logger.Error(e, "Error on item " + syncLog.Id); Error error = new Error(); error.SyncLog = syncLog; error.Exception = e; error.ItemId = syncLog.Id; error.Message = e.Message; Singleton.Instance.Repository.Add(error); } }
public void SetAccessControl() { Console.WriteLine("File.SetAccessControl()"); if (!IsAdmin()) Assert.Fail(); string path = SysDrive + @"\AlphaFile-" + Path.GetRandomFileName(); string pathAlpha = path; Console.WriteLine("\n\tFile: [{0}]", path); try { using (File.Create(pathAlpha)) { } // Initial read. Console.WriteLine("\n\tInitial read."); FileSecurity dsSystem = System.IO.File.GetAccessControl(path, AccessControlSections.Access); FileSecurity dsAlpha = File.GetAccessControl(pathAlpha, AccessControlSections.Access); AuthorizationRuleCollection accessRulesSystem = dsSystem.GetAccessRules(true, true, typeof(NTAccount)); AuthorizationRuleCollection accessRulesAlpha = dsAlpha.GetAccessRules(true, true, typeof(NTAccount)); Console.WriteLine("\t System.IO.File.GetAccessControl() rules found: [{0}]", accessRulesSystem.Count); Console.WriteLine("\t\t\t File.GetAccessControl() rules found: [{0}]", accessRulesAlpha.Count); Assert.AreEqual(accessRulesSystem.Count, accessRulesAlpha.Count); // Sanity check. DumpAccessRules(1, dsSystem, dsAlpha); // Remove inherited properties. // Passing true for first parameter protects the new permission from inheritance, and second parameter removes the existing inherited permissions Console.WriteLine("\n\tRemove inherited properties and persist it."); dsAlpha.SetAccessRuleProtection(true, false); File.SetAccessControl(pathAlpha, dsAlpha, AccessControlSections.Access); // Re-read, using instance methods. System.IO.FileInfo fiSystem = new System.IO.FileInfo(Path.LocalToUnc(path)); FileInfo fiAlpha = new FileInfo(Path.LocalToUnc(path)); dsSystem = fiSystem.GetAccessControl(AccessControlSections.Access); dsAlpha = fiAlpha.GetAccessControl(AccessControlSections.Access); // Sanity check. DumpAccessRules(2, dsSystem, dsAlpha); // Restore inherited properties. Console.WriteLine("\n\tRestore inherited properties and persist it."); dsAlpha.SetAccessRuleProtection(false, true); File.SetAccessControl(pathAlpha, dsAlpha, AccessControlSections.Access); // Re-read. dsSystem = System.IO.File.GetAccessControl(path, AccessControlSections.Access); dsAlpha = File.GetAccessControl(pathAlpha, AccessControlSections.Access); // Sanity check. DumpAccessRules(3, dsSystem, dsAlpha); fiSystem.Delete(); fiSystem.Refresh(); // Must Refresh() to get actual state. fiAlpha.Delete(); fiAlpha.Refresh(); // Must Refresh() to get actual state. Assert.IsFalse(fiAlpha.Exists); } catch (Exception ex) { Console.WriteLine("\nCaught Exception: [{0}]\n", ex.Message.Replace(Environment.NewLine, " ")); } }
private void DumpFileTrailingDotSpace(bool isLocal) { Console.WriteLine("\n=== TEST {0} ===\n", isLocal ? Local : Network); const string characterDot = "."; const string characterSpace = " "; string random = Path.GetRandomFileName(); string tempPathDot = Path.GetTempPath("File.Create()-" + random + "-file-with-dot-" + characterDot); string tempPathSpace = Path.GetTempPath("File.Create()-" + random + "-file-with-space-" + characterSpace); if (!isLocal) tempPathDot = Path.LocalToUnc(tempPathDot); if (!isLocal) tempPathSpace = Path.LocalToUnc(tempPathSpace); Console.WriteLine("Input File Path (with dot) : [{0}]", tempPathDot); Console.WriteLine("Input File Path (with space): [{0}]", tempPathSpace); Assert.IsTrue(tempPathDot.EndsWith(characterDot), "Path should have a trailing dot."); Assert.IsTrue(tempPathSpace.EndsWith(characterSpace), "Path should have a trailing space."); #region Path.GetFullPath(), Path Normalization string sysIo = System.IO.Path.GetFullPath(tempPathDot); Assert.IsFalse(sysIo.EndsWith(characterDot), "Path should not have a trailing dot."); sysIo = System.IO.Path.GetFullPath(tempPathSpace); Assert.IsFalse(sysIo.EndsWith(characterSpace), "Path should not have a trailing space."); string alphaFs = Path.GetFullPath(tempPathDot); Assert.IsFalse(alphaFs.EndsWith(characterDot), "Path should not have a trailing dot."); alphaFs = Path.GetFullPath(tempPathSpace); Assert.IsFalse(alphaFs.EndsWith(characterSpace), "Path should not have a trailing space."); Assert.AreEqual(sysIo, alphaFs, "Paths should be the same."); #endregion // Path.GetFullPath(), Path Normalization #region Path.GetLongPath(), No Path Normalization alphaFs = Path.GetLongPath(tempPathDot); Assert.IsTrue(alphaFs.EndsWith(characterDot), "Path should have a trailing dot."); alphaFs = Path.GetLongPath(tempPathSpace); Assert.IsTrue(alphaFs.EndsWith(characterSpace), "Path should have a trailing space."); Assert.AreNotEqual(alphaFs, sysIo, "Paths should not be the same."); #endregion // Path.GetLongPath(), No Path Normalization #region Path.GetRegularPath(), No Path Normalization alphaFs = Path.GetRegularPath(tempPathDot); Assert.IsTrue(alphaFs.EndsWith(characterDot), "Path should have a trailing dot."); alphaFs = Path.GetRegularPath(tempPathSpace); Assert.IsTrue(alphaFs.EndsWith(characterSpace), "Path should have a trailing space."); Assert.AreNotEqual(alphaFs, sysIo, "Paths should not be the same."); #endregion // Path.GetRegularPath(), No Path Normalization Console.WriteLine(); #region File() Class StopWatcher(true); #region TrailingDot #region System.IO // tempPathDot contains a trailing dot but gets stripped on path normalization. // System.IO handles the file without the trailing dot. Therefore, the file exists. // AlphaFS has the same behaviour as .NET for default methods. using (FileStream fs = System.IO.File.Create(tempPathDot)) { fs.WriteByte(1); } Assert.IsTrue(System.IO.File.Exists(tempPathDot), "File should exist."); Assert.IsTrue(File.Exists(tempPathDot), "File should be visible to AlphaFS."); Assert.IsFalse(File.Exists(tempPathDot, PathFormat.FullPath), "File should be invisible to AlphaFS."); using (StreamWriter sw = System.IO.File.AppendText(tempPathDot)) sw.WriteLine(TextHelloWorld); string lineSysIo; using (StreamReader sr = System.IO.File.OpenText(tempPathDot)) lineSysIo = sr.ReadToEnd(); System.IO.File.Delete(tempPathDot); Assert.IsFalse(System.IO.File.Exists(tempPathDot), "File should not exist."); #endregion // System.IO #region AlphaFS using (FileStream fs = File.Create(tempPathDot, PathFormat.FullPath)) { fs.WriteByte(1); } // Create file without path normalization. Assert.IsTrue(File.Exists(tempPathDot, PathFormat.FullPath), "File should exist and visible to AlphaFS."); Assert.IsFalse(System.IO.File.Exists(tempPathDot), "File should be invisible to System.IO."); using (StreamWriter sw = File.AppendText(tempPathDot, PathFormat.FullPath)) sw.WriteLine(TextHelloWorld); string lineAlphaFs; using (StreamReader sr = File.OpenText(tempPathDot, PathFormat.FullPath)) lineAlphaFs = sr.ReadToEnd(); File.Delete(tempPathDot, true, PathFormat.FullPath); // Delete file without path normalization. Assert.IsFalse(File.Exists(tempPathDot, PathFormat.FullPath), "File should not exist."); #endregion // AlphaFS Assert.AreEqual(lineSysIo, lineAlphaFs); #endregion // TrailingDot #region TrailingSpace #region System.IO // tempPathSpace contains a trailing space but gets stripped on path normalization. // System.IO handles the file without the trailing space. Therefore, the file exists. // AlphaFS has the same behaviour as .NET for default methods. using (FileStream fs = System.IO.File.Create(tempPathSpace)) { fs.WriteByte(1); } Assert.IsTrue(System.IO.File.Exists(tempPathSpace), "File should exist."); Assert.IsTrue(File.Exists(tempPathSpace), "File should be visible to AlphaFS."); Assert.IsFalse(File.Exists(tempPathSpace, PathFormat.FullPath), "File should be invisible to AlphaFS."); using (StreamWriter sw = System.IO.File.AppendText(tempPathSpace)) sw.WriteLine(TextHelloWorld); using (StreamReader sr = System.IO.File.OpenText(tempPathSpace)) lineSysIo = sr.ReadToEnd(); System.IO.File.Delete(tempPathSpace); Assert.IsFalse(System.IO.File.Exists(tempPathSpace), "File should not exist."); #endregion // System.IO #region AlphaFS using (FileStream fs = File.Create(tempPathSpace, PathFormat.FullPath)) { fs.WriteByte(1); } // Create file without path normalization. Assert.IsTrue(File.Exists(tempPathSpace, PathFormat.FullPath), "File should exist and visible to AlphaFS."); Assert.IsFalse(System.IO.File.Exists(tempPathSpace), "File should be invisible to System.IO."); using (StreamWriter sw = File.AppendText(tempPathSpace, PathFormat.FullPath)) sw.WriteLine(TextHelloWorld); using (StreamReader sr = File.OpenText(tempPathSpace, PathFormat.FullPath)) lineAlphaFs = sr.ReadToEnd(); File.Delete(tempPathSpace, true, PathFormat.FullPath); // Delete file without path normalization. Assert.IsFalse(File.Exists(tempPathSpace, PathFormat.FullPath), "File should not exist."); #endregion // AlphaFS Assert.AreEqual(lineSysIo, lineAlphaFs); #endregion // TrailingSpace Console.WriteLine("\tClass File(){0}", Reporter()); #endregion // File() Class #region FileInfo() Class StopWatcher(true); #region TrailingDot #region System.IO // tempPathDot contains a trailing dot but gets stripped on path normalization. // System.IO handles the file without the trailing dot. Therefore, the file exists. // AlphaFS has the same behaviour as .NET for default methods. System.IO.FileInfo sysIoFi = new System.IO.FileInfo(tempPathDot); Assert.IsTrue(sysIoFi.Name.EndsWith(characterDot), "Path should have a trailing dot."); using (FileStream fs = sysIoFi.Create()) { fs.WriteByte(100); Assert.IsTrue(sysIoFi.Exists, "File should exist."); Assert.IsTrue(File.Exists(tempPathDot), "File should be visible to AlphaFS."); Assert.IsFalse(File.Exists(tempPathDot, PathFormat.FullPath), "File should be invisible to AlphaFS."); } using (StreamWriter sw = sysIoFi.AppendText()) sw.WriteLine(TextHelloWorld); using (StreamReader sr = System.IO.File.OpenText(tempPathDot)) lineSysIo = sr.ReadToEnd(); sysIoFi.Delete(); Assert.IsFalse(System.IO.File.Exists(tempPathDot), "File should not exist."); #endregion // System.IO #region AlphaFS FileInfo alphaFsFi = new FileInfo(tempPathDot, PathFormat.FullPath); Assert.IsTrue(alphaFsFi.Name.EndsWith(characterDot), "Path should have a trailing dot."); using (FileStream fs = alphaFsFi.Create()) { fs.WriteByte(100); Assert.IsTrue(alphaFsFi.Exists, "File should exist."); Assert.IsTrue(File.Exists(tempPathDot, PathFormat.FullPath), "File should be visible to AlphaFS."); Assert.IsFalse(File.Exists(tempPathDot), "File should be invisible to AlphaFS."); } using (StreamWriter sw = alphaFsFi.AppendText()) sw.WriteLine(TextHelloWorld); using (StreamReader sr = File.OpenText(tempPathDot, PathFormat.FullPath)) lineAlphaFs = sr.ReadToEnd(); alphaFsFi.Delete(); alphaFsFi.Refresh(); // Must Refresh() to get actual state. Assert.IsFalse(File.Exists(tempPathDot, PathFormat.FullPath), "File should not exist."); #endregion // AlphaFS Assert.AreEqual(lineSysIo, lineAlphaFs); #endregion // TrailingDot #region TrailingSpace #region System.IO // tempPathSpace contains a trailing space but gets stripped on path normalization. // System.IO handles the file without the trailing space. Therefore, the file exists. // AlphaFS has the same behaviour as .NET for default methods. sysIoFi = new System.IO.FileInfo(tempPathSpace); Assert.IsTrue(sysIoFi.Name.EndsWith(characterSpace), "Path should have a trailing space."); using (FileStream fs = sysIoFi.Create()) { fs.WriteByte(100); Assert.IsTrue(sysIoFi.Exists, "File should exist."); Assert.IsTrue(File.Exists(tempPathSpace), "File should be visible to AlphaFS."); Assert.IsFalse(File.Exists(tempPathSpace, PathFormat.FullPath), "File should be invisible to AlphaFS."); } using (StreamWriter sw = sysIoFi.AppendText()) sw.WriteLine(TextHelloWorld); using (StreamReader sr = System.IO.File.OpenText(tempPathSpace)) lineSysIo = sr.ReadToEnd(); sysIoFi.Delete(); Assert.IsFalse(System.IO.File.Exists(tempPathSpace), "File should not exist."); #endregion // System.IO #region AlphaFS alphaFsFi = new FileInfo(tempPathSpace, PathFormat.FullPath); Assert.IsTrue(alphaFsFi.Name.EndsWith(characterSpace), "Path should have a trailing space."); using (FileStream fs = alphaFsFi.Create()) { fs.WriteByte(100); Assert.IsTrue(alphaFsFi.Exists, "File should exist."); Assert.IsTrue(File.Exists(tempPathSpace, PathFormat.FullPath), "File should be visible to AlphaFS."); Assert.IsFalse(File.Exists(tempPathSpace), "File should be invisible to AlphaFS."); } using (StreamWriter sw = alphaFsFi.AppendText()) sw.WriteLine(TextHelloWorld); using (StreamReader sr = File.OpenText(tempPathSpace, PathFormat.FullPath)) lineAlphaFs = sr.ReadToEnd(); alphaFsFi.Delete(); alphaFsFi.Refresh(); // Must Refresh() to get actual state. Assert.IsFalse(File.Exists(tempPathSpace, PathFormat.FullPath), "File should not exist."); #endregion // AlphaFS Assert.AreEqual(lineSysIo, lineAlphaFs); #endregion // TrailingSpace Console.WriteLine("\tClass FileInfo(){0}", Reporter()); #endregion // FileInfo() Class Console.WriteLine(); }
private void DumpGetSize(bool isLocal) { #region Setup Console.WriteLine("\n=== TEST {0} ===", isLocal ? Local : Network); string tempPath = Path.GetTempPath("File-GetSize()-file-" + Path.GetRandomFileName()); if (!isLocal) tempPath = Path.LocalToUnc(tempPath); Console.WriteLine("\nInput File Path: [{0}]\n", tempPath); int randomLines = new Random().Next(1000, 100000); // Create file with contents. FileInfo fi = new FileInfo(tempPath); using (StreamWriter sw = fi.CreateText()) for (int i = 0; i < randomLines; i++) sw.WriteLine(TextHelloWorld); long fileGetSize = File.GetSize(tempPath); long fileGetCompressedSize = File.GetCompressedSize(tempPath); long fiLength = fi.Length; Console.WriteLine("\tFile.GetSize()\t\t\t== [{0}] [{1} bytes]", Utils.UnitSizeToText(fileGetSize), fileGetSize); Console.WriteLine("\tFile.GetCompressedSize()\t== [{0}] [{1} bytes]", Utils.UnitSizeToText(fileGetCompressedSize), fileGetCompressedSize); Console.WriteLine("\tFileInfo().Length\t\t== [{0}] [{1} bytes]", Utils.UnitSizeToText(fiLength), fiLength); Console.WriteLine("\tFileInfo().Attributes\t\t== [{0}]", fi.Attributes); Assert.IsTrue((fi.Attributes & FileAttributes.Compressed) == 0, "File should be uncompressed."); Assert.IsTrue(fiLength == fileGetSize, "Uncompressed size should match."); Assert.IsTrue(fiLength == fileGetCompressedSize, "Uncompressed size should match."); #endregion // Setup #region Compress bool compressOk = false; string report; StopWatcher(true); try { fi.Compress(); report = Reporter(true); compressOk = true; fileGetSize = File.GetSize(tempPath); fileGetCompressedSize = File.GetCompressedSize(tempPath); } catch (Exception ex) { report = Reporter(true); Console.WriteLine("\n\tFile.Compress(): Caught unexpected Exception: [{0}]\n", ex.Message.Replace(Environment.NewLine, " ")); } // FileInfo() must Refresh(). Assert.IsTrue((fi.Attributes & FileAttributes.Compressed) == 0, "FileInfo() should not know it is compressed."); fi.Refresh(); fiLength = fi.Length; Assert.IsTrue((fi.Attributes & FileAttributes.Compressed) != 0, "FileInfo() should know it is compressed."); Console.WriteLine("\n\n\tFile.Compress() (Should be True): [{0}]{1}\n", compressOk, report); Console.WriteLine("\tFile.GetSize()\t\t\t== [{0}] [{1} bytes]", Utils.UnitSizeToText(fileGetSize), fileGetSize); Console.WriteLine("\tFile.GetCompressedSize()\t== [{0}] [{1} bytes]", Utils.UnitSizeToText(fileGetCompressedSize), fileGetCompressedSize); Console.WriteLine("\tFileInfo().Length\t\t== [{0}] [{1} bytes]", Utils.UnitSizeToText(fiLength), fiLength); Console.WriteLine("\tFileInfo().Attributes\t\t== [{0}]", fi.Attributes); Assert.IsTrue(compressOk); Assert.IsTrue((fi.Attributes & FileAttributes.Compressed) != 0, "File should be compressed."); Assert.IsTrue(fiLength != fileGetCompressedSize, "FileInfo() size should not match compressed size."); Assert.IsTrue(fiLength == fileGetSize, "File size should match FileInfo() size."); #endregion // Compress #region Decompress bool decompressOk = false; StopWatcher(true); try { File.Decompress(tempPath); report = Reporter(true); decompressOk = true; fileGetSize = File.GetSize(tempPath); fileGetCompressedSize = File.GetCompressedSize(tempPath); } catch (Exception ex) { report = Reporter(true); Console.WriteLine("\n\tFile.Decompress(): Caught unexpected Exception: [{0}]\n", ex.Message.Replace(Environment.NewLine, " ")); } // FileInfo() must Refresh(). Assert.IsTrue((fi.Attributes & FileAttributes.Compressed) != 0, "FileInfo() should not know it is compressed."); fi.Refresh(); fiLength = fi.Length; Assert.IsTrue((fi.Attributes & FileAttributes.Compressed) == 0, "FileInfo() should know it is compressed."); Console.WriteLine("\n\n\tFile.Decompress() (Should be True): [{0}]{1}\n", decompressOk, report); Console.WriteLine("\tFile.GetSize()\t\t\t== [{0}] [{1} bytes]", Utils.UnitSizeToText(fileGetSize), fileGetSize); Console.WriteLine("\tFile.GetCompressedSize()\t== [{0}] [{1} bytes]", Utils.UnitSizeToText(fileGetCompressedSize), fileGetCompressedSize); Console.WriteLine("\tFileInfo().Length\t\t== [{0}] [{1} bytes]", Utils.UnitSizeToText(fiLength), fiLength); Console.WriteLine("\tFileInfo().Attributes\t\t== [{0}]", fi.Attributes); Assert.IsTrue(decompressOk); Assert.IsTrue((fi.Attributes & FileAttributes.Compressed) == 0, "File should be uncompressed."); Assert.IsTrue(fiLength == fileGetSize, "Uncompressed size should match."); Assert.IsTrue(fiLength == fileGetCompressedSize, "Uncompressed size should match."); #endregion //Decompress fi.Delete(); fi.Refresh(); // Must Refresh() to get actual state. Assert.IsFalse(fi.Exists, "Cleanup failed: File should have been removed."); Console.WriteLine(); }
private void DumpGetXxxTimeXxx(bool isLocal) { #region Setup Console.WriteLine("\n=== TEST {0} ===", isLocal ? Local : Network); string path = NotepadExe; if (!isLocal) path = Path.LocalToUnc(path); Console.WriteLine("\nInput File Path: [{0}]\n", path); #endregion // Setup StopWatcher(true); #region GetCreationTimeXxx DateTime actual = File.GetCreationTime(path); DateTime expected = System.IO.File.GetCreationTime(path); Console.WriteLine("\tGetCreationTime() : [{0}] System.IO: [{1}]", actual, expected); Assert.AreEqual(expected, actual, "AlphaFS != System.IO"); actual = File.GetCreationTimeUtc(path); expected = System.IO.File.GetCreationTimeUtc(path); Console.WriteLine("\tGetCreationTimeUtc() : [{0}] System.IO: [{1}]\n", actual, expected); Assert.AreEqual(expected, actual, "AlphaFS != System.IO"); #endregion // GetCreationTimeXxx #region GetLastAccessTimeXxx actual = File.GetLastAccessTime(path); expected = System.IO.File.GetLastAccessTime(path); Console.WriteLine("\tGetLastAccessTime() : [{0}] System.IO: [{1}]", actual, expected); Assert.AreEqual(expected, actual, "AlphaFS != System.IO"); actual = File.GetLastAccessTimeUtc(path); expected = System.IO.File.GetLastAccessTimeUtc(path); Console.WriteLine("\tGetLastAccessTimeUtc(): [{0}] System.IO: [{1}]\n", actual, expected); Assert.AreEqual(expected, actual, "AlphaFS != System.IO"); #endregion // GetLastAccessTimeXxx #region GetLastWriteTimeXxx actual = File.GetLastWriteTime(path); expected = System.IO.File.GetLastWriteTime(path); Console.WriteLine("\tGetLastWriteTime() : [{0}] System.IO: [{1}]", actual, expected); Assert.AreEqual(expected, actual, "AlphaFS != System.IO"); actual = File.GetLastWriteTimeUtc(path); expected = System.IO.File.GetLastWriteTimeUtc(path); Console.WriteLine("\tGetLastWriteTimeUtc() : [{0}] System.IO: [{1}]\n", actual, expected); Assert.AreEqual(expected, actual, "AlphaFS != System.IO"); #endregion // GetLastWriteTimeXxx #region GetChangeTimeXxx Console.WriteLine("\tGetChangeTime() : [{0}] System.IO: [N/A]", File.GetChangeTime(path)); Console.WriteLine("\tGetChangeTimeUtc() : [{0}] System.IO: [N/A]", File.GetChangeTimeUtc(path)); #endregion // GetChangeTimeXxx Console.WriteLine(); Console.WriteLine(Reporter()); Console.WriteLine(); #region Trigger GetChangeTimeXxx // We can not compare ChangeTime against .NET because it does not exist. // Creating a file and renaming it triggers ChangeTime, so test for that. path = Path.GetTempPath("File-GetChangeTimeXxx()-file-" + Path.GetRandomFileName()); if (!isLocal) path = Path.LocalToUnc(path); FileInfo fi = new FileInfo(path); using (fi.Create()) { } string fileName = fi.Name; DateTime lastAccessTimeActual = File.GetLastAccessTime(path); DateTime lastAccessTimeUtcActual = File.GetLastAccessTimeUtc(path); DateTime changeTimeActual = File.GetChangeTime(path); DateTime changeTimeUtcActual = File.GetChangeTimeUtc(path); Console.WriteLine("\nTesting ChangeTime on a temp file."); Console.WriteLine("\nInput File Path: [{0}]\n", path); Console.WriteLine("\tGetChangeTime() : [{0}]\t", changeTimeActual); Console.WriteLine("\tGetChangeTimeUtc() : [{0}]\t", changeTimeUtcActual); fi.MoveTo(fi.FullName.Replace(fileName, fileName + "-Renamed")); // Pause for at least a second so that the difference in time can be seen. int sleep = new Random().Next(2000, 4000); Thread.Sleep(sleep); fi.MoveTo(fi.FullName.Replace(fileName + "-Renamed", fileName)); DateTime lastAccessTimeExpected = File.GetLastAccessTime(path); DateTime lastAccessTimeUtcExpected = File.GetLastAccessTimeUtc(path); DateTime changeTimeExpected = File.GetChangeTime(path); DateTime changeTimeUtcExpected = File.GetChangeTimeUtc(path); Console.WriteLine("\nTrigger ChangeTime by renaming the file."); Console.WriteLine("For Unit Test, ChangeTime should differ approximately: [{0}] seconds.\n", sleep / 1000); Console.WriteLine("\tGetChangeTime() : [{0}]\t", changeTimeExpected); Console.WriteLine("\tGetChangeTimeUtc() : [{0}]\t\n", changeTimeUtcExpected); Assert.AreNotEqual(changeTimeActual, changeTimeExpected); Assert.AreNotEqual(changeTimeUtcActual, changeTimeUtcExpected); Assert.AreEqual(lastAccessTimeExpected, lastAccessTimeActual); Assert.AreEqual(lastAccessTimeUtcExpected, lastAccessTimeUtcActual); #endregion // Trigger GetChangeTimeXxx fi.Delete(); fi.Refresh(); // Must Refresh() to get actual state. Assert.IsFalse(fi.Exists, "Cleanup failed: File should have been removed."); Console.WriteLine(); }