public void CopyTo(FilePath target) { if ((FileInfo.Attributes & FileAttributes.Encrypted) != 0) { FileInfo.Attributes &= ~FileAttributes.Encrypted; } if (target.FileInfo.IsReadOnly) { target.ClearReadOnlyAttribute(); } File.Copy(Path, target.Path, overwrite: true); if (FileInfo.IsReadOnly) //Has now been copied to target file { target.ClearReadOnlyAttribute(); //Required to allow modifying times } //Preserve file dates var sourceCreationUTC = File.GetCreationTimeUtc(Path); var targetCreationUTC = File.GetCreationTimeUtc(Path); if (targetCreationUTC != sourceCreationUTC) { File.SetCreationTimeUtc(target.Path, targetCreationUTC); } target.SetLastWriteTimeUtc(this); }
public void Demo() { //Creating paths (nothing is changed on disk) FilePath file = (FilePath)@"C:\Hello\World"; DirPath directory = file.Parent; // C:\Hello RelFilePath relative = file - directory; // World relative = relative.AppendPath(".txt"); // World.txt DirPath otherDirectory = (DirPath)@"C:\New"; FilePath newFile = otherDirectory + relative; // C:\New\World.txt FilePath pdfFile = newFile.GetWithExtension("pdf"); // C:\New\World.pdf DirPath otherDir = otherDirectory.CombineDir("Strong", "Typed"); // C:\New\Strong\Typed FilePath otherFile = otherDir.CombineFile("Test.txt"); // C:\New\Strong\Typed\Test.txt //Exploring disk IEnumerable <FilePath> files = directory.GetFiles("*", SearchOption.AllDirectories); IEnumerable <DirPath> directories = directory.GetDirectories(); var baseName = file.NameWithoutExtension; bool fileExists = file.Exists(); bool dirExists = directory.Exists(); //Operations on disk directory.CreateDirectory(); file.DeleteFile(); file.WriteAllText("Hello World"); file.ClearReadOnlyAttribute(); file.SetLastWriteTimeUtc(DateTime.UtcNow); directory.DeleteDir(); file.Move(pdfFile); newFile.CopyTo(otherFile); var newTarget = newFile.CopyTo(otherDir); // otherDir + newFile.Name; otherDir.CopyDirectory(otherDirectory); //Use path as a string File.ReadAllBytes(file.Path); }