zoukankan      html  css  js  c++  java
  • [转]C# 管理IIS7

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


    private static void TestDirectoryEntry()
            {
                
    try
                {
                    
    string path = "IIsWebService://" + System.Environment.MachineName + "/W3SVC";
                    System.Collections.ArrayList webSite 
    = new System.Collections.ArrayList();
                    DirectoryEntry iis 
    = new DirectoryEntry("IIS://localhost/W3SVC");
                    
    if (iis != null)
                    {
                        
    foreach (DirectoryEntry entry in iis.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 (Exception ex)
                {
                    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:\WINDOWS\system32\inetsrv这个目录看看,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%\System32\InetSrv)下,在项目中添加对其的引用后您就可以使用这些API了。下图显示了Microsoft.Web.Administration.dll中的主要对象。


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

    建立一个站点(Site)

     

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

     

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

     

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

     

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

     

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

     

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

     

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

     

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

     

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

     

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

     

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

     

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

  • 相关阅读:
    最新闲鱼数据采集软件【2020年4月更新】
    拼多多改价精灵
    idhttp采集时遇到乱码问题解决
    拼多多店铺采集如何采集?【爬虫技术】
    【原创最新2018】淘宝如何获取别人店铺宝贝的上下架时间?
    android.support.v4.app.NotificationCompat引用包
    Delphi 7启动后提示Unable to rename delphi32.dro的解决办法
    NOIP 膜你题 DAY2
    NIOP 膜你题
    一个hin秀的小学三年级奥数题 [hin秀]
  • 原文地址:https://www.cnblogs.com/answercard/p/1983387.html
Copyright © 2011-2022 走看看