开发者

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜