I would like to get the path directory of a geodatabase, gSSURGO_CA.gdb. Inside the gSSURGO_CA. gdb is a feature layer, MUPOLYGON. shp. How would I change this script to get the path directory for the geodatabase, instead of the feature layer, MUPOLYGON directory?
var MULayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(fl => fl.Name.Contains("MUPOLYGON")).FirstOrDefault();
var gdbPath = MULayer.GetPath();
Thanks!
Solved! Go to Solution.
Either of the following should work for you.
System.IO.Path.GetDirectoryName(MULayer.GetPath().ToString());
or
MULayer.GetFeatureClass().GetDatastore().GetPath().ToString()
Either of the following should work for you.
System.IO.Path.GetDirectoryName(MULayer.GetPath().ToString());
or
MULayer.GetFeatureClass().GetDatastore().GetPath().ToString()
Hi Daniel,
Thanks for your help! It worked. I was hoping you can help with a related question. How can I get the gdb path directory without the feature class, but with the gdb name instead? Thanks!
You can cast your MULayer to the type BasicFeatureLayer and then use the GetDataConnection method to get the data connection properties:
ArcGIS Pro 2.9 API Reference Guide - GetDataConnection Method—ArcGIS Pro
GetDataConnection returns CIMDataConnection which can be directly used to create layers, standalone tables etc. in the same geodatabase. You can cast CIMDataConnection to CIMStandardDataConnection in order to view the detail about the connection, Here is a sample button:
protected override void OnClick()
{
var TestLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<BasicFeatureLayer>().FirstOrDefault();
if (TestLayer == null) return;
QueuedTask.Run(() =>
{
var cimDataConnection = TestLayer.GetDataConnection() as CIMStandardDataConnection;
if (cimDataConnection == null) return;
System.Diagnostics.Trace.WriteLine(cimDataConnection.WorkspaceConnectionString);
});
}
and the output for cimDataConnection.WorkspaceConnectionString in my sample is:
DATABASE=C:\Data\Interacting with Maps\Interacting with Maps.gdb
which in essence is the path to the file geodatabase.