Getting path to MXD using IDocumentInfo2

4724
2
06-21-2011 02:17 PM
JeffreyHamblin
New Contributor III
I need to get the path to the currently open map document for a C# Add-In.

While searching, I found several references that use essentially the following:
public string GetActiveDocumentPath(IApplication application)
{
    // The active document is always the last template
    ITemplates templates = application.Templates;
    return templates.get_Item(templates.Count - 1);
}  

called with:
string mxdPath = GetActiveDocumentPath(ArcMap.Application);


Then I found IDocumentInfo2.Path in the documentation, and it seems to work fine:
public string GetActiveDocumentPath(IMapDocument mapDocument)
{
    IDocumentInfo2 docInfo = mapDocument as IDocumentInfo2;
    return docInfo.Path;
}

called with:
string mxdPath = GetActiveDocumentFolder(ArcMap.Document as IMapDocument);


My question is if anyone knows of any reasons to use one versus the other.
0 Kudos
2 Replies
KeithOlson
New Contributor III
I have a project that uses the ITemplates interface to get the mxd path.  If the document hasn't yet been saved it will return a string that ends in ".tmp", so maybe that's why some people use IDocumentInfo2.  Not sure what that interface would return if the doc hasn't been saved, though.

Hope that helps.  My advice would be to try one method and run it in debug mode with a couple of different scenarios.  If you don't get what you need then try the other one.
0 Kudos
JeffreyHamblin
New Contributor III
Keith,

Thanks for the heads-up about dealing with a new unsaved document. As it turns out, it IS an issue and I probably wouldn't have caught it until much later in testing.

I am streaming some data into the MXD in an overridden OnSave method in an Add-In Extension. It is some extended setup information for an Add-In that includes path information which must be re-written to use relative paths before saving in the MXD (and expanded to absolute after being read). That's why I need the path to the MXD.

IDocumentInfo2.Folder was working fine in my initial tests. However, when saving  a new unsaved document, the Folder property is null (and the Path property is empty) -- even though OnSave occurs after the file save dialog. I suspect the properties are set AFTER the save completes.

Fortunately, The templates method of getting the path does work correctly in the first OnSave of a new document. So that's what I will be using.
0 Kudos