【问题标题】:Setting a root path to use relative paths from设置根路径以使用相对路径
【发布时间】:2015-10-07 10:21:57
【问题描述】:

有没有办法设置“根”或基本路径,然后使用 C# 中的相对路径?

例如,假设我有路径:

C:\Users\Steve\Documents\Document.txt

然后我可以使用此路径而不是程序集作为基本路径吗?所以这将允许我使用类似的东西:

..\..\Pictures\Photo.png

谢谢

【问题讨论】:

标签: c# uri relative-path


【解决方案1】:

为什么不这样做:

string RootPath = string.empty

#if DEBUG
    RootPath = "C:\Users\Steve\Documents\Document.txt"
#else
    RootPath = System.Reflection.Assembly.GetAssembly(typeof(MyClass)).Location;
#endif

var newPath = Path.Combine(RootPath, "..\..\Pictures\Photo.png");

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2023-03-05
  • 2010-09-21
  • 1970-01-01
  • 1970-01-01
  • 2011-06-06
  • 1970-01-01
  • 2021-06-17
  • 1970-01-01
相关资源
最近更新 更多