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 folder.
///
///
public class TemporaryFolder : TemporaryItem
{
///
/// Initializes a new instance of the class.
///
/// The temporary item path.
public TemporaryFolder(string path) : base(path)
{
Directory.CreateDirectory(path);
}
///
/// Initializes a new instance of the class.
///
/// The temporary item path.
/// The item tag.
public TemporaryFolder(string path, object tag) : base(path, tag)
{
Directory.CreateDirectory(path);
}
///
/// Deletes the temporary item.
///
///
/// True is deletion was successful.
///
public override bool Delete()
{
base.Delete();
try
{
if (Directory.Exists(Path))
{
Directory.Delete(Path, true);
}
return true;
}
catch
{
return false;
}
}
///
/// Creates a new temporary file inside the temp folder and returns the file item.
///
///
public virtual TemporaryFile CreateFile(String extension = null)
{
return AddItem(new TemporaryFile(System.IO.Path.Combine(Path, System.IO.Path.GetRandomFileName() + extension))) as TemporaryFile;
}
///
/// Creates a new temporary file but does not create it.
///
///
public virtual TemporaryFile CreateImaginaryFile(String extension = null)
{
return AddItem(new TemporaryFile(System.IO.Path.Combine(Path, System.IO.Path.GetRandomFileName() + extension), false)) as TemporaryFile;
}
///
/// Creates a new temporary folder inside the temp folder and returns the folder item.
///
///
public virtual TemporaryFolder CreateFolder()
{
return AddItem(new TemporaryFolder(System.IO.Path.Combine(Path, System.IO.Path.GetRandomFileName()))) as TemporaryFolder;
}
///
/// 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 Directory.Exists(Path);
}
}
}