Retrieve data source from layer within MXD

694
5
12-14-2010 05:13 PM
jonathanNagy1
New Contributor
Hi all:

I'm having difficulty trying to programatically access the data source for layers within an mxd.  Code follows:

DirectoryInfo di = new DirectoryInfo(folder);
            FileInfo[] files = di.GetFiles("*.mxd");
            Dictionary<string, List<String>> referencedFiles = new Dictionary<string,List<string>>();

            foreach (FileInfo fi in files)
            {
                referencedFiles.Add(fi.FullName, new List<string>());

                IMapDocument doc = new MapDocument();
                doc.Open(fi.FullName);
                doc.SetActiveView(doc.PageLayout as IActiveView);
                doc.ActiveView.Activate(0);

                IEnumLayer pEnumLayer = doc.ActiveView.FocusMap.Layers;
                pEnumLayer.Reset();
                
                ILayer pLayer;
                while ((pLayer = pEnumLayer.Next()) != null)
                {
                    IFeatureLayer pFL = pLayer as FeatureLayer; 
                    IFeatureClass pFC = pFL as IFeatureClass; // pFC is coming up null every time for all shapefile layers.
                }
            }
0 Kudos
5 Replies
JoeVondracek
New Contributor III
What if you access the layers through the maps in the map document rather than the active view?
            foreach (FileInfo fi in files)
            {
                IMapDocument pMapDoc = new MapDocumentClass();
                if (pMapDoc.get_IsMapDocument(fi.FullName))
                {
                    pMapDoc.Open(fi.FullName, null);
                    IMap pMap;
                    for (int i = 0; i <= pMapDoc.MapCount - 1; i++)
                    {
                        pMap = pMapDoc.get_Map(i);
                        IEnumLayer pEnumLayer = pMap.get_Layers(null, true);
                        pEnumLayer.Reset();
                        ILayer pLayer = pEnumLayer.Next();
                        while (pLayer != null)
                        {
                            IFeatureLayer pFtrLayer = pLayer as IFeatureLayer;
                            if (pFtrLayer != null)
                            {
                                IFeatureClass pFtrClass = pFtrLayer.FeatureClass;
                                if (pFtrClass != null)
                                {
                                    IDataset pDataset = pFtrClass as IDataset;
                                    if (pDataset != null)
                                    {
                                        \\pDataset.Name
                                        \\pDataset.Workspace.PathName
                                    }
                                }
                            }
                            pLayer = pEnumLayer.Next();
                        }

                    }

                }
0 Kudos
Venkata_RaoTammineni
Occasional Contributor
Hi all:

I'm having difficulty trying to programatically access the data source for layers within an mxd.  Code follows:

DirectoryInfo di = new DirectoryInfo(folder);
            FileInfo[] files = di.GetFiles("*.mxd");
            Dictionary<string, List<String>> referencedFiles = new Dictionary<string,List<string>>();

            foreach (FileInfo fi in files)
            {
                referencedFiles.Add(fi.FullName, new List<string>());

                IMapDocument doc = new MapDocument();
                doc.Open(fi.FullName);
                doc.SetActiveView(doc.PageLayout as IActiveView);
                doc.ActiveView.Activate(0);

                IEnumLayer pEnumLayer = doc.ActiveView.FocusMap.Layers;
                pEnumLayer.Reset();
                
                ILayer pLayer;
                while ((pLayer = pEnumLayer.Next()) != null)
                {
                    IFeatureLayer pFL = pLayer as FeatureLayer; 
                    IFeatureClass pFC = pFL as IFeatureClass; // pFC is coming up null every time for all shapefile layers.
                }
            }


Hi,

Try this

public static string GetPath(IApplication pApplication)
       {
            IMxDocument pMxDoc;

            pMxDoc = pApplication.Document as IMxDocument;

            IMap pMap;
 
            pMap = pMxDoc.FocusMap;
           
           IFeatureLayer pFeatLayer;

           IDataset pDataset;

           pFeatLayer = pMap.get_Layer(0) as IFeatureLayer;

           pDataset = pFeatLayer.FeatureClass as IDataset;

           string strFilePath = "";

         strFilePath = pDataset.Workspace.PathName;

         return strFilePath;
       }
0 Kudos
jonathanNagy1
New Contributor
I've already tried the first solution before attempting to use the active view.

As for the second, I'm not looking to use the GUI, this is a console app, therefore IApplication / IMxDocument is not going to work for me.
0 Kudos
Venkata_RaoTammineni
Occasional Contributor
I've already tried the first solution before attempting to use the active view.

As for the second, I'm not looking to use the GUI, this is a console app, therefore IApplication / IMxDocument is not going to work for me.




There are different options

1) You need to get reference of IApplication ..if you want to get mxd source path
2)  or you need to write a code pure C# code to get file path...
0 Kudos
VivekPrasad
Occasional Contributor
Hi all:

I'm having difficulty trying to programatically access the data source for layers within an mxd.  Code follows:

DirectoryInfo di = new DirectoryInfo(folder);
            FileInfo[] files = di.GetFiles("*.mxd");
            Dictionary<string, List<String>> referencedFiles = new Dictionary<string,List<string>>();

            foreach (FileInfo fi in files)
            {
                referencedFiles.Add(fi.FullName, new List<string>());

                IMapDocument doc = new MapDocument();
                doc.Open(fi.FullName);
                doc.SetActiveView(doc.PageLayout as IActiveView);
                doc.ActiveView.Activate(0);

                IEnumLayer pEnumLayer = doc.ActiveView.FocusMap.Layers;
                pEnumLayer.Reset();
                
                ILayer pLayer;
                while ((pLayer = pEnumLayer.Next()) != null)
                {
                    IFeatureLayer pFL = pLayer as FeatureLayer; 
                    IFeatureClass pFC = pFL as IFeatureClass; // pFC is coming up null every time for all shapefile layers.
                }
            }


Hi,

From your code I have observed that you have written the below line of code

"IFeatureClass pFC = pFL as IFeatureClass;"

Actually, it should be

"IFeatureClass pFC = pFL.FeatureClass;"

the rest of your code is correct.

Hope this helps.

Regards,
Vara Prasad M S
0 Kudos