首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >获取相对于当前工作目录的路径?

获取相对于当前工作目录的路径?
EN

Stack Overflow用户
提问于 2009-03-31 22:09:55
回答 4查看 211.6K关注 0票数 79

我正在编写一个控制台实用程序来对命令行上指定的文件进行一些处理,但我遇到了一个无法通过Google/Stack和Overflow解决的问题。如果指定了完整路径(包括驱动器号),如何将该路径重新格式化为相对于当前工作目录?

一定有类似于VirtualPathUtility.MakeRelative函数的东西,但如果有,我也不知道。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-03-31 22:15:10

如果您不介意斜杠被交换,您可以滥用Uri

代码语言:javascript
复制
Uri file = new Uri(@"c:\foo\bar\blop\blap.txt");
// Must end in a slash to indicate folder
Uri folder = new Uri(@"c:\foo\bar\");
string relativePath = 
Uri.UnescapeDataString(
    folder.MakeRelativeUri(file)
        .ToString()
        .Replace('/', Path.DirectorySeparatorChar)
    );

作为函数/方法:

代码语言:javascript
复制
string GetRelativePath(string filespec, string folder)
{
    Uri pathUri = new Uri(filespec);
    // Folders must end in a slash
    if (!folder.EndsWith(Path.DirectorySeparatorChar.ToString()))
    {
        folder += Path.DirectorySeparatorChar;
    }
    Uri folderUri = new Uri(folder);
    return Uri.UnescapeDataString(folderUri.MakeRelativeUri(pathUri).ToString().Replace('/', Path.DirectorySeparatorChar));
}
票数 135
EN

Stack Overflow用户

发布于 2009-03-31 22:14:05

您可以使用Environment.CurrentDirectory获取当前目录,使用FileSystemInfo.FullPath获取任何位置的完整路径。因此,完全限定当前目录和相关文件,然后检查完整文件名是否以目录名开头-如果是,只需根据目录名的长度获取适当的子字符串。

下面是一些示例代码:

代码语言:javascript
复制
using System;
using System.IO;

class Program
{
    public static void Main(string[] args)
    {
        string currentDir = Environment.CurrentDirectory;
        DirectoryInfo directory = new DirectoryInfo(currentDir);
        FileInfo file = new FileInfo(args[0]);

        string fullDirectory = directory.FullName;
        string fullFile = file.FullName;

        if (!fullFile.StartsWith(fullDirectory))
        {
            Console.WriteLine("Unable to make relative path");
        }
        else
        {
            // The +1 is to avoid the directory separator
            Console.WriteLine("Relative path: {0}",
                              fullFile.Substring(fullDirectory.Length+1));
        }
    }
}

我并不是说它是世界上最健壮的东西(符号链接可能会把它搞糊涂),但如果这只是一个你偶尔会用到的工具,那也没问题。

票数 41
EN

Stack Overflow用户

发布于 2013-10-18 23:37:19

代码语言:javascript
复制
public string MakeRelativePath(string workingDirectory, string fullPath)
{
    string result = string.Empty;
    int offset;

    // this is the easy case.  The file is inside of the working directory.
    if( fullPath.StartsWith(workingDirectory) )
    {
        return fullPath.Substring(workingDirectory.Length + 1);
    }

    // the hard case has to back out of the working directory
    string[] baseDirs = workingDirectory.Split(new char[] { ':', '\\', '/' });
    string[] fileDirs = fullPath.Split(new char[] { ':', '\\', '/' });

    // if we failed to split (empty strings?) or the drive letter does not match
    if( baseDirs.Length <= 0 || fileDirs.Length <= 0 || baseDirs[0] != fileDirs[0] )
    {
        // can't create a relative path between separate harddrives/partitions.
        return fullPath;
    }

    // skip all leading directories that match
    for (offset = 1; offset < baseDirs.Length; offset++)
    {
        if (baseDirs[offset] != fileDirs[offset])
            break;
    }

    // back out of the working directory
    for (int i = 0; i < (baseDirs.Length - offset); i++)
    {
        result += "..\\";
    }

    // step into the file path
    for (int i = offset; i < fileDirs.Length-1; i++)
    {
        result += fileDirs[i] + "\\";
    }

    // append the file
    result += fileDirs[fileDirs.Length - 1];

    return result;
}

这段代码可能不是万无一失的,但这就是我想出来的。它更健壮一点。它采用两条路径并返回路径B作为相对于路径A的路径。

示例:

代码语言:javascript
复制
MakeRelativePath("c:\\dev\\foo\\bar", "c:\\dev\\junk\\readme.txt")
//returns: "..\\..\\junk\\readme.txt"

MakeRelativePath("c:\\dev\\foo\\bar", "c:\\dev\\foo\\bar\\docs\\readme.txt")
//returns: "docs\\readme.txt"
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/703281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档