Provides a class for managing a temporary directory and making reasonable a attempt to remove it upon disposal.
Syntax
Visual Basic (Declaration) | |
---|
Public Class TempDirectory |
C# | |
---|
public class TempDirectory |
Example
Library/Library.Test/TestTempFiles.cs
C# | Copy Code |
---|
using(TempDirectory a = new TempDirectory())
using(TempDirectory b = new TempDirectory())
{
File.WriteAllText(Path.Combine(a.TempPath, "a.txt"), "text-a");
Directory.CreateDirectory(Path.Combine(a.TempPath, "child"));
File.WriteAllText(Path.Combine(a.TempPath, @"child\b.txt"), "text-b");
a.CopyTo(b.TempPath);
Assert.IsTrue(File.Exists(Path.Combine(b.TempPath, "a.txt")));
Assert.AreEqual("text-a", File.ReadAllText(Path.Combine(b.TempPath, "a.txt")));
Assert.IsTrue(File.Exists(Path.Combine(b.TempPath, @"child\b.txt")));
Assert.AreEqual("text-b", File.ReadAllText(Path.Combine(b.TempPath, @"child\b.txt")));
File.WriteAllText(Path.Combine(a.TempPath, @"child\b.txt"), "text-b-new");
a.CopyTo(b.TempPath, true);
//has changed
Assert.AreEqual("text-b-new", File.ReadAllText(Path.Combine(b.TempPath, @"child\b.txt")));
} |
VB.NET | Copy Code |
---|
Using a As New TempDirectory()
Using b As New TempDirectory()
File.WriteAllText(Path.Combine(a.TempPath, "a.txt"), "text-a")
Directory.CreateDirectory(Path.Combine(a.TempPath, "child"))
File.WriteAllText(Path.Combine(a.TempPath, "child\b.txt"), "text-b")
a.CopyTo(b.TempPath)
Assert.IsTrue(File.Exists(Path.Combine(b.TempPath, "a.txt")))
Assert.AreEqual("text-a", File.ReadAllText(Path.Combine(b.TempPath, "a.txt")))
Assert.IsTrue(File.Exists(Path.Combine(b.TempPath, "child\b.txt")))
Assert.AreEqual("text-b", File.ReadAllText(Path.Combine(b.TempPath, "child\b.txt")))
File.WriteAllText(Path.Combine(a.TempPath, "child\b.txt"), "text-b-new")
a.CopyTo(b.TempPath, True)
'has changed
Assert.AreEqual("text-b-new", File.ReadAllText(Path.Combine(b.TempPath, "child\b.txt")))
End Using
End Using |
Inheritance Hierarchy
Requirements
Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7
See Also