4

I'm working in a legacy project that still using flash as front end.

But the backend is in c# .net 3.5.

I've created a new functionality that needs a file of the application, to do this I've used:

Path.GetDirectoryName(Assembly.GetCallingAssembly().Location)

But it's showing that error:

'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\sirius2\af2e88a7\10f74a24\assembly\dl3\872c826f\89d0f861_6ef5d001\Images' is denied

The project use IIS and the right path, the path where we can find the folder Images is in the directory mapped in IIS.

Is there a way to obtain that path?

JoaxAzevedo
  • 123
  • 1
  • 11

3 Answers3

5

Some answers are using a web project. But I'm using a Class Project, and Server class is not referenced in that kind of project.

Really is a problem created by shadow copy of dlls, but to resolve I need to use

Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase.ToString()).LocalPath)

And it returns the real path of dll.

Thank you all for the help!

JoaxAzevedo
  • 123
  • 1
  • 11
1

Due to the way asp.net handles your compiled code (by using Shadow Copy folders - the "Temporary ASP>NET Files dir" from above - you cannot use the regular Path.XXX static methods (see What is the "Temporary ASP.NET Files" folder for? for a fuller answer).

Instead you can use Server.MapPath("/") which will return the physical path of the root of the app - so you can also do (for example) Server.MapPath("/Images").

This answer shows other options for MapPath: https://stackoverflow.com/a/275791/990323


For clarity (from the comments below): "/" will return a path relative to the root of the domain (i.e. "C:\inetpub\wwwroot". "~/" returns the path relative to where the application is actually installed "C/MyCode/MyWebApp" Only by not beginning the string with a forward/backslash or tilde to you get relative path behaviour as discussed below.

Community
  • 1
  • 1
kenam
  • 125
  • 8
  • My understanding was `MapPAth("/")` was the web site root which may be suitable for the OP. The OP did not specify how the site was set up (i..e application under the site etc) so I linked to another answer with more options. I don't see how the post was particularly unhelpful.. – kenam Sep 22 '15 at 20:43
  • No - this is incorrect. The difference between "/" and "~/" is not related to the request. See the full answer by @splattne in the link. – kenam Sep 23 '15 at 08:51
  • For clarity: "/" will return a path relative to the root of the domain (i.e. "C:|inetpub\wwwroot". "~/" returns the path relative to where the application is actually installed "C/MyCode/MyWebApp" Only by not beginning the string with a forward/backslash or tilde to you get the relative behaviour you are talking about. – kenam Sep 23 '15 at 08:56
  • can you do a quick edit and I will remove the downvote? – DVK Sep 23 '15 at 13:20
0

If you have a static directory you are trying to access, typically you'll want to specify directories relative to the application root. This is done using "~/". For example, if you always wanted to find the physical directory for http://myapplication/images regardless of the directory your request is executing in, then you'd use the following:

string rootImageDirectory = Server.MapPath("~/Images");

If you don't specify the tilde and just use "/", then the directory returned is relative to the current execution path instead of to the root of the application.

DVK
  • 2,726
  • 1
  • 17
  • 20