/// <summary> /// Creates a text file with the given contents. If the contents of the text file aren't changed, it won't write the new contents to /// the file to avoid causing an action to be considered outdated. /// </summary> /// <param name="Location">Path to the intermediate file to create</param> /// <param name="Contents">Contents of the new file</param> /// <returns>File item for the newly created file</returns> public static FileItem CreateIntermediateTextFile(FileReference Location, string Contents) { // Only write the file if its contents have changed. if (!FileReference.Exists(Location)) { DirectoryReference.CreateDirectory(Location.Directory); FileReference.WriteAllText(Location, Contents, GetEncodingForString(Contents)); } else { string CurrentContents = Utils.ReadAllText(Location.FullName); if (!String.Equals(CurrentContents, Contents, StringComparison.InvariantCultureIgnoreCase)) { FileReference BackupFile = new FileReference(Location.FullName + ".old"); try { Log.TraceLog("Updating {0}: contents have changed. Saving previous version to {1}.", Location, BackupFile); FileReference.Delete(BackupFile); FileReference.Move(Location, BackupFile); } catch (Exception Ex) { Log.TraceWarning("Unable to rename {0} to {1}", Location, BackupFile); Log.TraceLog("{0}", ExceptionUtils.FormatExceptionDetails(Ex)); } FileReference.WriteAllText(Location, Contents, GetEncodingForString(Contents)); } } // Reset the file info, in case it already knows about the old file FileItem Item = GetItemByFileReference(Location); Item.ResetCachedInfo(); return(Item); }
/// <inheritdoc/> public FileItem CreateIntermediateTextFile(FileReference Location, string Contents) { // Write the file Utils.WriteFileIfChanged(Location, Contents, StringComparison.InvariantCultureIgnoreCase); // Reset the file info, in case it already knows about the old file FileItem Item = FileItem.GetItemByFileReference(Location); InternalDependencies.Add(Item); Item.ResetCachedInfo(); return(Item); }