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

originalPath
the name of the file
tempfilePath
[out] the temp file name

Glossary Item Box

Derives a new filename that doesn't exist from the provided name, ie. file.txt becomes file.txt.~0001

Syntax

Visual Basic (Declaration) 
Public Shared Function CreateDerivedFile( _
   ByVal originalPath As String, _
   ByRef tempfilePath As String _
) As Stream
C# 
public static Stream CreateDerivedFile( 
   string originalPath,
   out string tempfilePath
)

Parameters

originalPath
the name of the file
tempfilePath
[out] the temp file name

Return Value

A stream with exclusive write access to the file

Example

Library/Library.Test/TestReplaceFile.cs

C#Copy Code
string path;
using(DisposingList l = new DisposingList())
{
    TempFile ftarget = new TempFile();
    ftarget.Delete();

    l.Add(ftarget);
    TempFile fbigone = TempFile.Attach(String.Format("{0}.~{1:x4}", ftarget.TempPath, 0x10008 - 25));
    fbigone.Create().Dispose();
    Assert.IsTrue(fbigone.Exists);
    l.Add(fbigone);
    path = ftarget.TempPath;

    string tmpName;
    Dictionary<string, object> names = new Dictionary<string,object>(StringComparer.OrdinalIgnoreCase);

    for( int i=0; i < 25; i++ )
    {
        Stream s = ReplaceFile.CreateDerivedFile(ftarget.TempPath, out tmpName);
        l.Add(TempFile.Attach(tmpName));
        l.Add(s);
        names.Add(tmpName, null);
    }
    
    fbigone.Delete();
    Assert.AreEqual(25, Directory.GetFiles(Path.GetDirectoryName(path), Path.GetFileName(path) + "*").Length);
}
Assert.AreEqual(0, Directory.GetFiles(Path.GetDirectoryName(path), Path.GetFileName(path) + "*").Length);
VB.NETCopy Code
Dim path As String
Using l As New DisposingList()
    Dim ftarget As New TempFile()
    ftarget.Delete()

    l.Add(ftarget)
    Dim fbigone As TempFile = TempFile.Attach([String].Format("{0}.~{1:x4}", ftarget.TempPath, &H10008 - 25))
    fbigone.Create().Dispose()
    Assert.IsTrue(fbigone.Exists)
    l.Add(fbigone)
    path = ftarget.TempPath

    Dim tmpName As String
    Dim names As New Dictionary(Of String, Object)(StringComparer.OrdinalIgnoreCase)

    Dim i As Integer = 0
    While i < 25
        Dim s As Stream = ReplaceFile.CreateDerivedFile(ftarget.TempPath, tmpName)
        l.Add(TempFile.Attach(tmpName))
        l.Add(s)
        names.Add(tmpName, Nothing)
        System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
    End While

    fbigone.Delete()
    Assert.AreEqual(25, Directory.GetFiles(Path.GetDirectoryName(path), Path.GetFileName(path) + "*").Length)
End Using
Assert.AreEqual(0, Directory.GetFiles(Path.GetDirectoryName(path), Path.GetFileName(path) + "*").Length)

Requirements

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

See Also

Generated with Document! X 2011 by Innovasys