开发者

How do I access/open a file in C# not using an absolute path?

开发者 https://www.devze.com 2023-03-31 07:36 出处:网络
How do I access/open a file in C# not using an absolute path? The code below is not worki开发者_如何学Cng.

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

精彩评论

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