CSharpTest.Net
Commit Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.IO Namespace > TransactFile Class : Commit Method

Glossary Item Box

Commits the replace operation on the file

Syntax

Visual Basic (Declaration) 
Public Sub Commit() 
C# 
public void Commit()

Example

Library/Library.Test/TestReplaceFile.cs

C#Copy Code
string testdata = Guid.NewGuid().ToString();
string tmpPath;

using (TempFile replace = new TempFile())
{
    using (TransactFile temp = new TransactFile(replace.TempPath))
    {
        Assert.AreEqual(temp.TargetFile, replace.TempPath);
        tmpPath = temp.TempPath;
        Assert.IsTrue(temp.Exists);
        temp.WriteAllText(testdata);
        //missing commit:
        //temp.Commit();
    }
    Assert.AreEqual(0, replace.Length);
    Assert.IsFalse(File.Exists(tmpPath));

    //now for real
    using (TransactFile temp = new TransactFile(replace.TempPath))
    {
        tmpPath = temp.TempPath;
        Assert.IsTrue(temp.Exists);
        temp.WriteAllText(testdata);
        temp.Commit();
    }
    Assert.IsFalse(File.Exists(tmpPath));
    Assert.AreEqual(testdata, replace.ReadAllText());
}
VB.NETCopy Code
Dim testdata As String = Guid.NewGuid().ToString()
Dim tmpPath As String

Using replace As New TempFile()
    Using temp As New TransactFile(replace.TempPath)
        Assert.AreEqual(temp.TargetFile, replace.TempPath)
        tmpPath = temp.TempPath
        Assert.IsTrue(temp.Exists)
            'missing commit:
            'temp.Commit();
        temp.WriteAllText(testdata)
    End Using
    Assert.AreEqual(0, replace.Length)
    Assert.IsFalse(File.Exists(tmpPath))

    'now for real
    Using temp As New TransactFile(replace.TempPath)
        tmpPath = temp.TempPath
        Assert.IsTrue(temp.Exists)
        temp.WriteAllText(testdata)
        temp.Commit()
    End Using
    Assert.IsFalse(File.Exists(tmpPath))
    Assert.AreEqual(testdata, replace.ReadAllText())
End Using

Requirements

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

See Also

Generated with Document! X 2011 by Innovasys