This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
iotemporaryfileholder.html
72 lines (66 loc) · 1.87 KB
/
iotemporaryfileholder.html
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
---
layout: documentation
title: Temporary File Holder
teaser: Creates a file with specified content and deletes it on disposal.
navigation:
- name: IO Overview
link: io.html
- name: Access
link: ioaccess.html
- name: File Paths
link: iofilepaths.html
- name: Temporary File Holder
link: iotemporaryfileholder.html
---
<h1>Temporary File Holder</h1>
<h2>Motivation</h2>
<p>
Cleaning-up temporary files can be tedious.
</p>
<p>
The <code>TemporaryFileHolder</code> allows you to create a temporary file by specifying its content. When the temporary file holder is disposed, the file is deleted.
</p>
<h2>Usage</h2>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
Stream content = ...
using (var holder = new TemporaryFileHolder("c:\\folder\\filename.extension", content))
{
// do something interesting
}
]]></script>
<p>
You can specify the absolute file path of the temporary file to create and its content - either with a stream or a string.
</p>
<p>
To delete the temporary file, dispose the temporary file holder.
</p>
<h3>Inside a Unit Test</h3>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
[TestFixture]
public class Test
{
TemporaryFileHolder temporaryFileHolder;
[SetUp]
public void SetUp()
{
temporaryFileHolder = new TemporaryFileHolder(
"c:\\temp\\file.ext",
"this is the content");
}
[TearDown]
public void TearDown()
{
temporaryFileHolder.Dispose();
}
}
]]></script>
<p>
Use the SetUp method (NUnit), the constructor (xUnit) or Establish (MSpec) to create the file.
</p>
<p>
Use the TearDown method (NUnit), destructor (xUnit) or CleanUp (MSpec) to delete the file.
</p>
<h3>Limitations</h3>
<p>
When the file cannot be deleted upon disposal of the temporary file holder, the file will stay and the occurred exception is swallowed.
</p>