再议ArcGIS 10.4的0x80040228错误
问题描述
今天在测试时,又遇到了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)
官方帮助
在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,还是许可的问题。
解决方案
这里不再讨论原因,只讲有效的解决方法。
方法一:改用ArcServer产品代码
将esriLicenseProductCodeAdvanced许可改为esriLicenseProductCodeArcServer,不要问为什么,经测试其他产品代码都不行。
//绑定产品 ESRI.ArcGIS.RuntimeManager.Bind(ProductCode.Desktop); IAoInitialize aoInitialize = new AoInitializeClass(); //原初始化许可 //esriLicenseStatus licenseStatus = aoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced); //新初始化许可 esriLicenseStatus licenseStatus = aoInitialize.Initialize(esriLicenseProductCode. esriLicenseProductCodeArcServer);
有人说此方法无效,是因为初始化代码的位置不对。(整个软件只有第一次初始化许可有效!)
方法二:先打开其他工作空间
翻看了ESRI社区相关的讨论,shp和gdb的工作空间都是一个文件夹,有人也说了这是一个Bug,经同事测试,在打开shp的工作空间之前,先打开任意一个存在的gdb或mdb工作空间获取初始化授权后也能解决问题。测试代码如下 :
private void button1_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "*.shp|*.shp"; if (openFileDialog.ShowDialog() == DialogResult.OK) { string fileName = openFileDialog.FileName; string path = System.IO.Path.GetDirectoryName(fileName); string name = System.IO.Path.GetFileNameWithoutExtension(fileName); //先打开任意一个存在的gdb或mdb IWorkspaceFactory wsfgdb = new FileGDBWorkspaceFactoryClass(); wsfgdb.OpenFromFile($"{System.Windows.Forms.Application.StartupPath}\\Test.gdb", 0); IWorkspaceFactory wsf = new ShapefileWorkspaceFactory(); IWorkspace pWorkspace = wsf.OpenFromFile(path, 0); IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspace; IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(name); MessageBox.Show(pFeatureClass.FeatureCount(null) + ""); } }
作者:我也是个傻瓜
出处:http://www.cnblogs.com/liweis/
签名:成熟是一种明亮而不刺眼的光辉。