Share via


Adding a Schedule Using C#

The following is a C# code example that adds a new schedule called "Extended Work Hours," which includes all hours on all days. This code was written as a Windows Console Application. To use this code:

  1. In Visual Studio 2008 or Visual Studio 2005, create a new Visual C# console application.

  2. In Name, provide a name for your project, in Location, modify the path to your project as desired, and then click OK.

  3. On the Project menu, click Add Reference and add a reference to mspfccom.interop.dll.

  4. Copy the following code into the .cs source file and build the project.

    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using Microsoft.Isa.Interop;
    
    namespace CsAddSchedule
    {
        class AddSchedule
        {
            const string ScheduleName = "Extended Work Hours";
            const Int32 Error_AlreadyExists = -2147024713; // 0x800700B7
            private static FPC m_Root;
            private static FPCSchedules m_Schedules;
            private static FPCSchedule m_Schedule;
            static void Main(string[] args)
            {
                m_Root = new FPC();
                // Retrieve the schedules collection.
                m_Schedules = m_Root.GetContainingArray().RuleElements.Schedules;
                // Create the new schedule.
                try
                {
                    m_Schedule = m_Schedules.Add(ScheduleName);
                }
                catch (COMException comEx)
                {
                    if (comEx.ErrorCode == Error_AlreadyExists)
                    {
                        m_Schedule = m_Schedules.Item(ScheduleName);
                        Console.WriteLine("Using the existing schedule");
                    }
                }
                // Set the schedule.
                m_Schedule.Set(FpcScheduleDays.fpcALL_WEEK, FpcScheduleHours.fpcALL_DAY);
                // Save the changes.
                m_Schedules.Save(false, true);
                Console.WriteLine("\r\nDone!");
            }
        }
    }
    
  5. Run the application.

See Also

FPCSchedule

Send comments about this topic to Microsoft

Build date: 6/30/2010