// How to create a site link. Note, that this example assigns a single site to this site link
        public static void CreateSiteLink(string siteName, string siteLinkName)
        {
            try
            {
                DirectoryContext forestContext = new DirectoryContext(
                                            DirectoryContextType.Forest);

                //bind to a specific site in the forest
                ActiveDirectorySite site = ActiveDirectorySite.FindByName(
                                                                forestContext,
                                                                siteName);

                // rpc is the default transport type (smtp is the other,
                // this is how you can create a transport type variable to
                // later assign to the link
                ActiveDirectoryTransportType adTpt =
                                            ActiveDirectoryTransportType.Rpc;

                // using the overload requiring the transport type to
                // demonstrate how to assign a transport type to this link.
                ActiveDirectorySiteLink link = new ActiveDirectorySiteLink(
                                                    forestContext,
                                                    siteLinkName,
                                                    adTpt);

                // configure the ActiveDirectorySiteLink object
                link.Cost = 100;
                link.DataCompressionEnabled = true;

                // create an AD schedule object for setting intersite replication
                ActiveDirectorySchedule linkSchedule =
                                                new ActiveDirectorySchedule();

                // set the schedule for 5:30 am to 6:30 am
                linkSchedule.SetDailySchedule(HourOfDay.Zero,
                                              MinuteOfHour.Zero,
                                              HourOfDay.TwentyThree,
                                              MinuteOfHour.FortyFive);

                // set the schedule for 5:30 pm to 6:30 pm
                /*linkSchedule.SetDailySchedule(HourOfDay.Seventeen,
                                              MinuteOfHour.Thirty,
                                              HourOfDay.Eighteen,
                                              MinuteOfHour.Thirty);
                */
                // apply the replication schedule to the link
                link.InterSiteReplicationSchedule = linkSchedule;

                // enable inter-site change notification. Typically used for fast,
                // uncongested links between sites
                link.NotificationEnabled = true;

                // replicate every twelve hours
                TimeSpan linkTimeSpan = new TimeSpan(0, 15, 0);

                // assign the TimeSpan object to the ReplicationInterval property
                link.ReplicationInterval = linkTimeSpan;

                // configure the link so there is no reciprocal replication
                link.ReciprocalReplicationEnabled = false;

                // assign a site to this site link.
                link.Sites.Add(site);

                // commit the link to the directory
                link.Save();

                Console.BackgroundColor = ConsoleColor.Black;
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("\nLink \"{0}\" was created successfully", link.Name);
                Console.ResetColor();

            }
            catch (Exception e)
            {
                Console.WriteLine("\r\nUnexpected exception occured:\n\t{0}\n{1}",
                      e.GetType().Name, e.Message);

            }
        }
        public static void CreateNewConnection(string sourceServer, string targetServer, string connectionName)
        {
            try
            {

                // set a directory server context for the source server
                DirectoryContext sourceContext = new DirectoryContext(
                                                    DirectoryContextType.DirectoryServer,
                                                    sourceServer);

                // set a directory server context for the target server
                DirectoryContext targetContext = new DirectoryContext(
                                                    DirectoryContextType.DirectoryServer,
                                                    targetServer);

                // bind to a specific domain controller to serve as the
                // source of a replication connection
                DomainController sourceDc =
                                    DomainController.GetDomainController(sourceContext);

                ReplicationConnection connection = new ReplicationConnection(
                                                        targetContext,
                                                        connectionName,
                                                        sourceDc);

                // set change notification status
                connection.ChangeNotificationStatus = NotificationStatus.IntraSiteOnly;

                // create a customized replication schedule
                ActiveDirectorySchedule schedule = new ActiveDirectorySchedule();
                schedule.SetDailySchedule(HourOfDay.Twelve,
                                          MinuteOfHour.Zero,
                                          HourOfDay.Fifteen,
                                          MinuteOfHour.Zero);

                schedule.SetSchedule(DayOfWeek.Sunday,
                                     HourOfDay.Eight,
                                     MinuteOfHour.Zero,
                                     HourOfDay.Eleven,
                                     MinuteOfHour.Zero);

                schedule.SetSchedule(DayOfWeek.Saturday,
                                     HourOfDay.Seven,
                                     MinuteOfHour.Zero,
                                     HourOfDay.Ten,
                                     MinuteOfHour.Zero);

                connection.ReplicationSchedule = schedule;
                connection.ReplicationScheduleOwnedByUser = true;

                // save the new connection to the directory
                connection.Save();
                Console.WriteLine("\nNew replication connection created successfully\n" +
                  "from server {0} to {1}.\n The connection appears in the NTDS " +
                  "settings of {1}", sourceServer, targetServer);

            }
            catch (Exception e)
            {
                Console.WriteLine("\r\nUnexpected exception occured:\n{0}:{1}",
                                  e.GetType().Name, e.Message);
            }
        }