Save Mxd Document using C# code

3442
1
04-24-2013 10:55 PM
ChaitanyaPeddinty
New Contributor
Hi My requirement is to Open an Arc map application and close it automatically through C# coding.

For that I Wrote below code

IDocument pDoc = new MxDocumentClass();
                pApp = pDoc.Parent;
                pApp.OpenDocument(sMxdFileName);

                if (pApp != null)
                {
                  
                    pApp.Shutdown(); 
                 }
  // pApp.saveDocument is not working

Everything is fine , But before  shutdown statement is executing , In Map same dialog box like save changes to Mxd and Yes or No Buttons are avaialble there. We should able to handle that control also through coding before execute the Shutdoen statement.i.e we should close the document opened with affected changes.

can u please suggest me the code for that
0 Kudos
1 Reply
JasonPike
Occasional Contributor
Maybe you can try something like this:

IDocument pDoc = null;
IApplication pApp = null;

try
{
    pDoc = new MxDocumentClass();
    pApp = pDoc.Parent;
    pApp.OpenDocument(sMxdFileName);

    // save file
    IMapDocument mapDoc = (IMapDocument)pDoc;
    mapDoc.Save(true, true);

    // suppress "do you want to save dialog"
    IDocumentDirty2 dirty = (IDocumentDirty2)pDoc;
    dirty.SetClean();
}
finally
{
    if (pDoc != null)
    {
        Marshal.ReleaseComObject(pDoc);
    }
    if (pApp != null)
    {
        pApp.Shutdown();
        Marshal.ReleaseComObject(pApp);
    }
}
0 Kudos