問題描述
今天在測試時,又遇到了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) + ""); } }