If you are programming AutoCAD using the .NET API, you generally don't use commands to solve problems. You should probably download all of the API training materials if you've not done that already, and go through them. The API is very complex and takes some time to learn, but for starters, the way to find the filename of a loaded, resolved xref is not by using the BlockTableRecord's PathName property. You can fall-back to using the PathName if the xref is not loaded or resolved, but otherwise, You get the filename of the xref by calling the BlockTableRecord's GetXrefDatabase() method, which returns the Database object for the Xef, and then you just read the Database object's Filename property.
Given a BlockReference object that represents an insertion of the xref, here is the C# code that produces the name of the drawing file:
using System.IO;
// A best practice is to place extension methods in the
// same namespace as their invocation target:
namespace Autodesk.AutoCAD.DatabaseServices
{
public static class BlockReferenceExtensions
{
// Returns the xref filename for the given XREF block reference:
//
// - Requires an active transaction.
// - Returns an empty string if the given BlockReference is not
// an xref or overlay reference, or is unresolved and the file
// cannot be found.
public static string GetXrefFilename( this BlockReference blockref )
{
string result = string.Empty;
BlockTableRecord btr = (BlockTableRecord)
blockref.DynamicBlockTableRecord.GetObject( OpenMode.ForRead );
if( btr.IsFromExternalReference || btr.IsFromOverlayReference )
{
if( !btr.IsUnloaded && btr.XrefStatus == XrefStatus.Resolved )
{
using( Database db = btr.GetXrefDatabase( false ) )
{
if( db != null )
result = db.Filename;
}
}
else
{
try
{
result = HostApplicationServices.Current.FindFile(
btr.PathName, btr.Database, FindFileHint.XRefDrawing );
}
catch // don't know why, but FindFile throws an exception
{ // if the file is not found.
}
}
}
return result;
}
}
}