aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Core/IO/TemporaryFolder.cs
blob: 84f0ee6871d93e32783aea4e1274fd0d5ef0af1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
{
    /// <summary>
    /// Represents a temporary folder.
    /// </summary>
    /// <seealso cref="Tango.Core.IO.TemporaryItem" />
    public class TemporaryFolder : TemporaryItem
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TemporaryFolder"/> class.
        /// </summary>
        /// <param name="path">The temporary item path.</param>
        public TemporaryFolder(string path) : base(path)
        {
            Directory.CreateDirectory(path);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TemporaryFolder"/> class.
        /// </summary>
        /// <param name="path">The temporary item path.</param>
        /// <param name="tag">The item tag.</param>
        public TemporaryFolder(string path, object tag) : base(path, tag)
        {
            Directory.CreateDirectory(path);
        }

        /// <summary>
        /// Deletes the temporary item.
        /// </summary>
        /// <returns>
        /// True is deletion was successful.
        /// </returns>
        public override bool Delete()
        {
            base.Delete();

            try
            {
                if (Directory.Exists(Path))
                {
                    Directory.Delete(Path, true);
                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Creates a new temporary file inside the temp folder and returns the file item.
        /// </summary>
        /// <returns></returns>
        public virtual TemporaryFile CreateFile(String extension = null)
        {
            return AddItem(new TemporaryFile(System.IO.Path.Combine(Path, System.IO.Path.GetRandomFileName() + extension))) as TemporaryFile;
        }

        /// <summary>
        /// Creates a new temporary file but does not create it.
        /// </summary>
        /// <returns></returns>
        public virtual TemporaryFile CreateImaginaryFile(String extension = null)
        {
            return AddItem(new TemporaryFile(System.IO.Path.Combine(Path, System.IO.Path.GetRandomFileName() + extension), false)) as TemporaryFile;
        }

        /// <summary>
        /// Creates a new temporary folder inside the temp folder and returns the folder item.
        /// </summary>
        /// <returns></returns>
        public virtual TemporaryFolder CreateFolder()
        {
            return AddItem(new TemporaryFolder(System.IO.Path.Combine(Path, System.IO.Path.GetRandomFileName()))) as TemporaryFolder;
        }

        /// <summary>
        /// Makes the temporary item visible to the user somehow.
        /// </summary>
        public override void Display()
        {
            Process.Start("explorer.exe", string.Format("/select,\"{0}\"", Path));
        }

        /// <summary>
        /// Returns true if the item exists.
        /// </summary>
        /// <returns></returns>
        public override bool Exists()
        {
            return Directory.Exists(Path);
        }
    }
}