Reading the Critical Path in VSTO and Aspose.Tasks for .NET

Code Examples

VSTO

Following steps are required to accomplish this task:

  1. Create a new project in Visual Studio.

  2. In the Solution Explorer, right-click and select Add Reference, then select the COM components tab.

  3. Select the Microsoft Project 12.0 Object Library and then click OK.

  4. 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
 2
 3Application projectApplication = new MSProject.Application();
 4
 5object missingValue = System.Reflection.Missing.Value;
 6
 7//Open an MPP file
 8
 9projectApplication.FileOpenEx("Project1.mpp",
10
11    missingValue, missingValue, missingValue, missingValue,
12
13    missingValue, missingValue, missingValue, missingValue,
14
15    missingValue, missingValue, PjPoolOpen.pjPoolReadOnly,
16
17    missingValue, missingValue, missingValue, missingValue,
18
19    missingValue);
20
21//Create a Project object by assigning active project
22
23Project project = projectApplication.ActiveProject;
24
25// Enumerate the tasks
26
27foreach (Task task in project.Tasks)
28
29{
30
31    //Get critical tasks
32
33    if (task != null)
34
35        if ((bool)task.Critical)
36
37        {
38
39            Console.WriteLine(task.ID + "  " + task.Name);
40
41            Console.WriteLine(task.Start);
42
43            Console.WriteLine(task.Finish);
44
45        }
46
47}
48
49// Make sure to clean up and close the file
50
51projectApplication.FileCloseAll(PjSaveType.pjDoNotSave);

This code demonstrates how to read critical tasks from a Microsoft Project file using VSTO and the Microsoft Project Interop library. After referencing the Microsoft Project 12.0 Object Library, an Application object is created and an MPP file is opened in read-only mode. The code then iterates through the project’s tasks, checks if each task is marked as critical using the Critical property, and outputs its ID, name, start, and finish dates. Finally, it properly closes the project without saving changes.

Aspose.Tasks

The following steps are required to accomplish this task:

  1. Create a new project in Visual Studio.

  2. In the Solution Explorer, right-click and select Add Reference, then select the .NET tab.

  3. Select Aspose.Tasks and click OK.

  4. This imports the Aspose.Tasks namespace at the start of the code.

  5. Use the code from the following example to read tasks and resources.

 1string fileName = "Project.mpp";
 2
 3ProjectReader reader = new ProjectReader();
 4
 5Project project = reader.Read(fileName);
 6
 7// Get the critical path
 8
 9ArrayList criticalPath = new ArrayList(project.GetCriticalPath());
10
11// Enumerate the tasks in the critical path
12
13foreach (Aspose.Tasks.Task task in criticalPath)
14
15{
16
17  Console.WriteLine(task.Id + "  " + task.Name);
18
19  Console.WriteLine(task.Start);
20
21  Console.WriteLine(task.Finish);
22
23}

This example demonstrates how to extract the critical path from a Microsoft Project file using Aspose.Tasks for .NET. After adding a reference to the Aspose.Tasks library, the code loads an MPP file with ProjectReader. It then calls GetCriticalPath() to retrieve a list of tasks that make up the critical path. Each task in the path is enumerated and its ID, name, start, and finish dates are printed. This allows developers to identify key tasks that directly impact the project completion date.

Download Sample Code

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.