1

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.

Chris Li
  • 3,715
  • 3
  • 29
  • 31

4 Answers4

3

Can you try

Server.MapPath("~/Content/Images/anon.png")
Manish Pansiniya
  • 537
  • 4
  • 14
  • What's the namespace of Server? I have only one named "Microsoft.SqlServer.Server", do I need additional reference in my project? – Chris Li Dec 06 '10 at 09:40
  • Gr8 Chris. Is the project other than website project? – Manish Pansiniya Dec 06 '10 at 12:28
  • OOPS, it throws a new UnauthorizedAccessException Access to the path 'D:\VS_2010\Trunk\app\ClientPortal\Content\Images\anon.png' is denied. I set "Everyone" to full control of this folder but doesn't work. But after I published to IIS it works. Seems it's related to identity of Webdev(the vs web server). How is that? Yes this project not the website project. – Chris Li Dec 06 '10 at 12:39
  • I think it is related to rights issue. First you need to check by loggin which user is vs web server (identity) is using and might be giving specific right to that user works. – Manish Pansiniya Dec 06 '10 at 12:42
  • Thanks for your reply Manish, in the Web project, the identity is my login name in the AD, but in this project, the HttpContent.Current.User returns nothing (an Exception) and I have full control for the entire solution folder – Chris Li Dec 06 '10 at 13:53
  • http://stackoverflow.com/questions/265953/how-can-you-easily-check-if-access-is-denied-for-a-file-in-net Can you please follow Mark's answer and see what happens – Manish Pansiniya Dec 06 '10 at 17:35
1

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.

Chris Li
  • 3,715
  • 3
  • 29
  • 31
1

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

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • Hi Simon, thanks for the reply. However, as I mentioned, this class is in the class library project, so the two methods returns "C:\Content\Images\anon.png", and throws a exception when I get it's stream, any ideal? – Chris Li Dec 06 '10 at 09:20
  • If the exception is access denied, it's because the web server is running with a user that does not have read access to the c:\content\etc... path. You need to store your file elsewhere or to give proper access rights to the path. – Simon Mourier Dec 06 '10 at 10:33
  • No, the exceptions says file not found – Chris Li Dec 06 '10 at 12:04
0

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.

Pabuc
  • 5,528
  • 7
  • 37
  • 52
  • In fact it searches "C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\Content\Images\anon.png" according to the exception info. It's the webdev server path and I don't find my solution folder in there. But the solution is still running cause I can access other pages through my browser. – Chris Li Dec 06 '10 at 09:38
  • Manish's answer should solve your problem. I guess the path is not currect. – Pabuc Dec 06 '10 at 09:41