/// <summary>
 /// creates a feature class in the default database given a map server lyr id and ftrName
 /// </summary>
 /// <param name="ms2">mapservice</param>
 /// <param name="lyrId">layer id of the feature on the map service</param>
 /// <param name="ftrName">the name of the new map service</param>
 /// <returns>the feature class created</returns>
 private IFeatureClass createFtrClass(IMapServer2 ms2,int lyrId,string ftrName)
 {
     Console.WriteLine(ftrName);
     string mapN = ms2.DefaultMapName;
     IMapServerInfo2  msInfo2 = (IMapServerInfo2)ms2.GetServerInfo(mapN);
     IMapLayerInfos mLyrInfos = msInfo2.MapLayerInfos;
     IMapLayerInfo2 mLyrInfo2 = (IMapLayerInfo2)mLyrInfos.get_Element(lyrId);
     IFields flds = mLyrInfo2.Fields;
     IFields fldsClone = (IFields)((IClone)flds).Clone();
     UID CLSID = new UIDClass();
     CLSID.Value = "esriGeoDatabase.Feature";
     string sFld = "SHAPE";
     string oFld = "OBJECTID";
     bool fOid = false;
     bool fshape = false;
     for(int i=0;i<flds.FieldCount;i++)
     {
         IField fld = flds.get_Field(i);
         esriFieldType fType = fld.Type;
         if(fType== esriFieldType.esriFieldTypeGeometry)
         {
             sFld = fld.Name;
             fshape = true;
         }
         else if (fType==esriFieldType.esriFieldTypeOID)
         {
             oFld = fld.Name;
             fOid = true;
         }
         else
         {
         }
         if(fshape&&fOid)
         {
             break;
         }
     }
     if (fshape && fOid)
     {
         return fWks.CreateFeatureClass(ftrName, fldsClone, CLSID, null, esriFeatureType.esriFTSimple, sFld, "");
     }
     else
     {
         return null;
     }
 }
        /// <summary>
        /// return the layers from a mapservice
        /// </summary>
        /// <param name="ms2">IMapServer2 object</param>
        /// <returns>a dictionary of service name and its corresponding id</returns>
        public Dictionary<string, int> getLayers(IMapServer2 ms2)
        {
            Dictionary<string, int> lyrDic = new Dictionary<string, int>();
            int maxR = getMaxRecords(ms2);
            if(maxR<1)
            {
                return lyrDic;
            }
            esriUtil.Forms.RunningProcess.frmRunningProcessDialog rp = new Forms.RunningProcess.frmRunningProcessDialog(false);
            try
            {
                rp.TopMost = true;
                rp.addMessage("Looking for eligible layers...");
                rp.addMessage("Please be patient...");
                rp.Show();
                rp.Refresh();
                for (int m = 0; m < ms2.MapCount; m++)
                {
                    rp.addMessage("Searching Map " + m.ToString());
                    rp.stepPGBar(5);
                    rp.Refresh();
                    string mName = ms2.get_MapName(m);
                    IMapServerInfo msInfo = ms2.GetServerInfo(mName);
                    IMapLayerInfos mLyrInfos = msInfo.MapLayerInfos;
                    for (int j = 0; j < mLyrInfos.Count; j++)
                    {
                        IMapLayerInfo mLyrInfo = mLyrInfos.get_Element(j);
                        bool ftrF = mLyrInfo.IsFeatureLayer;
                        bool ftrC = mLyrInfo.IsComposite;
                        bool ftrS = mLyrInfo.CanSelect;
                        bool geoField = false;
                        for (int i = 0; i < mLyrInfo.Fields.FieldCount; i++)
                        {
                            IField fld = mLyrInfo.Fields.get_Field(i);
                            if (fld.Type == esriFieldType.esriFieldTypeGeometry)
                            {
                                geoField = true;
                                break;
                            }
                        }
                        if (ftrF && ftrS && geoField && !ftrC)
                        {
                            rp.addMessage("\tFound layer" + mLyrInfo.Name);
                            rp.Refresh();
                            string lNm = mLyrInfo.Name;
                            int lyrId = mLyrInfo.ID;
                            if (!lyrDic.ContainsKey(lNm))
                            {
                                lyrDic.Add(lNm, lyrId);
                            }
                        }

                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                rp.stepPGBar(100);
                rp.enableClose();
                rp.Close();
            }
            return lyrDic;
        }
 private bool checkServerLayer(IMapServer2 ms2, int lyrID, string lyrName)
 {
     IMapServerInfo2 msInfo2 = (IMapServerInfo2)ms2.GetServerInfo(ms2.DefaultMapName);
     IMapLayerInfos msLyrInfos = msInfo2.MapLayerInfos;
     IMapLayerInfo msLyrInfo = msLyrInfos.get_Element(lyrID);
     string nm = msLyrInfo.Name;
     if (lyrName.ToLower() != nm.ToLower())
     {
         return false;
     }
     else
     {
         return true;
     }
 }