问题描述

今天在测试时,又遇到了0x80040228的错误。曾经也遇到过:

​ArcGIS 10.4的0x80040228许可错误 - 我也是个傻瓜 - 博客园 (cnblogs.com)​

主要问题是在ArcGIS10.4的环境下打开SHP工作空间时,出现0x80040228错误,错误描述如下:

System.Runtime.InteropServices.COMException (0x80040228): Exception from HRESULT: 0x80040228

at ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass.OpenFromFile(String fileName, Int32 hWnd)

再议ArcGIS 10.4的0x80040228错误_初始化

官方帮助

在ESRI帮助中找到打开Shp文件的​​示例代码​​(不得不说,官方代码无论是逻辑,还注释都值得学习)。



/// <summary>
/// Get the FeatureClass from a Shapefile on disk (hard drive).
/// </summary>
/// <param name="string_ShapefileDirectory">A System.String that is the directory where the shapefile is located. Example: "C:\data\USA"</param>
/// <param name="string_ShapefileName">A System.String that is the shapefile name. Note: the shapefile extension's (.shp, .shx, .dbf, etc.) is not provided! Example: "States"</param>
/// <returns>An IFeatureClass interface. Nothing (VB.NET) or null (C#) is returned if unsuccessful.</returns>
/// <remarks></remarks>
public ESRI.ArcGIS.Geodatabase.IFeatureClass GetFeatureClassFromShapefileOnDisk(System.String string_ShapefileDirectory, System.String string_ShapefileName)
{

System.IO.DirectoryInfo directoryInfo_check = new System.IO.DirectoryInfo(string_ShapefileDirectory);
if (directoryInfo_check.Exists)
{

//We have a valid directory, proceed

System.IO.FileInfo fileInfo_check = new System.IO.FileInfo(string_ShapefileDirectory + "\\" + string_ShapefileName + ".shp");
if (fileInfo_check.Exists)
{

//We have a valid shapefile, proceed

ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
ESRI.ArcGIS.Geodatabase.IWorkspace workspace = workspaceFactory.OpenFromFile(string_ShapefileDirectory, 0);
ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explict Cast
ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(string_ShapefileName);

return featureClass;
}
else
{

//Not valid shapefile
return null;
}

}
else
{

// Not valid directory
return null;
}

}


使用官方的代码仍然也现这个问题,说明不是打开操作代码的问题,查询错误代码0x80040228即LICENSE_NOT_INITIALIZED,还是许可的问题。

解决方案

翻看了ESRI社区相关的讨论,有人也说了这是一个Bug,这里不再讨论原因,只讲有效的解决方法。

esriLicenseProductCodeAdvanced许可改为esriLicenseProductCodeArcServer,不要问为什么,经测试其他产品代码都不行。



//绑定产品
ESRI.ArcGIS.RuntimeManager.Bind(ProductCode.Desktop);
IAoInitialize aoInitialize = new AoInitializeClass();
//原初始化许可
//esriLicenseStatus licenseStatus = aoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced);
//新初始化许可
esriLicenseStatus licenseStatus = aoInitialize.Initialize(esriLicenseProductCode. esriLicenseProductCodeArcServer);


有人说此方法无效,是因为初始化代码的位置不对。(整个软件只有第一次初始化许可有效!)