using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Tango.Core.IO { /// /// Represents a temporary file. /// /// public class TemporaryFile : TemporaryItem { /// /// Gets the name of the file. /// public String FileName { get { return System.IO.Path.GetFileName(Path); } } /// /// Initializes a new instance of the class. /// /// The temporary item path. public TemporaryFile(string path, bool create = true) : base(path) { if (create) { Create(); } } /// /// Initializes a new instance of the class. /// /// The temporary item path. /// The item tag. public TemporaryFile(string path, object tag) : base(path, tag) { Create(); } /// /// Deletes the temporary item. /// /// /// True is deletion was successful. /// public override bool Delete() { base.Delete(); try { if (File.Exists(Path)) { File.Delete(Path); } return true; } catch { return false; } } /// /// Creates/Overwrites a new file in the specified path. /// public virtual void Create() { File.Create(Path).Dispose(); } /// /// Creates a stream to the file path. /// /// public virtual FileStream CreateStream() { return new FileStream(Path, FileMode.OpenOrCreate); } /// /// Writes the specified text to the file. /// /// The text. public virtual void WriteAllText(String text) { File.WriteAllText(Path, text); } /// /// Writes the specified bytes to the file. /// /// The data. public virtual void WriteAllBytes(byte[] data) { File.WriteAllBytes(Path, data); } /// /// Makes the temporary item visible to the user somehow. /// public override void Display() { Process.Start("explorer.exe", string.Format("/select,\"{0}\"", Path)); } /// /// Returns true if the item exists. /// /// public override bool Exists() { return File.Exists(Path); } } }