How do I access/open a file in C# not using an absolute path?
How do I access/open a file in C# not using an absolute path? The code below is not worki开发者_如何学Cng.
string path = Server.UrlEncode(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\css\\sample.css");
Decide what the relative path is relative to. It is common to use the BaseDirectory of the current application domain. Then use Path.Combine
to get a full path:
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "some\\relative\\path.txt");
If this is an ASP .NET application, use Server.MapPath
:
string path = Server.MapPath("~/some/relative/path.txt");
You want:
Server.MapPath("~/css/sample.css");
精彩评论