How do you get the path for a layer as a string (ArcGIS Pro SDK)?

2532
6
05-09-2019 11:23 AM
JohnPhillipsGeo
New Contributor III

This seems like it should be pretty straight-forward but I cannot figure out how to get a layer object's filepath as a string.  Any help is much appreciated.  For example:

I have a layer object

var lyr = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().First()

I would like to get lyr.Path() or something like that.  So that it will return:

C:/Path/To/Layer

0 Kudos
6 Replies
RichRuh
Esri Regular Contributor

Are you trying to get the file path for the data that the layer is pointed to?

0 Kudos
JohnPhillipsGeo
New Contributor III

Rich,

Yes, I am.  For example, for a feature class in a geodatabase, I would like it to return "C:/Path/To/Geodatabase.gdb/FeatureClass"

Thanks!

0 Kudos
RichRuh
Esri Regular Contributor

Geodatabase feature classes rarely correspond to a single file on disk.  If you open up a File Geodatabase using File Explorer, you'll see there's a large number of different files.  So there isn't really a path to a feature class.

There is (sometimes) a path to the geodatabase itself.

If the feature class belongs to a file geodatabase (Geodatabase.GetGeodatabaseType() returns GeodatabaseType.LocalDatabase) , the Geodatabase.GetConnector() routine will return a FileGeodatabaseConnectionPath object.  This object has a Path property.

There are other kinds of geodatabases, of course, including SDE geodatabases and feature service workspaces.  SDE geodatabases may or may not be opened from a .SDE file, but feature service workspaces don't have a file at all, just a URL.

Hope this helps explain why it isn't straightforward!

--Rich

JohnPhillipsGeo
New Contributor III

Thanks Rich!

var gdbPath = lyr.GetTable().GetDatastore().GetPath().ToString();

Is pretty close to what I'm looking for, I am trying to feed it into 

 Geoprocessing.MakeValueArray

To use as a parameter for a tool.

Geoprocessing.ExecuteToolAsync()


Do you know if passing the string path of a layer into a tool is the best way to go about it? 

Thanks

JP

0 Kudos
RichRuh
Esri Regular Contributor

Yes, we actually added Datastore.GetPath() for use with geoprocessing.  Use this and append the table name:

Uri path = geodatabase.GetPath();
string fullPath =Path.Combine(path.LocalPath, TABLE_NAME);
0 Kudos
osmansheikh
New Contributor

You can simply use it:

var layer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();
var layerPath = await QueuedTask.Run(() => layer.GetPath());

0 Kudos