Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save GregTrevellick/e26f86251557b8ef480ea428eafef86c to your computer and use it in GitHub Desktop.
Save GregTrevellick/e26f86251557b8ef480ea428eafef86c to your computer and use it in GitHub Desktop.
EnvDTE Project indexing is not zero based
// Set up (full example at https://github.com/GregTrevellick/AutoFindReplace)
IServiceContainer serviceContainer = this as IServiceContainer;
DTE dte = serviceContainer.GetService(typeof(SDTE)) as DTE;
var projs = new List<string>();
// Let's assume we have a solution containing three projects
// The following will crash
for (int i = 0; i < dte.Solution.Projects.Count; i++) // Projects.Count is '3' as we'd expect
{
var item = dte.Solution.Projects.Item(i); // "i"
projs.Add(item.FullName);
}
// The following will work and populate 'projs' with three entries
for (int i = 0; i < dte.Solution.Projects.Count; i++) // Projects.Count is still '3' as we'd expect
{
var item = dte.Solution.Projects.Item(i + 1); // "i + 1"
projs.Add(item.FullName);
}
// Of course the following will also work fine
for (int i = 1; i < dte.Solution.Projects.Count + 1; i++)
{
var item = dte.Solution.Projects.Item(i);
projs.Add(item.FullName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment