aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Core/IO/TemporaryFile.cs
blob: 340211d7453f4ca81b24d37b111a66aa6d2b1405 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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 file.
    /// </summary>
    /// <seealso cref="Tango.Core.IO.TemporaryItem" />
    public class TemporaryFile : TemporaryItem
    {
        /// <summary>
        /// Gets the name of the file.
        /// </summary>
        public String FileName
        {
            get { return System.IO.Path.GetFileName(Path); }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TemporaryFile"/> class.
        /// </summary>
        /// <param name="path">The temporary item path.</param>
        public TemporaryFile(string path, bool create = true) : base(path)
        {
            if (create)
            {
                Create();
            }
        }

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

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

            try
            {
                if (File.Exists(Path))
                {
                    File.Delete(Path);
                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Creates/Overwrites a new file in the specified path.
        /// </summary>
        public virtual void Create()
        {
            File.Create(Path).Dispose();
        }

        /// <summary>
        /// Creates a stream to the file path.
        /// </summary>
        /// <returns></returns>
        public virtual FileStream CreateStream()
        {
            return new FileStream(Path, FileMode.OpenOrCreate);
        }

        /// <summary>
        /// Writes the specified text to the file.
        /// </summary>
        /// <param name="text">The text.</param>
        public virtual void WriteAllText(String text)
        {
            File.WriteAllText(Path, text);
        }

        /// <summary>
        /// Writes the specified bytes to the file.
        /// </summary>
        /// <param name="data">The data.</param>
        public virtual void WriteAllBytes(byte[] data)
        {
            File.WriteAllBytes(Path, data);
        }

        /// <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 File.Exists(Path);
        }
    }
}