2015-10-07 64 views
0

中的相對路徑有沒有一種方法可以設置'root'或基本路徑,然後使用C#中的相對路徑?設置根路徑以使用

因此,例如,說我有路徑:

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

我能然後使用此路徑,而不是程序組件,是基本路徑。因此,這將隨後允許我使用類似:

..\..\Pictures\Photo.png 

感謝

+3

的可能重複http://stackoverflow.com/questions/4796254/relative- path-to-absolute-path-in-c?rq = 1 – Igor

+0

是的,這工作謝謝你! – Stinkidog

回答

0

爲什麼不這樣做:

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"); 
+0

我相信這會起作用,但並不完全在我所需要的範圍內。然而,我發現這裏的最佳答案是爲我工作。這個問題真的是重複的: http://stackoverflow.com/questions/4796254/relative-path-to-absolute-path-in-c?rq=1 – Stinkidog