कोई अंतर्निहित .NET नहीं है, लेकिन मूल कार्य है। इस तरह यह प्रयोग करें:
[DllImport("shlwapi.dll", CharSet=CharSet.Auto)]
static extern bool PathRelativePathTo(
[Out] StringBuilder pszPath,
[In] string pszFrom,
[In] FileAttributes dwAttrFrom,
[In] string pszTo,
[In] FileAttributes dwAttrTo
);
या यदि आप अभी भी प्रबंधित कोड को पसंद करते हैं तो कोशिश यह:
public static string GetRelativePath(FileSystemInfo path1, FileSystemInfo path2)
{
if (path1 == null) throw new ArgumentNullException("path1");
if (path2 == null) throw new ArgumentNullException("path2");
Func<FileSystemInfo, string> getFullName = delegate(FileSystemInfo path)
{
string fullName = path.FullName;
if (path is DirectoryInfo)
{
if (fullName[fullName.Length - 1] != System.IO.Path.DirectorySeparatorChar)
{
fullName += System.IO.Path.DirectorySeparatorChar;
}
}
return fullName;
};
string path1FullName = getFullName(path1);
string path2FullName = getFullName(path2);
Uri uri1 = new Uri(path1FullName);
Uri uri2 = new Uri(path2FullName);
Uri relativeUri = uri1.MakeRelativeUri(uri2);
return relativeUri.OriginalString;
}
स्रोत
2012-01-30 14:15:04
यह बताया जाना चाहिए कि जब इस पद्धति का उपयोग कि सापेक्ष पथ दिया जाएगा जो '\' के बजाय '/' होगा। आउटपुट देगा: ../../..Users/Steve/ एक साधारण प्रतिस्थापन फ़ाइल पथिंग के लिए इसे सही करेगा। –
यह सभी किनारे के मामलों को संभाल नहीं करता है। [यह] देखें (http://stackoverflow.com/questions/275689/how-to-get-relative-path-from-absolute-path/32113484#32113484) उत्तर। –