Workspace Factory COMException 0x80040228

4607
3
09-05-2011 08:55 AM
ChristopherBennett1
New Contributor
I've seen so many previous posts....  Tried all the things I could find, but still get this stubborn error when I try to instantiate a workspace from a file spec.

Environment:
Windows 7, sp 1
Intel i7, 8Gb RAM
Visual Studio 2010, sp 1
Arc version: 10.0.2414
.NET build target: 3.5


I have a very simple example console program to test opening a simple shape file.  (I'm actually trying to get a file gdb workspace opened, but that fails as well, so does personal gdb.)

All the other posts I've seen have been resolved by one or the other things I've added into the following code, so I'm turning to the real experts 🙂

using System;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem;

namespace EngineProj
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            string gdbName = @"C:\Projects\test\lines.shp";
            IWorkspace workspace = null;

            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);

            IAoInitialize ao = new AoInitializeClass();

            ao.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcEditor);

            Type factoryType = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");
            IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);

            // This line fails with COMException: Exception from HRESULT: 0x80040228
            workspace = workspaceFactory.OpenFromFile(gdbName, 0);
        }
    }
}


Any hints are most appreciated.

Thanks!

-Chris.
0 Kudos
3 Replies
DubravkoAntonic
New Contributor III
try this
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#//00490000008m000000

using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;

IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
IWorkspace workspace = workspaceFactory.OpenFromFile(string_ShapefileDirectory, 0);
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(string_ShapefileName);
0 Kudos
NeilClemmons
Regular Contributor III
The workspace for a shapefile is the directory that contains the files that make up the shapefile.  The problem with the code that you posted is that you're passing in the path to the *.shp file, which is incorrect.  The workspace for a file geodatabase is also a directory.  The workspace for a personal geodatabase is the database file.  So, make sure you're passing in the correct path for the type of workspace you're trying to open.  A recent thread also suggested that there is a bug when binding using the EngineOrDesktop option.  If you have Engine installed but not licensed then the binding will fail even if you have Desktop installed and properly licensed.
0 Kudos
ChristopherBennett1
New Contributor
Neil,

Thanks very much for your (as usual) correct response.  Changing the EngineOrDesktop option to Desktop solved the problem with opening the file GDB.  I'll have to wrap this in a try/catch block.

I haven't opened shapefiles with AO in a while and seemed to recall having to pass in the actual shapefile name, but I guess I mis-remembered.  If I had read a bit further in the help, I would have seen this.  I'll note here that the bug doesn't seem to affect opening the shapefile workspace.

Many thanks!
-Chris.


The workspace for a shapefile is the directory that contains the files that make up the shapefile.  The problem with the code that you posted is that you're passing in the path to the *.shp file, which is incorrect.  The workspace for a file geodatabase is also a directory.  The workspace for a personal geodatabase is the database file.  So, make sure you're passing in the correct path for the type of workspace you're trying to open.  A recent thread also suggested that there is a bug when binding using the EngineOrDesktop option.  If you have Engine installed but not licensed then the binding will fail even if you have Desktop installed and properly licensed.
0 Kudos