【问题标题】:How to find absolute path using a relative path in c#如何在c#中使用相对路径查找绝对路径
【发布时间】:2016-11-18 06:25:58
【问题描述】:

我有一个存储在变量say中的路径

string partialPath = '\editors\tinymce' 

我想得到上面fie的整个路径。上述文件的实际路径是 'D:\Back up\editors\tinymce'。我怎样才能在 C# 中做到这一点。谢谢

【问题讨论】:

  • 警告:不要在路径中添加“\”,否则你会得到 D:\editors\tinymce
  • 相对路径是相对于 当前 文件夹或特定文件夹的。如果当前文件夹在 C: 中,则路径指向 c:\editors\tinymce。如果您希望此路径引用另一个驱动器,则必须指定它。为什么是D: 而不是E:` or Z:`?

标签: c# absolute-path


【解决方案1】:
var dir = Directory.GetCurrentDirectory();
var output = Path.Combine(dir, partialPath);

【讨论】:

    【解决方案2】:

    这可能对你有用

    string partialPath = "\editors\tinymce";
    string fullPath = Path.GetFullPath(partialPath);
    

    输出:

    D:\editors\tinymce

    这可能对你有用

    string partialPath = "editors\tinymce";
    fullPath = Path.GetFullPath(partialPath);
    

    输出:

    D:\Back up\editors\tinymce

    【讨论】:

    • 但是如果我使用它,我会得到这个路径
    • @SamDaniel 这是正确的结果。这就是 relative 路径的含义——相对于某个文件夹。如果未指定,则文件夹为当前文件夹。如果你希望它指向一个不同的驱动器,你需要指定你想要的。操作系统不会搜索所有连接的驱动器以找到匹配项
    【解决方案3】:

    请尝试以下代码:

    代码:

    string partialPath = "\editors\tinymce";
    var output = Server.MapPath(partialPath);
    string str = HttpContext.Current.Server.MapPath(partialPath);
    

    您可以在输出 var 和 str 字符串上获取路径。

    让我们知道。

    【讨论】:

    • 如果我使用它,我的 C 盘被指出,但实际文件在 D 盘中
    • 字符串 partialPath = "\\editors\\tinymce"; var output = Server.MapPath(partialPath);
    猜你喜欢
    • 1970-01-01
    • 2011-06-15
    • 2014-03-09
    • 2011-10-16
    • 2015-12-24
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多