I'm working on a ArcMap plugin using ArcObjects that will change the location of a shapefile that is broken. However, I'm getting HResult Error E_Fail whenever I try opening the feataure class. This is the specific HResult value: -2147467259

Here's the setup. I have a MXD file with a shapefile that I created. This shapefile's location has been moved from the MXD file expects it to be. It expects it to be in C:\usr\Desktop, but I have moved it to C:\usr\Desktop\tmp. The shapefile is called "New_Shapefile.shp"

The goal is to repair the location of the shapefile. Here is the current code I have for shapefile replace. The following fails whenever I try opening the feature dataset.

IFeatureLayer featureLayer = (IFeatureLayer)pLayer //pLayer is the current ILayer;
IFeatureClass oldFeatureClass = featureLayer.FeatureClass;

IPropertySet propertySet = new PropertySetClass();
propertySet.SetProperty("DATABASE", "C:\usr\Desktop\tmp");
IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();

    IWorkspace workspace = workspaceFactory.Open(propertySet, 0);
    //I've also tried the following for opening up a workspace:
    //IWorkspace workspace = workspaceFactory.OpenFromFile("C:\usr\Desktop\tmp", 0);
    IFeatureWorkspace fWork = (IFeatureWorkspace)workspace;

    //program hits exception on line below
    IFeatureDataset newFeatureClass = fWork.OpenFeatureDataset("New_Shapefile"); 

    IMapAdmin2 mapAdmin2 = (IMapAdmin2)map;
    featureLayer.FeatureClass = newFeatureClass;
    mapAdmin2.FireChangeFeatureClass(oldFeatureClass, newFeatureClass);

    ESRI.ArcGIS.ArcMapUI.IContentsView contentsView = ArcMap.Document.CurrentContentsView;
catch (Exception e)
    ErrorMessage("uh oh something went wrong :(", e);

This code was based off of: http://edndoc.esri.com/arcobjects/9.2/ComponentHelp/esriGeodatabase/IFeatureWorkspace_OpenFeatureClass_Example.htm

Edit: This is for ArcMap 10.2

Edit: This code block works, I just had to rebuild the solution and it's working now. I'm not sure why it required a rebuild though. I've tested this for geodatabase files as well, just change the ShapeFileWorkspaceFactoryClass() to FileGDBWorkspaceFactoryClass()

  • 1
    Duplicate?: gis.stackexchange.com/questions/146611/… – mkennedy Dec 11 '15 at 17:35
  • Please always specify the version of software in use, especially when quoting ancient documentation (edit the question) – Vince Dec 11 '15 at 17:41
  • I've edited my question now to include ArcMap version number – Josh Ferrell Dec 11 '15 at 17:54

I think the problem here is you're trying to use an IPropertySet to open a shapefile... that's not correct. You need to use an IWorkspaceFactory for sure but you create it like this:

IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
IWorkspace pWS = pWSF.OpenFromFile(pOutPath, 0); // pOutPath is the path to the folder

then with the workspace as IFeatureWorkspace open the shapefile:

IFeatureClass pFC = (pWS as IFeatureWorkspace).OpenFeatureClass("New_Shapefile");  // Open feature class opens a feature class
IFeatureDataset newFeatureClass = (IFeatureDataset)pFC; // cast the feature class to a feature dataset.

The use of IPropertySet is intended for SDE workspaces that require credentials and transactional version name.

Another point here is that if the shapefile is moved the layer is invalid (can't find the data any more) - check this using pLayer.Valid before trying to cast to IFeatureLayer. To get the data source details from an invalid layer proceed via IDataLayer2.DataSourceName:

IDataLayer2 pDataLayer = (IDataLayer2)pLayer;
IDatasetName pDataName = pDataLayer.DataSourceName;
String pLayerName = pDataName.Name;

If you try to go via IFeatureLayer.FeatureClass on an invalid layer you will be disappointed as the reference is Null.

  • While it is true that the featurelayer of an invalid layer will be null, according to ESRI's documentation, in order to call the FireChangeFeatureClass you need to pass two featureclasses. Thankfully, my code works. I thought the same thing as you for the workspace, but using it as a property file or a open from file works for both. I'm not quite sure what caused my initial error, but after a rebuild it works. – Josh Ferrell Dec 14 '15 at 2:20
  • Fair enough, I found those problems years ago when migrating storage; I wrote a 'repointer' tool to find all the datasources again from invalid layers which is how I discovered that the FeatureClass is null and found the IDataLayer2 workaround... hopefully that helps someone encountering that issue. Firing the Change Feature classes should accept Null then the new feature class as the arguments but I haven't tested it. – Michael Stimson Dec 14 '15 at 2:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.