Reputation: 33
How do I access/open a file in C# not using an absolute path? The code below is not working.
string path = Server.UrlEncode(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\css\\sample.css");
Upvotes: 2
Views: 545
Reputation: 164281
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");
Upvotes: 3