forked from xerxesb/SpecFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTempFile.cs
106 lines (90 loc) · 2.59 KB
/
TempFile.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
namespace TechTalk.SpecFlow.Utils
{
// this class is based on the class found at http://www.vcskicks.com/code-snippet/temp-file-class.php
public class TempFile : IDisposable
{
private readonly string _tmpfile;
public TempFile()
: this(string.Empty)
{ }
public TempFile(string extension)
{
_tmpfile = Path.GetTempFileName();
if (!string.IsNullOrEmpty(extension))
{
string newTmpFile = _tmpfile + extension;
// create tmp-File with new extension ...
File.Create(newTmpFile).Dispose();
// delete old tmp-File
File.Delete(_tmpfile);
// use new tmp-File
_tmpfile = newTmpFile;
}
}
public void SetContent(string fileContent)
{
using (StreamWriter writer = new StreamWriter(FullPath, false, Encoding.UTF8))
{
writer.Write(fileContent);
}
}
public string FullPath
{
get { return _tmpfile; }
}
public string FileName
{
get { return Path.GetFileName(FullPath); }
}
public string FolderName
{
get { return Path.GetDirectoryName(FullPath); }
}
void IDisposable.Dispose()
{
try
{
if (!string.IsNullOrEmpty(_tmpfile) && File.Exists(_tmpfile))
File.Delete(_tmpfile);
}
catch(Exception ex)
{
Debug.WriteLine(ex, "TempFile.Dispose");
}
}
}
public class TempFolder : IDisposable
{
private readonly string tempFolder;
public TempFolder()
{
tempFolder = Path.GetTempFileName();
// delete old tmp-File
File.Delete(tempFolder);
// create a temp folder
Directory.CreateDirectory(tempFolder);
}
public string FolderName
{
get { return tempFolder; }
}
void IDisposable.Dispose()
{
try
{
if (!string.IsNullOrEmpty(tempFolder) && Directory.Exists(tempFolder))
Directory.Delete(tempFolder, true);
}
catch (Exception ex)
{
Debug.WriteLine(ex, "TempFolder.Dispose");
}
}
}
}