/// <summary> /// Deletes a virtual directory from IIS Server. /// </summary> /// <param name="dirName">The name of the directory to delete.</param> /// <param name="physicalPath">The physical path of the virtual directory.</param> public void DeleteVirtualDirectory(string dirName, string physicalPath) { //first attempt to delete it using EnterpriseServices try { System.EnterpriseServices.Internal.IISVirtualRoot vr = new System.EnterpriseServices.Internal.IISVirtualRoot(); string sError; vr.Delete("IIS://" + serverName + "/W3SVC/1/Root", physicalPath, dirName, out sError); if (sError == String.Empty) { //everything went ok, so we can return return; } } catch {} //it hasn't succeeded so we must try to delete it using DirectoryServices DirectoryEntry folderRoot = iisServer.Children.Find("Root", VirDirSchemaName); try { DirectoryEntry virDir = folderRoot.Children.Find(dirName, VirDirSchemaName); if (virDir != null) { folderRoot.Children.Remove(virDir); } folderRoot.CommitChanges(); iisServer.CommitChanges(); } catch (Exception e) { throw new Exception("Virtual Directory " + dirName + " does not exist or could not be deleted", e); } }
/// <summary> /// Creates a new virtual directory on IIS Server. /// </summary> /// <param name="dirName">The name of the directory to create.</param> /// <param name="appName">The name of the application to create.</param> /// <param name="physicalPath">The physical path of the virtual directory.</param> public void CreateVirtualDirectory(string dirName, string appName, string physicalPath) { //first attempt to build it using EnterpriseServices try { System.EnterpriseServices.Internal.IISVirtualRoot vr = new System.EnterpriseServices.Internal.IISVirtualRoot(); string sError; vr.Create("IIS://" + serverName + "/W3SVC/1/Root", physicalPath, dirName, out sError); if (sError == String.Empty) { //everything went ok, so we can return return; } } catch {} //it hasn't succeeded so we must try to create it using DirectoryServices DirectoryEntry folderRoot = iisServer.Children.Find("Root", VirDirSchemaName); try { DirectoryEntry newVirDir = folderRoot.Children.Add(dirName, VirDirSchemaName); // Set Properties newVirDir.Properties["AccessRead"].Add(true); newVirDir.Properties["Path"].Add(physicalPath); // Create an Application //newVirDir.Invoke("AppCreate",true); newVirDir.Properties["AppFriendlyName"][0] = appName; newVirDir.Properties["FrontPageWeb"][0] = 1; if (iisVersion == IISVersion.Four) { newVirDir.Invoke("AppCreate", true); } else { newVirDir.Invoke("AppCreate2", true); } // Save Changes newVirDir.CommitChanges(); folderRoot.CommitChanges(); iisServer.CommitChanges(); } catch (Exception e) { throw new Exception("Virtual Directory " + dirName + " Already Exists", e); } }
/// <summary> /// Deletes a virtual directory from IIS Server. /// </summary> /// <param name="dirName">The name of the directory to delete.</param> /// <param name="physicalPath">The physical path of the virtual directory.</param> public void DeleteVirtualDirectory(string dirName, string physicalPath) { //first attempt to delete it using EnterpriseServices try { System.EnterpriseServices.Internal.IISVirtualRoot vr = new System.EnterpriseServices.Internal.IISVirtualRoot(); string sError; vr.Delete("IIS://" + serverName + "/W3SVC/1/Root", physicalPath, dirName, out sError); if(sError==String.Empty) { //everything went ok, so we can return return; } } catch {} //it hasn't succeeded so we must try to delete it using DirectoryServices DirectoryEntry folderRoot = iisServer.Children.Find("Root",VirDirSchemaName); try { DirectoryEntry virDir = folderRoot.Children.Find(dirName,VirDirSchemaName); if(virDir != null) { folderRoot.Children.Remove(virDir); } folderRoot.CommitChanges(); iisServer.CommitChanges(); } catch (Exception e) { throw new Exception("Virtual Directory " + dirName + " does not exist or could not be deleted",e); } }
/// <summary> /// Creates a new virtual directory on IIS Server. /// </summary> /// <param name="dirName">The name of the directory to create.</param> /// <param name="appName">The name of the application to create.</param> /// <param name="physicalPath">The physical path of the virtual directory.</param> public void CreateVirtualDirectory(string dirName, string appName, string physicalPath) { //first attempt to build it using EnterpriseServices try { System.EnterpriseServices.Internal.IISVirtualRoot vr = new System.EnterpriseServices.Internal.IISVirtualRoot(); string sError; vr.Create("IIS://" + serverName + "/W3SVC/1/Root", physicalPath, dirName, out sError); if(sError==String.Empty) { //everything went ok, so we can return return; } } catch {} //it hasn't succeeded so we must try to create it using DirectoryServices DirectoryEntry folderRoot = iisServer.Children.Find("Root",VirDirSchemaName); try { DirectoryEntry newVirDir = folderRoot.Children.Add(dirName,VirDirSchemaName); // Set Properties newVirDir.Properties["AccessRead"].Add(true); newVirDir.Properties["Path"].Add(physicalPath); // Create an Application //newVirDir.Invoke("AppCreate",true); newVirDir.Properties["AppFriendlyName"][0] = appName; newVirDir.Properties["FrontPageWeb"][0] = 1; if(iisVersion == IISVersion.Four) { newVirDir.Invoke("AppCreate", true); } else { newVirDir.Invoke("AppCreate2", true); } // Save Changes newVirDir.CommitChanges(); folderRoot.CommitChanges(); iisServer.CommitChanges(); } catch (Exception e) { throw new Exception("Virtual Directory " + dirName + " Already Exists",e); } }