如何在C#中使用絕對路徑訪問/打開文件?下面的代碼不起作用。如何在C#中使用絕對路徑訪問/打開文件?
string path = Server.UrlEncode(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\css\\sample.css");
如何在C#中使用絕對路徑訪問/打開文件?下面的代碼不起作用。如何在C#中使用絕對路徑訪問/打開文件?
string path = Server.UrlEncode(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\css\\sample.css");
決定什麼的相對路徑是相。通常使用當前應用程序域的BaseDirectory。然後使用Path.Combine
得到一個完整的路徑:
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "some\\relative\\path.txt");
如果這是一個ASP .NET應用程序,使用Server.MapPath
:
string path = Server.MapPath("~/some/relative/path.txt");
感謝您的快速幫助.... Server.MapPath()工作... – rock
不工作怎麼樣? Server.UrlEncode不是必需的。 – driis
Web或Winforms? – Arun