Asp tutorial. net program IIS automatic deployment
To create a virtual directory, follow these steps:
1) obtain the DirectoryEntry object rootEntry in the parent directory of the virtual directory;
2) Add the virtual directory through DirectoryEntry: Childrens. Add of rootEntry;
// Create a virtual directory
DirectoryEntryentry = rootEntry. Children. Add (this. m_strAlias, "IIsWebVirtualDir ");
3) update the attributes of the virtual directory, such as updating the authentication mode, access permissions, and the corresponding physical directory. Note that you can use DirectoryEntry to create a virtual directory. You can set properties such as physical directories only after the virtual directory is created.
The code for creating a virtual directory is as follows:
/// <Summary>
/// Create an iis virtual directory
/// </Summary>
/// <Ti> the virtual directory operation is abnormal. </exception>
Publicoverrisponidcreatedirectory ()
{
// Creates a virtual directory without overwriting. An exception is thrown when the virtual directory exists.
This. CreateDirectory (false );
}
/// <Summary>
/// Create an iis virtual directory
/// </Summary>
/// <Paramname = "bReplace"> overwrite the original virtual directory </param>
/// <Ti> the virtual directory operation is abnormal. </exception>
Publicoverrisponidcreatedirectory (boolbReplace)
{
// Determine whether the directory exists
If (this. Exist ())
{
If (bReplace)
{
// If overwrite is allowed, the original virtual directory is deleted first.
This. DeleteDirectory ();
}
Else
{
// If overwrite is not allowed, an existing directory exception is thrown directly.
DirectoryException. Throw ("directoryalreadyexist ");
}
}
Try
{
// Obtain the DirectoryEntry object of the parent directory
DirectoryEntryrootEntry = SystemDirectory. GetParentEntry (this );
// Create a virtual directory
DirectoryEntryentry = rootEntry. Children. Add (this. m_strAlias, "IIsWebVirtualDir ");
Entry. Invoke ("AppCreate", true );
Entry. CommitChanges ();
RootEntry. CommitChanges ();
// Update virtual directory properties
SystemDirectory. UpdateEntry (entry, this. Property );
}
Catch (System. Exceptionex)
{
DirectoryException. Throw (ex. Message );
}
}
The commitChanges method of DirectoryEntry is used to submit the operation of DirectoryEntry. After the virtual directory is submitted in line41, the virtual directory can be created and then updated. If you do not have commitChanges, the update operation will throw an exception that cannot find the virtual directory. If you are interested, try it.
2. Delete a virtual directory:
Deleting a virtual directory is simple. You only need to call the Delete operation on the DirectoryEntry object of the parent node of the virtual directory. Note that the Delete object requires two parameters: the Alias and subnode types of the subnode (the type of the virtual directory node is IIsWebVirtualDir ).
/// <Summary>
/// Delete the iis virtual directory
/// </Summary>
/// <Ti> the virtual directory operation is abnormal. </exception>
Publicoverrisponiddeletedirectory ()
{
// Determine whether the directory exists
If (! This. Exist ())
{
// If the virtual directory to be deleted does not exist, an exception is thrown.
DirectoryException. Throw ("directorydoesnotexist ");
}
Try
{
// Obtain the DirectoryEntry object of the parent directory
DirectoryEntryrootEntry = SystemDirectory. GetParentEntry (this );
// Delete parameters
Object [] objParams = newobject [2];
ObjParams [0] = "IIsWebVirtualDir ";
ObjParams [1] = this. m_strAlias;
// Delete a virtual directory
RootEntry. Invoke ("Delete", objParams );
RootEntry. CommitChanges ();
}
Catch (System. Exceptionex)
{
DirectoryException. Throw (ex. Message );
}
}