public override void CreateVirtualDirectory(WebProject project)
        {
            string error       = null;
            var    virtualRoot = new IISVirtualRoot();

            virtualRoot.Create(IIS_WEB_LOCATION,
                               project.Directory,
                               project.Name,
                               out error);
            if (!String.IsNullOrEmpty(error))
            {
                throw new ApplicationException(error);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a virtual directory in local IIS or IIS Express.
        /// </summary>
        /// <param name="virtualDirectoryName">Virtual directory name.</param>
        /// <param name="virtualDirectoryPath">Physical path.</param>
        /// <returns></returns>
        public static string CreateVirtualDirectory(string virtualDirectoryName, string physicalDirectoryPath)
        {
            try {
                if (!IsIISInstalled)
                {
                    return(ResourceService.GetString("ICSharpCode.WepProjectOptionsPanel.IISNotFound"));
                }

                string error;

                switch (IISVersion)
                {
                case IISVersion.IIS5:
                case IISVersion.IIS6:
                    var vr = new IISVirtualRoot();
                    vr.Create(IIS_WEB_LOCATION,
                              physicalDirectoryPath,
                              virtualDirectoryName,
                              out error);
                    break;

                default:
                    // TODO: find a better way to create IIS 7 applications without Microsoft.Web.Administration.ServerManager
                    string name = "/" + virtualDirectoryName;
                    // load from GAC - IIS7 is installed
                    Assembly webAdministrationAssembly;
                    try {
                        // iis 7
                        webAdministrationAssembly = Assembly.Load("Microsoft.Web.Administration, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
                    }
                    catch {
                        // iis express
                        webAdministrationAssembly = Assembly.Load("Microsoft.Web.Administration, Version=7.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
                    }

                    // use dynamic because classic reflection is way TOO ugly
                    dynamic manager = webAdministrationAssembly.CreateInstance("Microsoft.Web.Administration.ServerManager");

                    if (manager.Sites[DEFAULT_WEB_SITE] != null)
                    {
                        if (manager.Sites[DEFAULT_WEB_SITE].Applications[name] == null)
                        {
                            manager.Sites[DEFAULT_WEB_SITE].Applications.Add(name, physicalDirectoryPath);
                            manager.CommitChanges();
                            error = string.Empty;
                        }
                        else
                        {
                            error = ResourceService.GetString("ICSharpCode.WepProjectOptionsPanel.ApplicationExists");
                        }
                    }
                    else
                    {
                        if (manager.Sites[0].Applications[name] == null)
                        {
                            manager.Sites[0].Applications.Add(name, physicalDirectoryPath);
                            manager.CommitChanges();
                            error = string.Empty;
                        }
                        else
                        {
                            error = ResourceService.GetString("ICSharpCode.WepProjectOptionsPanel.ApplicationExists");
                        }
                    }
                    manager.Dispose();
                    break;
                }

                return(error);
            }
            catch (Exception ex) {
                return(ex.Message);
            }
        }