2012-11-07 193 views
4

的绝对路径,我呼吁在C#中的方法如下:获取相对路径

return Chart.RenderChartHTML("../../Charts/MSLine.swf"); 

的事情是,该路径可以在哪个文件夹我从打电话RenderChartHTML不同,这取决于。

我尝试以下,这样找到的绝对路径,但不工作:

string mslinepath = HttpContext.Current.Server.MapPath("~/Charts/MSLine.swf"); 

return Chart.RenderChartHTML(mslinepath); 
+0

这有帮助吗? http://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-net-in-a-console-app – Joe

回答

8

使用ResolveUrl()。它将URL转换为请求客户端可用的URL。

那么试试这个:

string mslinepath = ResolveUrl("~/Charts/MSLine.swf") 

希望这将帮助!

+2

谢谢。为什么我需要〜虽然? –

+2

“〜”表示从“根”目录和“。”获取相对路径。指的是从当前目录获取相对路径。 –

7

你不需要~/。只需HttpContext.Current.Server.MapPath("Charts/MSLine.swf");