EnvDTE.Project Availability Determination

I need to go through a big solution containing inaccessible projects . These inaccessible projects are unavailable because their paths no longer exist. If this solution was to keep these inaccessible links, is it possible to determine if every project that I run is accessible / inaccessible?

The following is a loop whose purpose is to determine whether each ProjectItem is saved in the current solution. But, since some projects are not available, I keep getting null references.

bool isDirty = false;
foreach (Project proj in sln.Projects) //sln is my current solution
{
    if (proj == null) //hoping that might do the trick
        continue;
    foreach (ProjectItem pi in proj.ProjectItems)
    {
        if (pi == null) //hoping that might do the trick
            continue;
        if (!pi.Saved)
        {
            isDirty = true;
            break;
        }
    }
}
+4
source share
1 answer

You can rewrite this in a simple operation LINQ:

//import the LINQ extensions
using System.Linq;
// some code here;
bool isDirty = sln.Projects.Any(pr => pr.ProjectItems != null && !pr.Saved);

MSDN _Solution.Projects Projects, IEnumerable generic, OfType<TResult>, :

bool isDirty = sln.Projects.OfType<Project>().Any(pr => pr.ProjectItems != null && !pr.Saved);

MSDN:

, , , a ArrayList. , OfType<TResult> IEnumerable. OfType<TResult> IEnumerable<T>, , IEnumerable.

+3

All Articles