Jason94
Jason94

Reputation: 13610

How can I open a text file relative to my MVC project?

In my bin file I have set up som test data, and I want my application to be able to access logfiles that are stored in bin/log/log00001.txt.

However, in my crontroller, when I try to use a TextReader on the following path it goes somewhere else: new StreamReader("log/log00001.txt")

How do I read stuff relative to my project?

Upvotes: 8

Views: 5549

Answers (2)

chead23
chead23

Reputation: 1879

Try using

StreamReader reader = new StreamReader(Server.MapPath("~/bin/log/log00001.txt"));

Upvotes: 13

spender
spender

Reputation: 120450

HttpContext.Current.Server.MapPath("~/some/path/relative/to/your/web/app")

Upvotes: 6

Related Questions