Quantcast
Channel: How to get relative path from absolute path - Stack Overflow
Browsing index pages (25 articles)

How to get relative path from absolute path

There's a part in my apps that displays the file path loaded by the user through OpenFileDialog. It's taking up too much space to display the whole path, but I don't want to display only the filename...

View Article


Answer by Kevin for How to get relative path from absolute path

Use:RelPath = AbsPath.Replace(ApplicationPath, ".")

View Article


Answer by 3Doubloons for How to get relative path from absolute path

I'd split both of your paths at the directory level. From there, find the point of divergence and work your way back to the assembly folder, prepending a '../' everytime you pass a directory.Keep in...

View Article

Answer by DavidK for How to get relative path from absolute path

There is a Win32 (C++) function in shlwapi.dll that does exactly what you want: PathRelativePathTo()I'm not aware of any way to access this from .NET other than to P/Invoke it, though.

View Article

Answer by Jonathan Leffler for How to get relative path from absolute path

As Alex Brault points out, especially on Windows, the absolute path (with drive letter and all) is unambiguous and often better.Shouldn't your OpenFileDialog use a regular tree-browser structure?To get...

View Article


Answer by James Newton-King for How to get relative path from absolute path

I have used this in the past./// <summary>/// Creates a relative path from one file/// or folder to another./// </summary>/// <param name="fromDirectory">/// Contains the directory...

View Article

Answer by CestLaGalere for How to get relative path from absolute path

If you have a readonly text box, could you not not make it a label and set AutoEllipsis=true?alternatively there are posts with code for generating the autoellipsis yourself: (this does it for a grid,...

View Article

Answer by Vilx- for How to get relative path from absolute path

It's a long way around, but System.Uri class has a method named MakeRelativeUri. Maybe you could use that. It's a shame really that System.IO.Path doesn't have this.

View Article


Answer by Dave for How to get relative path from absolute path

.NET Core 2.0 has Path.GetRelativePath, else, use this./// <summary>/// Creates a relative path from one file or folder to another./// </summary>/// <param name="fromPath">Contains...

View Article


Answer by ctacke for How to get relative path from absolute path

A bit late to the question, but I just needed this feature as well. I agree with DavidK that since there is a built-in API function that provides this, you should use it. Here's a managed wrapper for...

View Article

Answer by user3458 for How to get relative path from absolute path

If you're sure that your absolute path 2 is always relative to absolute path, just remove the first N characters from path2, where N is the length of path1.

View Article

Answer by AMissico for How to get relative path from absolute path

You want to use the CommonPath method of this RelativePath class. Once you have the common path, just strip it out of the path you want to display.Namespace IO.Path Public NotInheritable Class...

View Article

Answer by Cameron Stone for How to get relative path from absolute path

If you know that toPath is contained by fromPath then you can keep it simple. I'll leave out the asserts for brevity.public static string MakeRelativePath(string fromPath, string toPath){ // use...

View Article


Answer by Szybki for How to get relative path from absolute path

The function that uses URI returned "almost" relative path. It included directory that directly contains the file which relative path I wanted to get.Some time ago I wrote a simple function that...

View Article

Answer by Maxence for How to get relative path from absolute path

I'm using this:public static class StringExtensions{ /// <summary> /// Creates a relative path from one file or folder to another. /// </summary> /// <param name="absPath">Absolute...

View Article


Answer by Muhammad Rehan Saeed for How to get relative path from absolute path

.NET Core 2.0 Answer.NET Core 2.0 has Path.GetRelativePath which can be used like so:var relativePath = Path.GetRelativePath( @"C:\Program Files\Dummy Folder\MyProgram", @"C:\Program Files\Dummy...

View Article

Answer by user626528 for How to get relative path from absolute path

public static string ToRelativePath(string filePath, string refPath) { var pathNormalized = Path.GetFullPath(filePath); var refNormalized = Path.GetFullPath(refPath); refNormalized =...

View Article


Answer by excanoe for How to get relative path from absolute path

This should work:private string rel(string path) { string[] cwd = new Regex(@"[\\]").Split(Directory.GetCurrentDirectory()); string[] fp = new Regex(@"[\\]").Split(path); int common = 0; for (int n =...

View Article

Answer by Alexey Makarenya for How to get relative path from absolute path

Way with Uri not worked on linux/macOS systems. Path '/var/www/root' can't be converted to Uri. More universal way - do all by hands.public static string MakeRelativePath(string fromPath, string...

View Article

Answer by Spongman for How to get relative path from absolute path

here's mine:public static string RelativePathTo(this System.IO.DirectoryInfo @this, string to){ var rgFrom = @this.FullName.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar },...

View Article
Browsing index pages (25 articles)


Latest Images