Error : Exception has been thrown by the target of an invocation Options

616 views Asked by At

I have written an external application to drive autocad with a dll that was registered for COM. I have followed this codes to write my application however i have replaced the following code with AddNumbers() method:

public string OpenDWGFile(string MyDWGFilePath)
{
DocumentCollection dm = Application.DocumentManager;
Document doc = null;

if(File.Exists(MyDWGFilePath))
{
   doc = dm.Open(MyDWGFilePath, false);
   Application.DocumentManager.MdiActiveDocument = doc;
   return "This file is exists";
 }
else
   return "This file is not exist";
}

but when i run my application the autocad software open and then close immediatly and this error message is shown : Exception has been thrown by the target of an invocation.

but if i comment the following lines of my code the application works without any errors:

doc = dm.Open(MyDWGFilePath, false);
Application.DocumentManager.MdiActiveDocument = doc;
1

There are 1 answers

0
Simon Richter On

You are creating a second instance of the DocumentManager and giving it a reference to an object retrieved from the first one. I think you want to use

dm.MdiActiveDocument = doc;