bhavanisankarnimmala

Get Workspace from ArcMap

Blog Post created by bhavanisankarnimmala on Jan 1, 2019

/// <summary>
/// Fetching the Workspace from ArcMap
/// </summary>
/// <returns></returns>
public static IWorkspace GetWorkspace()
{
IWorkspace workspace = null;
try
{
IMxDocument mdoc = Globals.m_app.Document as IMxDocument;
IMap map = mdoc.FocusMap;
if (map.LayerCount > 0)
{
ESRI.ArcGIS.esriSystem.IUID uid = new ESRI.ArcGIS.esriSystem.UIDClass();
uid.Value = "{40A9E885-5533-11D0-98BE-00805F7CED21}";
ESRI.ArcGIS.Carto.IEnumLayer enumLayer = map.get_Layers(((ESRI.ArcGIS.esriSystem.UID)(uid)), true); // Explicit Cast
enumLayer.Reset();
ESRI.ArcGIS.Carto.ILayer layer = null;
while ((layer = enumLayer.Next()) != null)
{
IFeatureLayer flayer = layer as IFeatureLayer;
IFeatureClass fclass = flayer.FeatureClass;
IDataset layerDs = (IDataset)fclass;
workspace = layerDs.Workspace;
break;
}
}
}
catch
{
workspace = null;
}
finally
{
GC.Collect();
}
return workspace;
}

Outcomes