zoukankan      html  css  js  c++  java
  • C# 管理IIS7(转)

      最先我需要在IIS下创建虚拟目录,用的是DirecotryEntry这个类,怎么也不能,总会报![System.Runtime.InteropServices.COMException]{"未知错误(0x80005000)"}

      这个错误。

    privatestaticvoidTestDirectoryEntry()
    {
      try
      {
        stringpath="IIsWebService://"+System.Environment.MachineName+"/W3SVC";
        System.Collections.ArrayListwebSite=newSystem.Collections.ArrayList();
        DirectoryEntryiis=newDirectoryEntry("IIS://localhost/W3SVC");
        if(iis!=null)
        {
          foreach(DirectoryEntryentryiniis.Children)
          {
            if(string.Compare(entry.SchemaClassName,"IIsWebServer")==0)
             {
               Console.WriteLine(entry.Name);
               Console.WriteLine(entry.Properties["ServerComment"].Value.ToString());
                Console.WriteLine(entry.Path);
             }
            Console.WriteLine();        }      }    }    catch(Exceptionex)    {       Console.WriteLine(ex.Message);       Console.WriteLine(ex.StackTrace);    } }

      后来疯狂地找原因

      第一步,找机子上安全方面的问题。我IIS7是装在Windows server2008上的。我取消了系统的UAC。并用“以管理员身份”运行的该程序。结果还是不行。

      第二步,在网上找有没有人跟我碰到相同的问题。果然!很多人都有,网上给的解决方案是:

      The IIS Metabase and IIS6 Configuration Compatibility is not automatically installed when you enable the Web Server role in Windows 2008 Server. If you enable this feature, your old DirectoryServices code in .NET should work like it used to.

      需要安装 IIS6 Metabase 兼容性组件

      用了这种方案果然成功了!但事情并没有结束。

      第三步:找原因,为什么IIS7 不能用这种方法!

      功夫不负有心人!

      IIS7 是没有元数据的。哎~~这就是根本原因,大家可以到C:WINDOWSsystem32inetsrv这个目录看看,IIS6的和IIS7的文件不同的。

      所以后来找到这种方法:

      (http://dflying.cnblogs.com/archive/2006/04/17/377276.html)

      Microsoft中提供了管理IIS7的一些非常强大的API——Microsoft.Web.Administration,可以很方便的让我们以编程的方式管理,设定IIS 7的各项配置。Microsoft.Web.Administration.dll位于IIS的目录(%WinDir%System32InetSrv)下,在项目中添加对其的引用后您就可以使用这些API了。下图显示了Microsoft.Web.Administration.dll中的主要对象。

    C# 管理IIS7

      让我们通过几个例子来使用Microsoft.Web.Administration,下面的例子均非常易懂,我就不再过多解释了。

      建立一个站点(Site)

    ServerManageriisManager=new ServerManager();
    iisManager.Sites.Add("NewSite","http","*:8080:","d:MySite");
    iisManager.Update();

      将一个应用程序(Application)添加到一个站点

    ServerManageriisManager=new ServerManager();
    iisManager.Sites["NewSite"].Applications.Add("/Sales","d:MyApp");
    iisManager.Update();

      建立一个虚拟目录(Virtual Directory)

    ServerManageriisManager=new ServerManager();
    Applicationapp=iisManager.Sites["NewSite"].Applications["/Sales"];
    app.VirtualDirectories.Add("/VDir","d:MyVDir");
    iisManager.Update();

      运行时控制:停止一个站点

    ServerManageriisManager=new ServerManager();
    iisManager.Sites["NewSite"].Stop();

      运行时控制:回收应用程序池(Recyciling an Application Pool)

    ServerManageriisManager=new ServerManager();
    iisManager.ApplicationPools["DefaultAppPool"].Recycle();

      运行时控制:得到当前正在处理的请求

    ServerManageriisManager=new ServerManager();
    foreach(WorkerProcessw3wpiniisManager.WorkerProcesses)
    {
      Console.WriteLine("W3WP({0})",w3wp.ProcessId);
      foreach(Requestrequestinw3wp.GetRequests(0))
      {
        Console.WriteLine("{0}-{1},{2},{3}",
        request.Url,
        request.ClientIPAddr,
        request.TimeElapsed,
        request.TimeInState);
      }
    }

       OK,问题从根本解决了,希望能帮助遇到同样问题的你!

  • 相关阅读:
    hdu 3335 Divisibility
    最小点覆盖,最小路径覆盖
    hdu 4109 Instrction Arrangement
    sjtu 1077 加分二叉树
    hdu 1542 Atlantis
    多线程中互斥体
    在子页面中,javascript让模板页中添加的用户控件中的控件选中focus
    模板页中用javascript判断是否为空
    控件包含代码块(即 <% ... %>),因此无法修改控件集合 asp.net
    lambda从指定集合中去除指定数据 asp.net
  • 原文地址:https://www.cnblogs.com/netserver/p/1368423.html
Copyright © 2011-2022 走看看