Created
April 27, 2021 19:14
-
-
Save aspose-com-gists/03f5fe501a33bd09b09fb5dc9210aa2f to your computer and use it in GitHub Desktop.
Create MS Project Files in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a project instance | |
Project project = new Project(); | |
// Define Calendar | |
Calendar cal = project.Calendars.Add("Calendar1"); | |
// Add working days monday through thursday with default timings | |
cal.WeekDays.Add(WeekDay.CreateDefaultWorkingDay(DayType.Monday)); | |
cal.WeekDays.Add(WeekDay.CreateDefaultWorkingDay(DayType.Tuesday)); | |
cal.WeekDays.Add(WeekDay.CreateDefaultWorkingDay(DayType.Wednesday)); | |
cal.WeekDays.Add(WeekDay.CreateDefaultWorkingDay(DayType.Thursday)); | |
cal.WeekDays.Add(new WeekDay(DayType.Saturday)); | |
cal.WeekDays.Add(new WeekDay(DayType.Sunday)); | |
// Set friday as short working day | |
WeekDay myWeekDay = new WeekDay(DayType.Friday); | |
// Sets working time. Only time part of DateTime is important | |
WorkingTime wt1 = new WorkingTime(); | |
wt1.FromTime = new DateTime(1, 1, 1, 9, 0, 0, 0); | |
wt1.ToTime = new DateTime(1, 1, 1, 12, 0, 0, 0); | |
WorkingTime wt2 = new WorkingTime(); | |
wt2.FromTime = new DateTime(1, 1, 1, 13, 0, 0, 0); | |
wt2.ToTime = new DateTime(1, 1, 1, 16, 0, 0, 0); | |
myWeekDay.WorkingTimes.Add(wt1); | |
myWeekDay.WorkingTimes.Add(wt2); | |
myWeekDay.DayWorking = true; | |
cal.WeekDays.Add(myWeekDay); | |
// Save project | |
project.Save("Project_DefineCalendarWeekdays_out.xml", SaveFileFormat.XML); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create project | |
Project project = new Project(); | |
// Add resources | |
Resource resource = project.Resources.Add("Rsc"); | |
// Save project | |
project.Save("CreateResources_out.xml", SaveFileFormat.XML); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create project | |
Project project = new Project(); | |
// Add task and set task properties | |
Task task = project.RootTask.Children.Add("Task1"); | |
task.Set(Tsk.Start, project.RootTask.Get(Tsk.Start).AddDays(1)); | |
task.Set(Tsk.Name, "new name"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create project | |
Project project = new Project("New Project.mpp") | |
{ | |
CalculationMode = CalculationMode.Automatic | |
}; | |
// Set properties | |
project.Set(Prj.DateFormat, DateFormat.DateDddMmDdYy); | |
project.Set(Prj.StartDate, new DateTime(2019, 9, 16, 9, 0, 0)); | |
project.Set(Prj.NewTasksAreManual, false); | |
project.Set(Prj.ActualsInSync, false); | |
// Add resources | |
Resource workResource = project.Resources.Add("Servente (Work)"); | |
workResource.Set(Rsc.Name, "Servente (Work)"); | |
workResource.Set(Rsc.Initials, "S"); | |
workResource.Set(Rsc.Type, ResourceType.Work); | |
workResource.Set(Rsc.StandardRateFormat, RateFormatType.Hour); | |
workResource.Set(Rsc.Code, "1503"); | |
Resource materialResource = project.Resources.Add("Tijolo (Material)"); | |
materialResource.Set(Rsc.Name, "Tijolo (Material)"); | |
materialResource.Set(Rsc.Initials, "T"); | |
materialResource.Set(Rsc.Type, ResourceType.Material); | |
materialResource.Set(Rsc.StandardRateFormat, RateFormatType.MaterialResourceRate); | |
materialResource.Set(Rsc.Code, "21341"); | |
// Add tasks | |
Task task1 = project.RootTask.Children.Add("Task - 01"); | |
task1.Set(Tsk.IsRollup, new NullableBool(true)); | |
task1.Set(Tsk.IsPublished, new NullableBool(false)); | |
Task task2 = task1.Children.Add("Task - 01.01"); | |
task2.Set(Tsk.IsRollup, new NullableBool(true)); | |
task2.Set(Tsk.IsPublished, new NullableBool(false)); | |
Task task3 = task2.Children.Add("Task - 01.01.001"); | |
task3.Set(Tsk.IsEstimated, new NullableBool(false)); | |
task3.Set(Tsk.Start, new DateTime(2019, 9, 16, 9, 0, 0)); | |
task3.Set(Tsk.Duration, project.GetDuration(10, TimeUnitType.Day)); | |
task3.Set(Tsk.Work, project.GetDuration(10, TimeUnitType.Day)); | |
task3.Set(Tsk.IsRollup, new NullableBool(true)); | |
task3.Set(Tsk.IsPublished, new NullableBool(false)); | |
// Assign tasks | |
ResourceAssignment assignment1 = project.ResourceAssignments.Add(task3, materialResource); | |
assignment1.Set(Asn.Delay, project.GetDuration(40, TimeUnitType.Hour)); | |
assignment1.Set(Asn.Start, new DateTime(2019, 9, 23, 9, 0, 0)); | |
assignment1.Set(Asn.Finish, new DateTime(2019, 9, 27, 18, 0, 0)); | |
ResourceAssignment assignment2 = project.ResourceAssignments.Add(task3, workResource); | |
assignment2.Set(Asn.Work, project.GetDuration(56, TimeUnitType.Hour)); | |
assignment2.Set(Asn.Start, new DateTime(2019, 9, 16, 9, 0, 0)); | |
assignment2.Set(Asn.Finish, new DateTime(2019, 9, 24, 18, 0, 0)); | |
// to match expected MPP fully | |
assignment2.Set(Asn.WorkContour, WorkContourType.Contoured); | |
task3.Set(Tsk.IsManual, new NullableBool(true)); | |
task1.Set(Tsk.IsManual, new NullableBool(true)); | |
// Save project | |
project.Save("Assignment_Dates.mpp", SaveFileFormat.MPP); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a new project | |
Project project = new Project(); | |
// Save it as .mpp file | |
project.Save("New Project.mpp", SaveFileFormat.MPP); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a new project | |
Project project = new Project(); | |
// Add task and sub task | |
Task task = project.RootTask.Children.Add("Summary1"); | |
Task subtask = task.Children.Add("Subtask1"); | |
// Save file | |
project.Save("CreateTasks_out.xml", SaveFileFormat.XML); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a new project | |
Project project = new Project(); | |
// Save it as .xml file | |
project.Save("EmptyProjectSaveXML_out.xml", SaveFileFormat.XML); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create project | |
Project project = new Project(); | |
// Add resources | |
Resource resource = project.Resources.Add("Rsc"); | |
// Save project | |
project.Save("CreateResources_out.xml", SaveFileFormat.XML); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment