Quantcast
Channel: How to get relative path from absolute path - Stack Overflow
Viewing all articles
Browse latest Browse all 25

Answer by Sergey Orlov for How to get relative path from absolute path

$
0
0

Play with something like:

private String GetRelativePath(Int32 level, String directory, out String errorMessage) {        if (level < 0 || level > 5) {            errorMessage = "Find some more smart input data";            return String.Empty;        }        // ==========================        while (level != 0) {            directory = Path.GetDirectoryName(directory);            level -= 1;        }        // ==========================        errorMessage = String.Empty;        return directory;    }

And test it

[Test]    public void RelativeDirectoryPathTest() {        var relativePath =            GetRelativePath(3, AppDomain.CurrentDomain.BaseDirectory, out var errorMessage);        Console.WriteLine(relativePath);        if (String.IsNullOrEmpty(errorMessage) == false) {            Console.WriteLine(errorMessage);            Assert.Fail("Can not find relative path");        }    }

Viewing all articles
Browse latest Browse all 25


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>