Ejemplo n.º 1
0
        // 查找 dp2OPAC 路径
        // return:
        //      -1  出错
        //      其他  返回找到的路径个数
        public static int GetOpacInfo(out List <OpacAppInfo> infos,
                                      out string strError)
        {
            strError = "";
            infos    = new List <OpacAppInfo>();

            Version version = OpacAppInfo.GetIisVersion();

            if (version.Major >= 7)
            {
                // 使用 appcmd 获得信息
                // appcmd list apps
                return(GetOpacInfoByAppCmd(out infos,
                                           out strError));
            }

            // 用 Active Directory 方法
            for (int i = 0; ; i++)
            {
                string strSitePath = "IIS://localhost/W3SVC/" + (i + 1);

                try
                {
                    if (DirectoryEntry.Exists(strSitePath) == false)
                    {
                        break;
                    }
                }
                catch (COMException ex)
                {
                    if ((uint)ex.ErrorCode == 0x80005000)
                    {
                        strError = "IIS 尚未启用";
                        return(-1);
                    }
                }

                string strRootPath = strSitePath + "/ROOT";
                if (DirectoryEntry.Exists(strRootPath) == false)
                {
                    continue;
                }

                DirectoryEntry entry = new DirectoryEntry(strRootPath);

                foreach (DirectoryEntry child in entry.Children)
                {
                    if (child.SchemaClassName != "IIsWebVirtualDir")
                    {
                        continue;
                    }

                    OpacAppInfo info = new OpacAppInfo(child);

                    if (string.IsNullOrEmpty(info.PhysicalPath) == false && // 如果不是应用目录,则 PhysicalPath 可能为空
                        info.IsOPAC() == true)
                    {
                        infos.Add(info);
                    }
                }
            }

            return(infos.Count);
        }