CSharpTest.Net
TempDirectory Class
Members  Example  See Also  Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.IO Namespace : TempDirectory Class

Glossary Item Box

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.NETCopy 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

System.Object
   CSharpTest.Net.IO.TempDirectory

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys