private void DumpClassByHandleFileInfo(bool isLocal) { Console.WriteLine("\n=== TEST {0} ===", isLocal ? UnitTestConstants.Local : UnitTestConstants.Network); var tempPath = Path.GetTempPath("File.GetFileInfoByHandle()-" + Path.GetRandomFileName()); if (!isLocal) { tempPath = Path.LocalToUnc(tempPath); } Console.WriteLine("\nInput File Path: [{0}]", tempPath); var stream = File.Create(tempPath); stream.WriteByte(1); UnitTestConstants.StopWatcher(true); var bhfi = File.GetFileInfoByHandle(stream.SafeFileHandle); Console.WriteLine(UnitTestConstants.Reporter()); Assert.IsTrue(UnitTestConstants.Dump(bhfi, -18)); Assert.AreEqual(System.IO.File.GetCreationTimeUtc(tempPath), bhfi.CreationTimeUtc); Assert.AreEqual(System.IO.File.GetLastAccessTimeUtc(tempPath), bhfi.LastAccessTimeUtc); Assert.AreEqual(System.IO.File.GetLastWriteTimeUtc(tempPath), bhfi.LastWriteTimeUtc); stream.Close(); File.Delete(tempPath, true); Assert.IsFalse(File.Exists(tempPath), "Cleanup failed: File should have been removed."); Console.WriteLine(); }
private static void WriteHashCache(string file, string hash) { using var fs = File.Create(file + Consts.HashFileExtension); using var bw = new BinaryWriter(fs); bw.Write(HashCacheVersion); var lastModified = File.GetLastWriteTimeUtc(file).AsUnixTime(); bw.Write(lastModified); bw.Write(BitConverter.ToUInt64(hash.FromBase64())); }
public void GetFilesWithExtension_FileContainingPatternUseToReturn() { const string name1 = "G-SN750B_02_S13UJ1NQ221583.cde"; const string name2 = "G-SN750B_02_S13UJ1NQ221583.cde-backup-with-hash"; var f1 = File.Create(name1); var f2 = File.Create(name2); f1.Close(); f2.Close(); var files = AlphaFSHelper.GetFilesWithExtension(".", "cde"); foreach (var file in files) { Console.WriteLine($"file {file}"); } //System.Threading.Thread.Sleep(1000); // delay 1 second File.Delete(name1); File.Delete(name2); Assert.That(files.Count(), Is.EqualTo(1), "Oops somehow we got a file not ending in \"cde\" in our result set."); }
public FileStream Create() { return(File.Create(_path)); }
public void WriteAllText(string text) { using var fs = File.Create(_path); fs.Write(Encoding.UTF8.GetBytes(text)); }
public async Task WriteAllTextAsync(string text) { await using var fs = File.Create(_path); await fs.WriteAsync(Encoding.UTF8.GetBytes(text)); }
// Pattern: <class>_<function>_<scenario>_<expected result> #region Unit Tests private void Dump83Path(bool isLocal) { #region Setup Console.WriteLine("\n=== TEST {0} ===", isLocal ? UnitTestConstants.Local : UnitTestConstants.Network); var myLongPath = Path.GetTempPath("My Long Data File Or Directory"); if (!isLocal) { myLongPath = Path.LocalToUnc(myLongPath); } Console.WriteLine("\nInput Path: [{0}]\n", myLongPath); #endregion // Setup #region File string short83Path; try { using (File.Create(myLongPath)) UnitTestConstants.StopWatcher(true); short83Path = Path.GetShort83Path(myLongPath); Console.WriteLine("Short 8.3 file path : [{0}]\t\t\t{1}", short83Path, UnitTestConstants.Reporter(true)); Assert.IsTrue(!short83Path.Equals(myLongPath)); Assert.IsTrue(short83Path.EndsWith(@"~1")); UnitTestConstants.StopWatcher(true); var longFrom83Path = Path.GetLongFrom83ShortPath(short83Path); Console.WriteLine("Long path from 8.3 path: [{0}]{1}", longFrom83Path, UnitTestConstants.Reporter(true)); Assert.IsTrue(longFrom83Path.Equals(myLongPath)); Assert.IsFalse(longFrom83Path.EndsWith(@"~1")); } catch (Exception ex) { Console.WriteLine("Caught (unexpected) {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, " ")); } finally { if (File.Exists(myLongPath)) { File.Delete(myLongPath); } } Console.WriteLine(); #endregion // File #region Directory try { Directory.CreateDirectory(myLongPath); UnitTestConstants.StopWatcher(true); short83Path = Path.GetShort83Path(myLongPath); Console.WriteLine("Short 8.3 directory path: [{0}]\t\t\t{1}", short83Path, UnitTestConstants.Reporter(true)); Assert.IsFalse(short83Path.Equals(myLongPath)); Assert.IsTrue(short83Path.EndsWith(@"~1")); UnitTestConstants.StopWatcher(true); var longFrom83Path = Path.GetLongFrom83ShortPath(short83Path); Console.WriteLine("Long path from 8.3 path : [{0}]{1}", longFrom83Path, UnitTestConstants.Reporter(true)); Assert.IsTrue(longFrom83Path.Equals(myLongPath)); Assert.IsFalse(longFrom83Path.EndsWith(@"~1")); } catch (Exception ex) { Console.WriteLine("Caught (unexpected) {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, " ")); } finally { if (Directory.Exists(myLongPath)) { Directory.Delete(myLongPath); } } Console.WriteLine(); #endregion // Directory }
private void DumpGetFinalPathNameByHandle(bool isLocal) { Console.WriteLine("\n=== TEST {0} ===", isLocal ? UnitTestConstants.Local : UnitTestConstants.Network); var tempFile = Path.GetTempFileName(); if (!isLocal) { tempFile = Path.LocalToUnc(tempFile); } var longTempStream = Path.LongPathPrefix + tempFile; bool gotFileNameNormalized; bool gotFileNameOpened; bool gotVolumeNameDos; bool gotVolumeNameGuid; bool gotVolumeNameNt; bool gotVolumeNameNone; bool gotSomething; using (var stream = File.Create(tempFile)) { // For Windows versions < Vista, the file must be > 0 bytes. if (!NativeMethods.IsAtLeastWindowsVista) { stream.WriteByte(1); } var handle = stream.SafeFileHandle; UnitTestConstants.StopWatcher(true); var fileNameNormalized = Path.GetFinalPathNameByHandle(handle); var fileNameOpened = Path.GetFinalPathNameByHandle(handle, FinalPathFormats.FileNameOpened); var volumeNameDos = Path.GetFinalPathNameByHandle(handle, FinalPathFormats.None); var volumeNameGuid = Path.GetFinalPathNameByHandle(handle, FinalPathFormats.VolumeNameGuid); var volumeNameNt = Path.GetFinalPathNameByHandle(handle, FinalPathFormats.VolumeNameNT); var volumeNameNone = Path.GetFinalPathNameByHandle(handle, FinalPathFormats.VolumeNameNone); // These three output the same. gotFileNameNormalized = !string.IsNullOrWhiteSpace(fileNameNormalized) && longTempStream.Equals(fileNameNormalized); gotFileNameOpened = !string.IsNullOrWhiteSpace(fileNameOpened) && longTempStream.Equals(fileNameOpened); gotVolumeNameDos = !string.IsNullOrWhiteSpace(volumeNameDos) && longTempStream.Equals(volumeNameDos); gotVolumeNameGuid = !string.IsNullOrWhiteSpace(volumeNameGuid) && volumeNameGuid.StartsWith(Path.VolumePrefix) && volumeNameGuid.EndsWith(volumeNameNone); gotVolumeNameNt = !string.IsNullOrWhiteSpace(volumeNameNt) && volumeNameNt.StartsWith(Path.DevicePrefix); gotVolumeNameNone = !string.IsNullOrWhiteSpace(volumeNameNone) && tempFile.EndsWith(volumeNameNone); Console.WriteLine("\nInput Path: [{0}]", tempFile); Console.WriteLine("\n\tFilestream.Name : [{0}]", stream.Name); Console.WriteLine("\tFilestream.Length: [{0}] (Note: For Windows versions < Vista, the file must be > 0 bytes.)\n", Utils.UnitSizeToText(stream.Length)); Console.WriteLine("\tFinalPathFormats.None : [{0}]", fileNameNormalized); Console.WriteLine("\tFinalPathFormats.FileNameOpened: [{0}]", fileNameOpened); Console.WriteLine("\tFinalPathFormats.VolumeNameDos : [{0}]", volumeNameDos); Console.WriteLine("\tFinalPathFormats.VolumeNameGuid: [{0}]", volumeNameGuid); Console.WriteLine("\tFinalPathFormats.VolumeNameNT : [{0}]", volumeNameNt); Console.WriteLine("\tFinalPathFormats.VolumeNameNone: [{0}]", volumeNameNone); Console.WriteLine("\n{0}", UnitTestConstants.Reporter(true)); gotSomething = true; } var fileExists = File.Exists(tempFile); File.Delete(tempFile, true); Assert.IsFalse(File.Exists(tempFile), "Cleanup failed: File should have been removed."); Assert.IsTrue(fileExists); Assert.IsTrue(gotFileNameNormalized); Assert.IsTrue(gotFileNameOpened); Assert.IsTrue(gotVolumeNameDos); Assert.IsTrue(gotVolumeNameGuid); Assert.IsTrue(gotVolumeNameNt); Assert.IsTrue(gotVolumeNameNone); Assert.IsTrue(gotSomething); // AlphaFS implementation of fileStream.Name returns = "[Unknown]" // System.IO returns the full path. Console.WriteLine(); var fileName = Path.Combine(Environment.ExpandEnvironmentVariables("%temp%") + "foo.bar"); var fileStream2 = System.IO.File.Create(fileName); Assert.AreEqual(fileStream2.Name, fileName); fileStream2.Close(); File.Delete(fileName); var fileStream = File.Create(fileName); var fileStreamName = Alphaleonis.Win32.Filesystem.Path.GetFinalPathNameByHandle(fileStream.SafeFileHandle); Assert.AreNotEqual(fileName, fileStream.Name); Assert.AreEqual(fileName, Path.GetRegularPath(fileStreamName)); fileStream.Close(); File.Delete(fileName); }