0

I'm working on asp.net (vb, maybe that can change something)

I have a project using as class library. I want it to load a text file. the file is on a folder on this project

usely I use "File.Exists" and "Server.MapPath("~")". but I do not have Server lib.

I look a way to use the file as content or resource. but I didnt succed for the moment(probably my fault)

So if you know a way to do what I want? Or how to use the file as a resource like by stream or something else?

I looked at this but my first try not working (http://keithelder.net/2007/12/14/How-To-Load-an-Embedded-Resource-From-A-DLL/)

1
  • I will have little problem, because my framework didn't copy my text file to iis. Commented Mar 27, 2012 at 18:05

1 Answer 1

1

Add System.Web as a project reference to your class library, and use HttpContext:

using System.Web;

public class Foo
{
    public void Bar()
    {
        var path = HttpContext.Current.Server.MapPath("~/somefolder/somefile.txt");
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I try, httpcontext can be use. but with ... I get an high level exception : System.Web.HttpException failed to get...
It can or it can't be used? Assuming you meant that it can't be used, why?
HttpContext.Current.Server.MapPath("~") is the of root my site, but MapPath("..."); cause an error
The ellipses ("...") was intended to be a placeholder for the path to the file. I updated my answer so it's a little more clear.
tanks, its working. (just need to found the way that my VS copy my file into IIS.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.