Quantcast
Channel: How to get relative path from absolute path - Stack Overflow
Browsing latest articles
Browse All 25 View Live

Answer by Chris Schaller for How to get relative path from absolute path

Adapted answer from duplicate: How to make an absolute path relative to a particular folder? and updated to match parameter sequence as the .Net standard implementationThis is a more concise...

View Article



Answer by Dragos Durlut for How to get relative path from absolute path

In ASP.NET Core 2, if you want the relative path to bin\Debug\netcoreapp2.2 you can use the following combination: using Microsoft.AspNetCore.Hosting;using Microsoft.Extensions.Configuration;public...

View Article

Answer by Anton Krouglov for How to get relative path from absolute path

As pointed above .NET Core 2.x has implementation of Path.GetRelativePath. Code below is adapted from sources and works fine with .NET 4.7.1 Framework.// Licensed to the .NET Foundation under one or...

View Article

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

If you are using .NET Core 2.0, Path.GetRelativePath() is available providing this specific functionality: var relativeTo = @"C:\Program Files\Dummy Folder\MyProgram"; var path = @"C:\Program...

View Article

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

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";...

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

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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 Kevin for How to get relative path from absolute path

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

View Article

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

Browsing latest articles
Browse All 25 View Live




Latest Images