crash on OpenFeatureClass... adding a shapefile

420
3
11-15-2010 07:17 AM
EugeneDerner
New Contributor
I am coding an AddIn button to add a specific shapefile to my map. 

I borrowed from the code snippet finder but haven't had any luck using it.  When I step through the project in debug mode, ArcMap always crashes on the line: 

IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(shapefile); 

Any help would be appreciated.

        public void AddShapefile()
 {
               IMxDocument mxd;
               mxd = ArcMap.Application.Document as IMxDocument;

               string shapeDir = "C:\\dev\\shapefiles";
               string shapefile = "basins_today";
            
              IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
              IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(shapeDir, 0); // Explicit Cast
              IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(shapefile);

              IFeatureLayer featureLayer = new FeatureLayerClass();
              featureLayer.FeatureClass = featureClass;
              featureLayer.Name = featureClass.AliasName;
              featureLayer.Visible = true;

              // Zoom to RFC extent
              ESRI.ArcGIS.Geometry.IEnvelope pEnv = new ESRI.ArcGIS.Geometry.EnvelopeClass();
              pEnv.PutCoords(-115.2, 34.89, -89.33, 51.92);
              mxd.ActiveView.Extent = pEnv;
              mxd.ActiveView.Refresh();

 }
0 Kudos
3 Replies
JamesCrandall
MVP Frequent Contributor
Edit: yeah, I see where I am incorrect.  Sorry about that --- I am not much into C# so I should have checked before posting. 

Just a shot in the dark on this one, but could it be with the extra "\" characters in your path?  Or is that something specific to C#? (I don't code in that language so sorry if this is an elementary misunderstanding on my part).

orig:
string shapeDir = "C:\\dev\\shapefiles";

try this:
string shapeDir = "C:\dev\shapefiles";
0 Kudos
AlexanderGray
Occasional Contributor III
The code looks ok.
James, the extra backslash is because c# does not use string literals, the slash is an escape character (\n is an end of line.)  Double slash is correct.
The shapefile could be corrupt.  You could try adding it from ArcMap add data.  Also, if you don't have an exception handler (try, catch) I suggest you put one in and examine the error message.  You might also consider using the IWorkspace2.nameexists method because openshapefile with a featureclass that is missing raises an exception

try-catch:
http://msdn.microsoft.com/en-us/library/0yd65esw.aspx
0 Kudos
EugeneDerner
New Contributor

The shapefile could be corrupt. 


Geez did I get tunnel vision there!!  I was banging my head trying to figure out why this wouldn't work.  That was the problem!

Thanks so much!!!
0 Kudos