.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

C# Open Folder of Files

2 REPLIES 2
Reply
Message 1 of 3
MDeacon_SC
1745 Views, 2 Replies

C# Open Folder of Files

Good Afternoon,

 

For this program, I am running AutoCAD2014 and Visual Studio 2012 Express C#.

 

Here is what I am trying to accomplish.  I need to iterate through a folder of dwg files and open them in AutoCAD and CloseSave them. Everytime I try to accomplish this, it is not working with each attempt giving me a different error.  Some of these include FaultExecutionEngineError, NullExceptionError, IndexOutOfRange error, and more.  Now the errors I cannot even easily debug because they are happening randomly before it crashes.

 

There is my code that I am running.  I just have a hardcoded file path with a folder that contains .dxf files. 

 

DocumentCollection acDocManager = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;

string[] filesInDirectory;
filesInDirectory = Directory.GetFiles(folderPath, "*.dxf", SearchOption.TopDirectoryOnly);

foreach (string someFile in filesInDirectory)
{
Document acDoc = acDocManager.Open(someFile, false);
acDoc.CloseAndSave(someFile);
}

 

Thanks in advanced for the help!

2 REPLIES 2
Message 2 of 3
mcicognani
in reply to: MDeacon_SC

It can be a number of things, obviously you reported a little code snippet out of context.

 

To begin with, I'd assure the command would run in a session context, using the CommandFlags.Session attributes.

next you'll need to lock the document if you plan to make some changes, but if you want just to load dxf and convert them to dwg it's not needed.

 

The code snippet seems ok, I have something very similar, even if I explicity change the name extension from .dxf to .dwg before saving the dwg.

 

But, if your real intention is to open dxf and save as dxf too, I'd use the Database.Dxfout() function instead and discard the dwg next.

Message 3 of 3
MDeacon_SC
in reply to: mcicognani

Thanks for the tips. I did find it out last night a couple hours after posting this.

 

I set Visual Studio to do a release build instead of a debug build and then I ran it in AutoCAD. It worked perfectly and did as intended. 

 

I do not know if it was the version of AutoCAD which starts when you debug that was the problem or if it is that when you compile as a release build it compiles it differently than a debug build.  Both of those options do not make any sense to me but my program is working now after doing them.

 

Thanks for the post! 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost