I'm trying to store the opened documents from the Visual Studio environment. The code works for some solutions but I have 1 solution where it always throws an exception. I have tried to wait for some kind of initialization but nothing changes over time. As soon as I catch the exception, when I call the property again it's fine. What do I do wrong?
internal static IList<Document> GetOpenedDocuments(DTE service)
{
ThreadHelper.ThrowIfNotOnUIThread();
if (service == null)
{
throw new ArgumentNullException(nameof(service));
}
List<Document> openedDocuments = new List<Document>();
foreach (Document document in service.Documents)
{
if (document != null)
{
openedDocuments.Add(document);
}
}
return openedDocuments;
}
Accessing service.Documents always crashes the first time.
Top of stack trace:
at EnvDTE.Documents.GetEnumerator() at XXX.FileHelper.GetOpenedDocuments(DTE service) in
Exception message:
The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
Thanks for the help!