Chris Li
Chris Li

Reputation: 3725

how to find local image in asp.net project?

I have a solution contains a web project named "Web", and a dependeny class library project named "Service". I use the ASP.Net MVC2 to build up my solution. As you know, there's a Content folder storing images and css files under the web project. Now I need to get the stream reference of "Content\Images\anon.png" in one class of my "Service" project.

I tried

var result = new FileStream(@"Content\Images\anon.png", FileMode.Open);

and press F5 to debug, but it cannot find the file and throws an exception.

I am using VS2010, please tell me how can I access to this image. Thanks very much.

Upvotes: 1

Views: 2802

Answers (4)

Chris Li
Chris Li

Reputation: 3725

Visual studio makes it to a temp directory for your web app, not your solution folder/ If you publish your app on a IIS server, the Server.map will be correct.

Upvotes: 1

Manish Pansiniya
Manish Pansiniya

Reputation: 545

Can you try

Server.MapPath("~/Content/Images/anon.png")

Upvotes: 3

Pabuc
Pabuc

Reputation: 5638

Because it is searching for the file in Debug\Content\Images\ . Are you sure the images is really there? Make sure that your file is in that path.

Upvotes: 0

Simon Mourier
Simon Mourier

Reputation: 139256

You can use System.Web.VirtualPathUtility.ToAbsolute("~/Content/Images/anon.png"); or RequestContext.HttpContext.Request.MapPath as well

Upvotes: 1

Related Questions