1
votes

In my IIS server I have a Virtual Directory ("docPath") which is mapped with a physical folder of my machine.

I have a Window Service and from this service I need to get the Physical path of Virtual directory (created on IIS) i.e. "docPath".

Since this is Windows service so I do not have HTTPContext object and I cannot use the HttpContext.Current.Server.MapPath("/docPath");

This is what I've tried so far:

I have tried to use the ServerManager from Microsoft.Web.Administration.

ServerManager serverManager = new ServerManager();
Site site = serverManager.Sites.FirstOrDefault(s => s.Name == "Default Web Site");
Application myApp = site.Applications["/docPath"];

But in Sites only web application created on IIS server are coming and not Virtual directories.

Also, System.Web.Hosting.HostingEnvironment.MapPath is also not working.

Could anybody let me know how can I get physical path of a Virtual Directory in Windows Service?

1
You could have the service send an HTTP request to a URL on IIS that simply outputs the return value of MapPath(). Then the service can read that response. - Remy Lebeau
Try System.Web.Hosting.HostingEnvironment.MapPath. - JanneP
@JanneP I have already tried this and this is also not working. - Geeky Ninja
@RemyLebeau How could I do this any lead? - Geeky Ninja
Are you sure it's preferable for your windows service to have to know website and virtual directory names (either hard coded or via configuration) vs just being configured with the directory it should use directly? - Damien_The_Unbeliever

1 Answers

0
votes

Add a generic handler to your web app, then have it return to you the physical path. In the generic handler you can have this code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication2
{
    /// <summary>
    /// Summary description for PhysicalPathHandler
    /// </summary>
    public class PhysicalPathHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            context.Response.Write(HttpContext.Current.Server.MapPath("docPath"));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

Then you can use it like this in the windows service.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("your-web-app-web-address-to-the-generic-handler"); //e.g. http://localhost:2950/PhysicalPathHandler.ashx

            string physicalPath = string.Empty; //this will store the physical path
            using (var response = (HttpWebResponse)request.GetResponse())
            {
                var encoding = Encoding.GetEncoding(response.CharacterSet);

                using (var responseStream = response.GetResponseStream())
                using (var reader = new StreamReader(responseStream, encoding))
                    physicalPath= reader.ReadToEnd();
            }

Remember to include the following namespaces into the windows service

using System.IO;
using System.Net;
using System.Text;