[DllImport("shlwapi.dll", CharSet = CharSet.Auto)]
static extern bool PathIsRelative([In] string lpszPath);
Private Declare Function PathIsRelative Lib "shlwapi" Alias "PathIsRelativeA" (ByVal pszPath As String) As Long 'THIS IS UNTESTED!!!
None.
None.
None.
A better alternative would be to use the Path.IsPathRooted function. The return value is opposite of PathIsRelative, where it returns true for an absolute path, and false for a relative path.
[DllImport("shlwapi.dll", CharSet = CharSet.Auto)] static extern bool PathIsRelative([In] string lpszPath);
public static bool IsRelativePath(string path)
{
return PathIsRelative(path);
}
//nunit test
[Test]
public void TestIsRelative()
{
Assert.AreEqual(true,IO.IsRelativePath(@"test.txt"),"Test 1");
Assert.AreEqual(true, IO.IsRelativePath(@"..\test.txt"), "Test 2");
Assert.AreEqual(true, IO.IsRelativePath(@"..\..\test.txt"), "Test 3");
Assert.AreEqual(false, IO.IsRelativePath(@"C:\test.txt"), "Test 4");
}