CSharpTest.Net
Commit Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.IO Namespace > ReplaceFile 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;

TempFile replace = new TempFile();
using (ReplaceFile temp = new ReplaceFile(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));

string backupfile = Path.ChangeExtension(replace.TempPath, ".bak");
File.Delete(Path.ChangeExtension(replace.TempPath, ".bak"));
Assert.IsFalse(File.Exists(backupfile));
replace.WriteAllText("backup");

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

Dim replace As New TempFile()
Using temp As New ReplaceFile(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))

Dim backupfile As String = Path.ChangeExtension(replace.TempPath, ".bak")
File.Delete(Path.ChangeExtension(replace.TempPath, ".bak"))
Assert.IsFalse(File.Exists(backupfile))
replace.WriteAllText("backup")

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

Assert.IsTrue(File.Exists(backupfile))
Using fbackup As TempFile = TempFile.Attach(backupfile)
    Assert.AreEqual("backup", fbackup.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