Goodspeed

导航

用ASP.net创建网站

using System;
using System.Drawing;
using System.Runtime.InteropServices;

class MainClass
{
 [DllImport("user32.dll", EntryPoint="GetWindowDC")]
 public static extern IntPtr GetWindowDC(IntPtr hwnd);

 [DllImport("user32.dll", EntryPoint="GetDesktopWindow")]
 public static extern IntPtr GetDesktopWindow();

 public static void Main(string[] args)
 {
  Graphics newGraphics = Graphics.FromHdc(GetWindowDC(GetDesktopWindow()));
  FontFamily[] families = FontFamily.GetFamilies(newGraphics);
  foreach (FontFamily family in families)
  {
   System.Console.WriteLine(family.Name);
  }
 }
}

 

  private static int GetNextOpenID()
  {
   DirectoryEntry iisComputer = new DirectoryEntry("IIS://localhost/w3svc");
   int nextID = 0;
   foreach( DirectoryEntry iisWebServer in iisComputer.Children )
   {
    string sname = iisWebServer.Name;
    try
    {
     int name = int.Parse(sname);
     if( name > nextID )
     {
      nextID = name;
     }
    }
    catch
    {
    }
   }
   return ++nextID;
  }

  public static void CreateWebSite(string name, string path, string
description, int port, bool makeActive)
  {
   CreateWebSite( name, path, description, port, makeActive, null, null );
  }

  public static void CreateWebSite(string name, string path, string
description, int port, bool makeActive, string UserName, string Password)
  {
   try
   {
    DirectoryEntry iisObject = new DirectoryEntry("IIS://localhost/w3svc");
    int id = GetNextOpenID();

    DirectoryEntry webSite = (DirectoryEntry)iisObject.Invoke( "Create", new
object[]{"IIsWebServer",id} );
    webSite.Properties["ServerComment"][0] = name;
    webSite.Properties["ServerAutoStart"][0] = makeActive;
    string prt = ":" + port.ToString() + ":";
    webSite.Properties["ServerBindings"].Add( prt );
    webSite.CommitChanges();

    // Create new IIS virtual directory
    DirectoryEntry virtualDirectory = (DirectoryEntry)webSite.Invoke(
"Create", new object[]{"IIsWebVirtualDir", "Root"} );
    virtualDirectory.CommitChanges();

    // Set common properties
    if( UserName != null )
    {
     virtualDirectory.Properties["AnonymousUserName"][0] = UserName;
     virtualDirectory.Properties["AnonymousUserPass"][0] = Password;
    }
    virtualDirectory.Properties["AccessExecute"][0] = true;
    virtualDirectory.Properties["EnableDefaultDoc"][0] = true;
    virtualDirectory.Properties["DefaultDoc"][0] = "default.aspx";
    Directory.Exists( path );
    virtualDirectory.Properties["Path"][0] = path;
    virtualDirectory.CommitChanges();

    // set medium (pooled) application protection
    virtualDirectory.Invoke("AppCreate2", new object[] {2});

    // Commit the property/children changes to the virtual directory
    virtualDirectory.Properties["AppFriendlyName"][0] = name;
    virtualDirectory.CommitChanges();

    if( makeActive )
    {
     try
     {
      webSite.Invoke( "Start", new object[]{} );
     }
     catch
     {
      Trace.WriteLine("Error trying to start website.  This is often caused
by creating a website on a port that is already in use.");
      MessageBox.Show("Error trying to start website.  This is often caused
by creating a website on a port that is already in use.", "Error" );
     }
    }

   }
   catch(Exception ex)
   {
    Trace.WriteLine( ex.Message );
    MessageBox.Show( "创建网站出错!", "Error");
   }
  }

posted on 2004-04-22 19:24  Goodspeed  阅读(1017)  评论(2)    收藏  举报