public static ReplicationConnection FindByName(DirectoryContext context, string name)
        {
            ReplicationConnection replicationConnection;

            ReplicationConnection.ValidateArgument(context, name);
            context = new DirectoryContext(context);
            DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);

            try
            {
                string propertyValue = (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.ServerName);
                string str           = string.Concat("CN=NTDS Settings,", propertyValue);
                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, str);
                string[] strArrays = new string[1];
                strArrays[0] = "distinguishedName";
                ADSearcher   aDSearcher   = new ADSearcher(directoryEntry, string.Concat("(&(objectClass=nTDSConnection)(objectCategory=NTDSConnection)(name=", Utils.GetEscapedFilterValue(name), "))"), strArrays, SearchScope.OneLevel, false, false);
                SearchResult searchResult = null;
                try
                {
                    searchResult = aDSearcher.FindOne();
                }
                catch (COMException cOMException1)
                {
                    COMException cOMException = cOMException1;
                    if (cOMException.ErrorCode != -2147016656)
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
                    }
                    else
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ReplicationConnection), name);
                    }
                }
                if (searchResult != null)
                {
                    DirectoryEntry directoryEntry1 = searchResult.GetDirectoryEntry();
                    replicationConnection = new ReplicationConnection(context, directoryEntry1, name);
                }
                else
                {
                    Exception activeDirectoryObjectNotFoundException = new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ReplicationConnection), name);
                    throw activeDirectoryObjectNotFoundException;
                }
            }
            finally
            {
                directoryEntry.Dispose();
            }
            return(replicationConnection);
        }
 public ReplicationConnection(DirectoryContext context, string name, DirectoryServer sourceServer, ActiveDirectorySchedule schedule, ActiveDirectoryTransportType transport)
 {
     ReplicationConnection.ValidateArgument(context, name);
     if (sourceServer != null)
     {
         if (transport < ActiveDirectoryTransportType.Rpc || transport > ActiveDirectoryTransportType.Smtp)
         {
             throw new InvalidEnumArgumentException("value", (int)transport, typeof(ActiveDirectoryTransportType));
         }
         else
         {
             context = new DirectoryContext(context);
             this.ValidateTargetAndSourceServer(context, sourceServer);
             this.context        = context;
             this.connectionName = name;
             this.transport      = transport;
             DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
             try
             {
                 try
                 {
                     string propertyValue = (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.ServerName);
                     string str           = string.Concat("CN=NTDS Settings,", propertyValue);
                     directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, str);
                     string escapedPath = string.Concat("cn=", this.connectionName);
                     escapedPath = Utils.GetEscapedPath(escapedPath);
                     this.cachedDirectoryEntry = directoryEntry.Children.Add(escapedPath, "nTDSConnection");
                     DirectoryContext directoryContext = sourceServer.Context;
                     directoryEntry = DirectoryEntryManager.GetDirectoryEntry(directoryContext, WellKnownDN.RootDSE);
                     string propertyValue1 = (string)PropertyManager.GetPropertyValue(directoryContext, directoryEntry, PropertyManager.ServerName);
                     propertyValue1 = string.Concat("CN=NTDS Settings,", propertyValue1);
                     this.cachedDirectoryEntry.Properties["fromServer"].Add(propertyValue1);
                     if (schedule != null)
                     {
                         this.cachedDirectoryEntry.Properties["schedule"].Value = schedule.GetUnmanagedSchedule();
                     }
                     string dNFromTransportType = Utils.GetDNFromTransportType(this.TransportType, context);
                     directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, dNFromTransportType);
                     try
                     {
                         //TODO: REVIEW: URGENT!!: directoryEntry.Bind(true);
                     }
                     catch (COMException cOMException1)
                     {
                         COMException cOMException = cOMException1;
                         if (cOMException.ErrorCode == -2147016656)
                         {
                             DirectoryEntry directoryEntry1 = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                             if (Utils.CheckCapability(directoryEntry1, Capability.ActiveDirectoryApplicationMode) && transport == ActiveDirectoryTransportType.Smtp)
                             {
                                 throw new NotSupportedException(Res.GetString("NotSupportTransportSMTP"));
                             }
                         }
                         throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
                     }
                     this.cachedDirectoryEntry.Properties["transportType"].Add(dNFromTransportType);
                     this.cachedDirectoryEntry.Properties["enabledConnection"].Value = false;
                     this.cachedDirectoryEntry.Properties["options"].Value           = 0;
                 }
                 catch (COMException cOMException3)
                 {
                     COMException cOMException2 = cOMException3;
                     throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException2);
                 }
             }
             finally
             {
                 directoryEntry.Close();
             }
             return;
         }
     }
     else
     {
         throw new ArgumentNullException("sourceServer");
     }
 }