Cannot Open Shapefile

3396
9
02-22-2011 11:58 PM
KeremCelik
New Contributor
Hi,

I have a problem with opening shape file. I have a shape file in "C:\Shared\sample\sample.shp" (Of course the other files with extension dbf, prj, sbn, sbx, shp.xml and shx are in the same directory.) When I run the code below, I receive the exception called COMException was unhandled "Exception from HRESULT: 0x80040228".

I am running this code in Windows 7 Professional 64bit, ArcGIS 10 Engine and Visual Studio project target platform is x86.

If you have any idea, about the problem, please help me.

Thanks in advance.

Kerem Celik.

Type factory_type = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");

IWorkspaceFactory workspace_factory = (IWorkspaceFactory)Activator.CreateInstance(factory_type);

IFeatureWorkspace workspace = (IFeatureWorkspace)workspace_factory.OpenFromFile(@"C:\Shared\sample\", 0);

IFeatureClass feature_class = workspace.OpenFeatureClass(System.IO.Path.GetFileName(sample);

ESRI.ArcGIS.Carto.IFeatureLayer feature_layer = new FeatureLayerClass();

feature_layer.FeatureClass = (IFeatureClass)feature_class;

default_map.AddLayer((ILayer)feature_layer);
0 Kudos
9 Replies
KeremCelik
New Contributor
Hi,

I really stuck on this problem. I have to solve it somehow. I need to load shape file programmatically, as explained in the thread above.

Please help me.

Thanks again.
Kerem.
0 Kudos
EdEngle
New Contributor III
Not sure if you ever got this working but since your code helped me get adding shapefiles...  I figured I would post how I did it.

// Variables
string fullpathToShapefile = "C:\Shared\sample\sample.shp"
string shp_path = Path.GetDirectoryName(fullpathToShapefile); // This will be "C:\Shared\sample"
string shp_name = Path.GetFileNameWithoutExtension(fullpathToShapefile); // this will be  "sample"

Type factory_type = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");
IWorkspaceFactory ShpWksFact = (IWorkspaceFactory)Activator.CreateInstance(factory_type);

IFeatureWorkspace FeatWks = (IFeatureWorkspace)ShpWksFact.OpenFromFile(shp_path, 0);
IFeatureClass FeatClass = FeatWks.OpenFeatureClass(shp_name);
IFeatureLayer FeatLayer = new FeatureLayerClass();

FeatLayer.FeatureClass = FeatClass;


Hope it helps
0 Kudos
EdgarBejarano
Occasional Contributor
Which statement throws the exception?  What is the exact string value that is being evaluated with your expression System.IO.Path.GetFileName(sample)?  Does it include the .shp extension?  If so, try without the .shp extension.  The below code snippet states to specify the shapefile name without the extension.  Try both ways as a test and make sure you are not missing a close parenthesis:

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/Get_FeatureClass_From_Sh...
0 Kudos
KeremCelik
New Contributor
Which statement throws the exception?  What is the exact string value that is being evaluated with your expression System.IO.Path.GetFileName(sample)?  Does it include the .shp extension?  If so, try without the .shp extension.  The below code snippet states to specify the shapefile name without the extension.  Try both ways as a test and make sure you are not missing a close parenthesis:

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/Get_FeatureClass_From_Sh...


The exception is thrown in the line IFeatureWorkspace workspace = (IFeatureWorkspace)workspace_factory.OpenFromFile(@"C:\Shared\sample\", 0);

It says : COMException was unhandled "Exception from HRESULT: 0x80040228"

Thanks,
Kerem.
0 Kudos
KeremCelik
New Contributor
Not sure if you ever got this working but since your code helped me get adding shapefiles...  I figured I would post how I did it.

// Variables
string fullpathToShapefile = "C:\Shared\sample\sample.shp"
string shp_path = Path.GetDirectoryName(fullpathToShapefile); // This will be "C:\Shared\sample"
string shp_name = Path.GetFileNameWithoutExtension(fullpathToShapefile); // this will be  "sample"

Type factory_type = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");
IWorkspaceFactory ShpWksFact = (IWorkspaceFactory)Activator.CreateInstance(factory_type);

IFeatureWorkspace FeatWks = (IFeatureWorkspace)ShpWksFact.OpenFromFile(shp_path, 0);
IFeatureClass FeatClass = FeatWks.OpenFeatureClass(shp_name);
IFeatureLayer FeatLayer = new FeatureLayerClass();

FeatLayer.FeatureClass = FeatClass;


Hope it helps


Thanks for your help kashmir772 but the code snippet you provided does not work for me.

There is a problem with IFeatureWorkspace's OpenFromFile function. All I know just an error code (COMException was unhandled "Exception from HRESULT: 0x80040228").

Kerem.
0 Kudos
EdgarBejarano
Occasional Contributor
Is this in a stand-alone application, e.g. a MapControl or are you compiling a DLL to register with ArcGIS Desktop?  If the former, are you initializing with a license?

ESRI.ArcGIS.esriSystem.IAoInitialize
0 Kudos
EdgarBejarano
Occasional Contributor
By the way, I tested your exact code and it does not throw the exception at the reported statement, thus I wonder if you have initialized ArcObjects with a valid license (if a stand-alone application)
0 Kudos
WilliamSmith4
New Contributor
Hi there,

Hopefully if someone can explain how to open shape files in arcgis. Do I need to write code. Which code do I have to and where shall I write it exactly. Please I hope someone can help me.

Thanks a million in advance.
0 Kudos
AlexanderGray
Occasional Contributor III
Error 0x80040228 is usually related to the license not being initialized.  Are you sure the license is activated?

http://forums.esri.com/Thread.asp?c=159&f=1707&t=200073
http://forums.esri.com/Thread.asp?c=2&f=1720&t=237013

Can you specify which version of ArcGIS you have (licensing has changed is 10)?
Can you specify the product installed?  Engine, desktop etc?
Can you specify the type of project, exe, add-in, dll customization?
Can you post the code that activates the license?  and the return values from the activation?
Can you specify which license you are using (engine, viewer, editor, arcinfo...)?
0 Kudos