Getting the geodatabase directory path

1201
3
Jump to solution
12-06-2021 06:27 PM
ThiPham12
New Contributor III

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!

0 Kudos
1 Solution

Accepted Solutions
DanielHuantes
New Contributor

Either of the following should work for you.

System.IO.Path.GetDirectoryName(MULayer.GetPath().ToString());

or

MULayer.GetFeatureClass().GetDatastore().GetPath().ToString()

View solution in original post

0 Kudos
3 Replies
DanielHuantes
New Contributor

Either of the following should work for you.

System.IO.Path.GetDirectoryName(MULayer.GetPath().ToString());

or

MULayer.GetFeatureClass().GetDatastore().GetPath().ToString()

0 Kudos
ThiPham12
New Contributor III

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!

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

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. 

0 Kudos