WEB
- 仮想ディレクトリの作成・削除
物理ディレクトリをIIS上の仮想ディレクトリにマッピングします。
仮想ディレクトリ作成はIISマネージャにより行いますがこれを自動化することが出来ます。
/// <summary>IISの既定のWEBサイトのROOTディレクトリを取得</summary>
/// <returns>IISの既定のWEBサイトのROOTディレクトリ</returns>
private static DirectoryEntry GetWebSiteRoot()
{
try
{
DirectoryEntry iisAdmin = new DirectoryEntry( "IIS://localhost/W3SVC" );
if( iisAdmin == null ) return null;
foreach( DirectoryEntry root in iisAdmin.Children )
{
if( root.SchemaClassName != "IIsWebServer" )
{
continue;
}
if( root.Properties[ "ServerComment" ][ 0 ].ToString() != "既定のWeb サイト" )
{
continue;
}
foreach( DirectoryEntry rootItem in root.Children )
{
if( rootItem.Name == "ROOT" )
{
return rootItem;
}
}
}
}
catch( Exception ex )
{
Console.WriteLine( ex.Message );
return null;
}
return null;
}
/// <summary>仮想ディレクトリ作成</summary>
/// <param name="szName">仮想ディレクトリ名</param>
/// <param name="szPhysicalPath">WEBアプリケーションの物理パス</param>
/// <returns><para>True: OK</para><para>False: NG</para></returns>
public static bool CreateVirtualDirectory( string szName, string szPhysicalPath )
{
try
{
DirectoryEntry root = GetWebSiteRoot();
if( root == null ) return false;
using( DirectoryEntry directoryEntry = root.Children.Add( szName, "IIsWebVirtualDir" ) )
{
directoryEntry.Properties[ "AccessRead" ][ 0 ] = true;
directoryEntry.Properties[ "AccessExecute" ][ 0 ] = true;
directoryEntry.Properties[ "AccessWrite" ][ 0 ] = true;
directoryEntry.Properties[ "AccessScript" ][ 0 ] = true;
directoryEntry.Properties[ "EnableDefaultDoc" ][ 0 ] = true;
directoryEntry.Properties[ "Path" ][ 0 ] = szPhysicalPath;
directoryEntry.CommitChanges();
directoryEntry.Invoke( "AppCreate", false );
directoryEntry.Properties[ "AppIsolated" ][ 0 ] = ( System.Int32 )2;
directoryEntry.CommitChanges();
}
}
catch( Exception ex )
{
Console.WriteLine( ex.Message );
return false;
}
return true;
}
/// <summary>仮想ディレクトリ削除</summary>
/// <param name="szName">仮想ディレクトリ名</param>
/// <returns><para>True: OK</para><para>False: NG</para></returns>
public static bool RemoveVirtualDirectory( string szName )
{
try
{
DirectoryEntry root = GetWebSiteRoot();
if( root == null ) return false;
DirectoryEntry found = root.Children.Find( szName, string.Empty );
if( found == null ) return false;
root.Children.Remove( found );
}
catch( Exception ex )
{
Console.WriteLine( ex.Message );
return false;
}
return true;
}