/// <summary>
        /// Upgrades the site with the specified ID to the current version of the product
        /// </summary>
        /// <param name="siteId"></param>
        /// <param name="templateId"></param>
        /// <returns></returns>
        protected override Site UpgradeSite(string siteId, string templateId, out FaultContract fault)
        {
            // Get the site
            Site site = SiteConfiguration.FindExistingSiteByID(siteId);
            if (site == null)
                throw new Exception("Unable to find site with siteId = " + siteId);

            // Do upgrade
            fault = null;
            bool upgraded = ApplicationBuilderHelper.UpgradeSite(site, templateId, out fault);

            if (upgraded)
            {
                // Upgrade successful - update the version on the site and save it to disk
                Version currentVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
                site.ProductVersion = string.Format("{0}.{1}.{2}.0", currentVersion.Major, currentVersion.Minor,
                    currentVersion.Build);
                SiteConfiguration.SaveSite(site);
                return site;
            }
            else
            {
                if (fault == null)
                    fault = new FaultContract() { Message = "Upgrade failed" };
                return null;
            }
        }
        public IIsVirtualDirectoryInfo CreateNewSite(Site site, string webSiteId, out FaultContract Fault)
        {
            string status = string.Empty;
            Fault = null;
            if (!string.IsNullOrEmpty(site.IISPath) && !string.IsNullOrEmpty(site.IISHost))
            {
                string iisLocation = string.Empty;

                // Check if we can query IIS
                string pathToQuery = string.Format("IIS://{0}/W3SVC", site.IISHost);                

                // Get the location of the the website running at this host and that port .. as Active Directory entry
                iisLocation = IISHelper.GetIISLocation(site.IISHost, string.Format(":{0}:", site.IISPort), out status);

                string websitePhysicalPath = GetPhysicalPathForSite(site, false, out Fault);
                if (!string.IsNullOrEmpty(status))
                {
                    Fault = new FaultContract
                    {
                        FaultType = "Error",
                        Message = "Error retrieving physical path for site.Server:- " + site.IISHost + " Port:- " + site.IISPort + " Status:- " + status
                    };
                    return null;
                }

                // When doing File I/O .. we need UNC paths .. but for IIS Helper .. we need a normal path
                string websiteUNCPath = GetPhysicalPathForSite(site, true, out Fault);
                if (!Directory.Exists(websiteUNCPath))
                {
                    Directory.CreateDirectory(websiteUNCPath);
                }
                if (IISHelper.VirtualDirectoryExists(iisLocation, site.IISPath, out status))
                {
                    Fault = new FaultContract
                    {
                        FaultType = "Error",
                        Message = "Virtual Directory already exists" + status
                    };
                    return null;
                }
                if (IISHelper.CreateVirtualDirectory(iisLocation + "/Root", site.IISPath, websitePhysicalPath, out status))
                {
                    IIsVirtualDirectoryInfo vDir = new IIsVirtualDirectoryInfo {
                        ADSIPath = pathToQuery + "/" + site.IISPath,
                         Name = site.Name,
                        PhysicalPath = websiteUNCPath,
                         VirtualPath = site.IISPath
                    };
                    return vDir;
                }
            }
            return null;
        }
 public static string GetPhysicalPathForId(string siteId, bool isTemplate, string relativePath, out FaultContract Fault)
 {
     Fault = null;
     string physicalPath = null;
     if (isTemplate)
     {
         Template template = TemplateConfiguration.FindTemplateById(siteId);
         if (template == null)
         {
             Fault = new FaultContract();
             Fault.FaultType = "Error";
             Fault.Message = "Unable to find template with ID = " + siteId;
             return null;
         }
         else
         {
             physicalPath = ServerUtility.MapPath(TemplateFolderPath) + "\\" + template.ID;
             if (!string.IsNullOrEmpty(relativePath))
                 physicalPath += "\\" + relativePath.Replace("/", "\\");
         }
     }
     else
     {
         Site site = SiteConfiguration.FindExistingSiteByID(siteId);
         if (site == null)
         {
             Fault = new FaultContract();
             Fault.FaultType = "Error";
             Fault.Message = "Unable to find Site with ID = " + siteId;
             return null;
         }
         else
         {
             physicalPath = site.PhysicalPath;
             if (!string.IsNullOrEmpty(relativePath))
                 physicalPath += "\\" + relativePath.Replace("/", "\\");
         }
     }
     return physicalPath;
 }
 protected abstract Site UpgradeSite(string siteId, string templateId, out FaultContract fault);
        public bool DeleteSite(Site site, out FaultContract Fault)
        {
            Fault = null;
            string iisLocation = "";
            string innerStatus = "";

            try
            {
                iisLocation = IISHelper.GetIISLocation(site.IISHost, String.Format(":{0}:", site.IISPort), out innerStatus);
                if (!string.IsNullOrEmpty(iisLocation))
                {
                    string siteName = site.IISPath; //of the format Root/Folder/SubFolder
                    if (IISHelper.DeleteVirtualDirectory(iisLocation + "/Root", siteName, out innerStatus))
                    {
                        string iisPhysicalPath = "";
                        string websitePhysPath = "";

                        siteName = siteName.Replace("/", "\\");
                        if (IISHelper.GetPhysicalPath(iisLocation + "/Root", out iisPhysicalPath, out innerStatus))
                        {
                            websitePhysPath = iisPhysicalPath + "\\" + siteName;
                            websitePhysPath = ConvertToUNCPath(site.IISHost, websitePhysPath);
                            try
                            {
                                if (Directory.Exists(websitePhysPath))
                                {
                                    Directory.Delete(websitePhysPath, true);
                                }
                            }
                            catch (IOException)
                            {
                                // Sometimes the delete operations fail on first attempt on IIS 7 machines
                                // So retry once more
                                try
                                {
                                    if (Directory.Exists(websitePhysPath))
                                    {
                                        Directory.Delete(websitePhysPath, true);
                                    }
                                }
                                catch (IOException ioe)
                                {
                                    Fault = new FaultContract
                                    {
                                        FaultType = "Error",
                                        Message = "Error deleting directory " + ioe.ToString()
                                    };
                                    return false;
                                }
                            }
                        }
                    }
                    else
                    {                        
                        return false;
                    }
                }
                else
                {                    
                    return false;
                }
            }            
            catch (Exception)
            {   
                return false;
            }            
            return true;
        }
 public IIsWebsiteInfo[] GetWebSitesOnIIsWebServer(string server, out FaultContract fault)
 {            
     fault = null;
     try
     {
         return IISHelper.GetWebsitesOnIISServer(server).ToArray();
     }
     catch (Exception ex)
     {
         fault = new FaultContract
         {
             Message = ex.ToString(),
             FaultType = "Error retrieving websites on " + server
         };
         return null;
     }
 }
 public string GetIISLocation(string server, int port, out FaultContract Fault)
 {
     Fault = null;
     string status;
     string iisLocation = IISHelper.GetIISLocation(server, string.Format(":{0}:", port), out status);
     if (!string.IsNullOrEmpty(status))
     {
         Fault = new FaultContract() { 
              FaultType = "Error",
               Message = "Unable to retrieve IIS location",
         };
         return null;
     }
     return iisLocation;
 }
        public string GetPhysicalPathForSite(Site site, bool makeUNCPath, out FaultContract Fault)
        {
            Fault = null;
            string status = string.Empty;
            string iisLocation = string.Empty;
            // Get the location of the the website running at this host and that port .. as Active Directory entry
            iisLocation = IISHelper.GetIISLocation(site.IISHost, String.Format(":{0}:", site.IISPort), out status);
            if (!string.IsNullOrEmpty(status))
            {
                Fault = new FaultContract
                {
                    FaultType = "Error",
                    Message = "Unable to retrieve IIS location for " + site.IISHost + " Port " + site.IISPort
                };
                return null;
            }
            string iisPhysicalPath = string.Empty;
            if (!string.IsNullOrEmpty(iisLocation))
            {
                string websitePhysicalPath = string.Empty;

                // Check if we can query IIS
                string pathToQuery = iisLocation + "/Root";

                // get the physical location of IIS on the server
                IISHelper.GetPhysicalPath(iisLocation + "/Root", out iisPhysicalPath, out status);
                if (string.IsNullOrEmpty(iisPhysicalPath))
                {
                    Fault = new FaultContract
                    {
                        FaultType = "Error",
                        Message = "Unable to retrieve IIS location for " + site.IISHost + " Port " + site.IISPort
                    };
                    return null;    
                }
                if (!iisPhysicalPath.EndsWith("\\", StringComparison.Ordinal))
                    iisPhysicalPath = iisPhysicalPath + "\\";

                string virtualDir = site.IISPath;
                virtualDir = virtualDir.Replace("/", "\\"); // replace forward slashes to backward slashes for path

                if (isRemoteServer(site.IISHost) && makeUNCPath)
                {
                    // We need a UNC path ... eg:- \\maestro\C$\Inetput\wwwroot
                    iisPhysicalPath = "\\\\" + site.IISHost + "\\" + iisPhysicalPath.Replace(':', '$');
                    websitePhysicalPath = iisPhysicalPath + virtualDir;
                }
                else
                {
                    websitePhysicalPath = Path.Combine(iisPhysicalPath, virtualDir);
                }

                return websitePhysicalPath;
            }
            else
            {
                Fault = new FaultContract
                {
                    FaultType = "Error",
                    Message = "Unable to retrieve IIS location for " + site.IISHost + " Port " + site.IISPort
                };
                return null;                
            }            
        }
 public string RenameSite(Site site, string oldSiteName, string newSiteName, out FaultContract Fault)
 {
     Fault = null;
     string status = string.Empty;
     string newSitePhysPath = string.Empty;
     if (CopyWebApplication(site, oldSiteName, newSiteName, out newSitePhysPath, out status, true))
         return newSitePhysPath;
     else
     {
         Fault = new FaultContract
         {
             FaultType = "Error",
             Message = "Error renaming site " + status
         };
         return null;
     }
 }
 public static bool UploadTextFileContents(string filePhysicalPath, string fileContents, out FaultContract Fault)
 {
     Fault = null;
     if (!File.Exists(filePhysicalPath))
     {
         Fault = new FaultContract { FaultType = "Error", Message = "File doesn't exist" + filePhysicalPath };
         return false;
     }
     try
     {
         using (FileStream stream = new FileStream(filePhysicalPath, FileMode.Truncate))
         {
             using (StreamWriter writer = new StreamWriter(stream))
             {
                 writer.Write(fileContents);
                 writer.Flush();
                 writer.Close();
             }
         }
         return true;
     }
     catch (Exception ex)
     {
         Fault = new FaultContract { FaultType = "Error", Message = "Error writing file:- " + ex.Message };
     }
     return true;
 }
 public static void UploadFile(string filePath, byte[] bytes, out FaultContract Fault)
 {
     Fault = null;
     if (string.IsNullOrEmpty(filePath))
     {
         Fault = new FaultContract
         {
             FaultType = "Error",
             Message = "Must enter a valid file path"
         };
         return;
     }
     try
     {
         if (bytes == null || bytes.Length > 0)
         {
             using (System.IO.FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
             {
                 fs.Write(bytes, 0, bytes.Length);
                 fs.Close();
             }
         }
         else
         {
             Fault = new FaultContract { FaultType = "Warning", Message = "Empty bytes for file" };
         }
     }
     catch (Exception ex)
     {
         Fault = new FaultContract { FaultType = "Error", Message = ex.Message };
     }
 }