CSharpTest.Net
ReplaceFile Constructor(String,String)
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.IO Namespace > ReplaceFile Class > ReplaceFile Constructor : ReplaceFile Constructor(String,String)

targetName
The name of the file to replace
backupExtension
A valid file extension beginning with '.'

Glossary Item Box

Creates a backup of the target file when replacing using the extension provided

Syntax

Visual Basic (Declaration) 
Public Function New( _
   ByVal targetName As String, _
   ByVal backupExtension As String _
)
C# 
public ReplaceFile( 
   string targetName,
   string backupExtension
)

Parameters

targetName
The name of the file to replace
backupExtension
A valid file extension beginning with '.'

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