Search code examples
c#pathrelative-path

Getting the relative path from the full path


I have to get the path excluding the relative path from the full path, say

The relative path is ,C:\User\Documents\

fullpath ,C:\User\Documents\Test\Folder2\test.pdf

I want to get only the path after the relative path i.e \Test\Folder2\test.pdf

how can i achieve this.

I am using C# as the programming language


Solution

  • You are not talking about relative, so i will call it partial path. If you can be sure that the partial path is part of your full path its a simple string manipulation:

    string fullPath = @"C:\User\Documents\Test\Folder2\test.pdf";
    string partialPath = @"C:\User\Documents\";
    string resultingPath = fullPath.Substring(partialPath.Length);
    

    This needs some error checking though - it will fail when either fullPath or partialPath is null or both paths have the same length.