Reading the Critical Path in VSTO and Aspose.Tasks
Contents
[
Hide
Show
]Code Examples
VSTO
Following steps are required to accomplish this task:
- Create a new project in Visual Studio.
- In the Solution Explorer, right-click and select Add Reference, then select the COM components tab.
- Select the Microsoft Project 12.0 Object Library and then click OK.
- This imports the Microsoft.Office.Interop.MSProject namespace at the start of the code.Use the code from the following example to read critical tasks.
1// Create Application object
2Application projectApplication = new MSProject.Application();
3object missingValue = System.Reflection.Missing.Value;
4//Open an MPP file
5projectApplication.FileOpenEx("Project1.mpp",
6 missingValue, missingValue, missingValue, missingValue,
7 missingValue, missingValue, missingValue, missingValue,
8 missingValue, missingValue, PjPoolOpen.pjPoolReadOnly,
9 missingValue, missingValue, missingValue, missingValue,
10 missingValue);
11//Create a Project object by assigning active project
12Project project = projectApplication.ActiveProject;
13// Enumerate the tasks
14foreach (Task task in project.Tasks)
15{
16 //Get critical tasks
17 if (task != null)
18 if ((bool)task.Critical)
19 {
20 Console.WriteLine(task.ID + " " + task.Name);
21 Console.WriteLine(task.Start);
22 Console.WriteLine(task.Finish);
23 }
24}
25// Make sure to clean up and close the file
26projectApplication.FileCloseAll(PjSaveType.pjDoNotSave);
Aspose.Tasks
The following steps are required to accomplish this task:
- Create a new project in Visual Studio.
- In the Solution Explorer, right-click and select Add Reference, then select the .NET tab.
- Select Aspose.Tasks and click OK.
- This imports the Aspose.Tasks namespace at the start of the code.
- Use the code from the following example to read tasks and resources.
1string fileName = "Project.mpp";
2ProjectReader reader = new ProjectReader();
3Project project = reader.Read(fileName);
4// Get the critical path
5ArrayList criticalPath = new ArrayList(project.GetCriticalPath());
6// Enumerate the tasks in the critical path
7foreach (Aspose.Tasks.Task task in criticalPath)
8{
9 Console.WriteLine(task.Id + " " + task.Name);
10 Console.WriteLine(task.Start);
11 Console.WriteLine(task.Finish);
12}