Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve physical path for virtual directory

Tags:

c#

iis

wcf

I have a wcf web service deployed in IIS 7. When an error occurs I need to log the message that caused the error. The log needs to be created on a network share where developers have access to the share (as we do not have access to the server where the service is deployed). A virtual directory was created in IIS whose physical path is on the network share.

How can I get the physical path of the virtual directory? I have tried using

System.Web.Hosting.HostingEnvironment.MapPath(virtualDirectoryPath)

but this just returns the physical path on the server where the service is located.

For example, if the virtual directory is "WSLogs" and I give the virtualDirectoryPath as "~/WSLogs" I will get back C:\inetpub\wwwroot\myServiceDirectory\WSLogs\

like image 649
knightscharge Avatar asked Sep 03 '13 21:09

knightscharge


People also ask

How do I find virtual directory?

In the Internet Information Services window, expand server name (where server name is the name of the server). Right-click the Web site that you want (for example, Default Web Site), point to New, and then click Virtual Directory.

What is virtual path and physical path?

First of all, let's get the overview of both. Physical path - This is the actual path the file is located by IIS. Virtual path - This is the logical path to access the file which is pointed to from outside of the IIS application folder.

What is a virtual file path?

Virtual Path or Relative Virtual Path: The path that the application identifies or is identified by from its Web server. For instance, in IIS (or OWIN) you may have a resource directory for your images in folder c:\\inetpub\ftp\images but the developer maps this folder to the app like so... ~\Images .


1 Answers

The virtual directory was created at the same level (under Default Web Site node) as the web service. After moving the virtual directory to be located under the web service node itself, I was able to get the physical path for the virtual directory using:

System.Web.Hosting.HostingEnvironment.MapPath(virtualDirectoryPath)

where the virtualDirectoryPath is ~/WSLogs

like image 151
knightscharge Avatar answered Nov 25 '22 17:11

knightscharge