CSharpTest.Net
MakeRelativePath Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Utils Namespace > FileUtils Class : MakeRelativePath Method

startFile
targetFile

Glossary Item Box

For this to work for a directory the argument should end with a '\' character

Syntax

Visual Basic (Declaration) 
Public Shared Function MakeRelativePath( _
   ByVal startFile As String, _
   ByVal targetFile As String _
) As String
C# 
public static string MakeRelativePath( 
   string startFile,
   string targetFile
)

Parameters

startFile
targetFile

Example

Library/Library.Test/TestFileUtils.cs

C#Copy Code
Assert.IsNull(FileUtils.MakeRelativePath(null, @"C:\Test\fileb.txt"));
Assert.IsNull(FileUtils.MakeRelativePath(@"C:\Test\fileb.txt", null));

Assert.AreEqual(
    @"filea.txt",
    FileUtils.MakeRelativePath(@"C:\Test\filea.txt", @"C:\Test\filea.txt")
    );
Assert.AreEqual(
    @"fileb.txt",
    FileUtils.MakeRelativePath(@"C:\Test\filea.txt", @"C:\Test\fileb.txt")
    );
Assert.AreEqual(
    @"..\fileb.txt",
    FileUtils.MakeRelativePath(@"C:\Test\filea.txt\", @"C:\Test\fileb.txt")
    );
Assert.AreEqual(
    @"..\fileb.txt\",
    FileUtils.MakeRelativePath(@"C:\Test\filea.txt\", @"C:\Test\fileb.txt\")
    );
Assert.AreEqual(
    @"fileb.txt\",
    FileUtils.MakeRelativePath(@"C:\Test\filea.txt", @"C:\Test\fileb.txt\")
    );
Assert.AreEqual(
    @"sub\fileb.txt",
    FileUtils.MakeRelativePath(@"C:\Test\filea.txt", @"C:\Test\sub\fileb.txt")
    );
Assert.AreEqual(
    @"..\fileb.txt",
    FileUtils.MakeRelativePath(@"C:\Test\sub\filea.txt", @"C:\Test\fileb.txt")
    );
Assert.AreEqual(
    @"C:\Test\sub\fileb.txt",
    FileUtils.MakeRelativePath(@"E:\Test\sub\filea.txt", @"C:\Test\sub\fileb.txt")
    );
Assert.AreEqual(
    @"..\test\fileb.txt",
    FileUtils.MakeRelativePath(@"sub\filea.txt", @"test\fileb.txt")
    );
Assert.AreEqual(
    @"..\..\test\fileb.txt",
    FileUtils.MakeRelativePath(@"..\sub\filea.txt", @"test\fileb.txt")
    );
Assert.AreEqual(
    @"fileb.txt",
    FileUtils.MakeRelativePath(@"sub\", @"sub\fileb.txt")
    );
Assert.AreEqual(
    @"sub\fileb.txt",
    FileUtils.MakeRelativePath(@"sub", @"sub\fileb.txt")
    );
Assert.AreEqual(
    @"sub\fileb.txt",
    FileUtils.MakeRelativePath(@".\sub", @"sub\fileb.txt")
    );
Assert.AreEqual(
    @"..\sub\fileb.txt",
    FileUtils.MakeRelativePath(@"sub\test", @".\sub\.\..\sub\fileb.txt")
    );
VB.NETCopy Code
Assert.IsNull(FileUtils.MakeRelativePath(Nothing, "C:\Test\fileb.txt"))
Assert.IsNull(FileUtils.MakeRelativePath("C:\Test\fileb.txt", Nothing))

Assert.AreEqual("filea.txt", FileUtils.MakeRelativePath("C:\Test\filea.txt", "C:\Test\filea.txt"))
Assert.AreEqual("fileb.txt", FileUtils.MakeRelativePath("C:\Test\filea.txt", "C:\Test\fileb.txt"))
Assert.AreEqual("..\fileb.txt", FileUtils.MakeRelativePath("C:\Test\filea.txt\", "C:\Test\fileb.txt"))
Assert.AreEqual("..\fileb.txt\", FileUtils.MakeRelativePath("C:\Test\filea.txt\", "C:\Test\fileb.txt\"))
Assert.AreEqual("fileb.txt\", FileUtils.MakeRelativePath("C:\Test\filea.txt", "C:\Test\fileb.txt\"))
Assert.AreEqual("sub\fileb.txt", FileUtils.MakeRelativePath("C:\Test\filea.txt", "C:\Test\sub\fileb.txt"))
Assert.AreEqual("..\fileb.txt", FileUtils.MakeRelativePath("C:\Test\sub\filea.txt", "C:\Test\fileb.txt"))
Assert.AreEqual("C:\Test\sub\fileb.txt", FileUtils.MakeRelativePath("E:\Test\sub\filea.txt", "C:\Test\sub\fileb.txt"))
Assert.AreEqual("..\test\fileb.txt", FileUtils.MakeRelativePath("sub\filea.txt", "test\fileb.txt"))
Assert.AreEqual("..\..\test\fileb.txt", FileUtils.MakeRelativePath("..\sub\filea.txt", "test\fileb.txt"))
Assert.AreEqual("fileb.txt", FileUtils.MakeRelativePath("sub\", "sub\fileb.txt"))
Assert.AreEqual("sub\fileb.txt", FileUtils.MakeRelativePath("sub", "sub\fileb.txt"))
Assert.AreEqual("sub\fileb.txt", FileUtils.MakeRelativePath(".\sub", "sub\fileb.txt"))
Assert.AreEqual("..\sub\fileb.txt", FileUtils.MakeRelativePath("sub\test", ".\sub\.\..\sub\fileb.txt"))

Requirements

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

See Also

Generated with Document! X 2011 by Innovasys