Published
Monday, May 29, 2006 9:43 PM
by
Aleph
Handy class if you plan to create sites from your .net application. There are xml comments on it, so if you need more details on how to use it let me know, I'll give you an example.
Here comes the code :)
using System;
using System.DirectoryServices;
using System.ServiceProcess;
/// <summary>
/// IIS Services Schema
/// </summary>
public enum Schema : int
{
/// <summary>MSFTPSVC</summary>
FTP = 1,
/// <summary>W3SVC</summary>
WEB = 2
}
/// <summary>
/// IIS Virtual Directory manager
/// </summary>
public class IISManager
{
private static string ftpSchema = "IIsFtpVirtualDir";
private static string ftpRoot = "/MSFTPSVC/";
private static string webSchema = "IIsWebVirtualDir";
private static string webRoot = "/W3SVC/";
private int serverId = 1;
private string serverName = "localhost";
private string root;
private Schema schema = Schema.WEB;
private DirectoryEntry deRoot;
private string errorMessage = string.Empty;
/// <summary>
/// Returns last error
/// </summary>
public string ErrorMessage
{
get { return this.errorMessage; }
}
/// <summary>
/// Default constructor,
/// IIS://localhost/W3SVC/1/Root
/// </summary>
public IISManager()
{
root = "IIS://" + this.serverName + webRoot + this.serverId + "/Root";
this.deRoot = new DirectoryEntry(this.root);
}
/// <summary>
/// Custom Constructor
/// </summary>
/// <param name="serverName">localhost</param>
/// <param name="serverId">1</param>
/// <param name="schema">WEB</param>
public IISManager(string serverName, int serverId, Schema schema)
{
this.serverName = serverName;
this.serverId = serverId;
this.schema = schema;
if (this.schema == Schema.FTP)
{
root = "IIS://" + this.serverName + ftpRoot + this.serverId + "/Root";
}
else
{
root = "IIS://" + this.serverName + webRoot + this.serverId + "/Root";
}
this.deRoot = new DirectoryEntry(this.root);
}
/// <summary>
/// Check for existing virtual directory
/// </summary>
/// <param name="name">virtual directory name</param>
/// <returns></returns>
public bool IsDirectory(string name)
{
try
{
return DirectoryEntry.Exists(root + "/" + name);
}
catch
{
return false;
}
}
/// <summary>
/// Create a virtual directory
/// </summary>
/// <param name="name">virtual directory name</param>
/// <param name="folderPath">local directory path</param>
/// <param name="isApplication">create application for web virtual directory</param>
/// <returns></returns>
public bool CreateDirectory(string name, string folderPath, bool isApplication)
{
if (IsDirectory(name))
{
this.errorMessage = "Directory " + name + " exist";
return false;
}
bool ok = true;
DirectoryEntry deNewVDir;
try
{
deRoot.RefreshCache();
if (this.schema == Schema.FTP)
{
deNewVDir = deRoot.Children.Add(name, ftpSchema);
}
else
{
deNewVDir = deRoot.Children.Add(name, webSchema);
}
deNewVDir.Properties["Path"].Insert(0, folderPath);
deNewVDir.CommitChanges();
deRoot.CommitChanges();
// Create a Web Application
if (schema == Schema.WEB)
{
deNewVDir.Invoke("AppCreate", isApplication);
}
// Save Changes
deNewVDir.CommitChanges();
deRoot.CommitChanges();
deNewVDir.Close();
deRoot.Close();
}
catch (Exception ex)
{
this.errorMessage = ex.Message;
ok = false;
}
return ok;
}
/// <summary>
/// Delete a virtual directory
/// </summary>
/// <param name="name">virtual directory name</param>
/// <returns></returns>
public bool DeleteDirectory(string name)
{
bool ok = true;
if (!IsDirectory(name))
{
this.errorMessage = "Directory " + name + " does not exist";
return false;
}
Object[] parameters;
try
{
if (this.schema == Schema.FTP)
{
parameters = new object[] { ftpSchema, name };
}
else
{
parameters = new object[] { webSchema, name };
}
deRoot.Invoke("Delete", parameters);
deRoot.CommitChanges();
deRoot.Close();
}
catch (Exception ex)
{
this.errorMessage = ex.Message;
ok = false;
}
return ok;
}
/// <summary>
/// Stop IIS Services
/// </summary>
/// <param name="schema">MSFTPSVC or W3SVC</param>
public static void StopIIS(Schema schema)
{
ServiceController myServiceController;
if (schema == Schema.FTP)
{
myServiceController = new ServiceController("MSFTPSVC");
}
else
{
myServiceController = new ServiceController("W3SVC");
}
if (null != myServiceController)
{
do
{
myServiceController.Refresh();
}
while
(myServiceController.Status == ServiceControllerStatus.ContinuePending ||
myServiceController.Status == ServiceControllerStatus.PausePending ||
myServiceController.Status == ServiceControllerStatus.StartPending ||
myServiceController.Status == ServiceControllerStatus.StopPending);
if (ServiceControllerStatus.Running == myServiceController.Status ||
ServiceControllerStatus.Paused == myServiceController.Status)
{
myServiceController.Stop();
myServiceController.WaitForStatus(ServiceControllerStatus.Stopped);
}
myServiceController.Close();
}
}
/// <summary>
/// Start IIS Services
/// </summary>
/// <param name="schema">MSFTPSVC or W3SVC</param>
public static void StartIIS(Schema schema)
{
ServiceController myServiceController;
if (schema == Schema.FTP)
{
myServiceController = new ServiceController("MSFTPSVC");
}
else
{
myServiceController = new ServiceController("W3SVC");
}
if (null != myServiceController)
{
do
{
myServiceController.Refresh();
}
while
(myServiceController.Status == ServiceControllerStatus.ContinuePending ||
myServiceController.Status == ServiceControllerStatus.PausePending ||
myServiceController.Status == ServiceControllerStatus.StartPending ||
myServiceController.Status == ServiceControllerStatus.StopPending);
if (ServiceControllerStatus.Stopped == myServiceController.Status)
{
myServiceController.Start();
myServiceController.WaitForStatus(ServiceControllerStatus.Running);
}
else
{
if (ServiceControllerStatus.Paused == myServiceController.Status)
{
myServiceController.Continue();
myServiceController.WaitForStatus(ServiceControllerStatus.Running);
}
}
myServiceController.Close();
}
}
}