获取mdb、gdb、shp的Ifeatureclass

问题描述:如题

 

解决方法:

  private IFeatureClass GetFeatureClass()
        {
            IFeatureClass fc = null;
            FileInfo fileInfo = new FileInfo(_outfilepath);
            if (_outfilepath.ToUpper().Contains(".SHP"))
            {
                IWorkspaceFactory pWorkspaceFactory;
                pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
                IFeatureWorkspace pFeatWS;
                pFeatWS = pWorkspaceFactory.OpenFromFile(fileInfo.Directory.ToString(), 0) as IFeatureWorkspace;
                //打开一个要素类
                fc = pFeatWS.OpenFeatureClass(fileInfo.Name);
            }
            else if (_outfilepath.ToUpper().Contains(".MDB"))
            {
                IWorkspaceFactory pFactory = new AccessWorkspaceFactoryClass();
                ESRI.ArcGIS.Geodatabase.IWorkspace pWorkspace = pFactory.OpenFromFile(fileInfo.DirectoryName, 0);
                IFeatureWorkspace pFeatWorkspace = pWorkspace as IFeatureWorkspace;
                fc = pFeatWorkspace.OpenFeatureClass(fileInfo.Name);
            }
            else if (_outfilepath.ToUpper().Contains(".GDB"))
            {
                IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();
                ESRI.ArcGIS.Geodatabase.IWorkspace workspace = workspaceFactory.OpenFromFile(fileInfo.DirectoryName, 0);
                IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
                fc = featureWorkspace.OpenFeatureClass(fileInfo.Name);
            }
            return fc;

        }

 

更新:

如果路径是GDB、MDB切包含了数据集的路径时,fileInfo.DirectoryName得到的是要素集的全路径,而不是MDB、GDB的

改进方法如下:

 


        private IFeatureClass GetFeatureClass(string _outfilepath)
        {
            IFeatureClass fc = null;
            FileInfo fileInfo = new FileInfo(_outfilepath);
            if (_outfilepath.ToUpper().Contains(".SHP"))
            {
                IWorkspaceFactory pWorkspaceFactory;
                pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
                IFeatureWorkspace pFeatWS;
                pFeatWS = pWorkspaceFactory.OpenFromFile(fileInfo.Directory.ToString(), 0) as IFeatureWorkspace;
                //打开一个要素类
                fc = pFeatWS.OpenFeatureClass(fileInfo.Name);
            }
            else if (_outfilepath.ToUpper().Contains(".MDB"))
            {
                string mdbpath = fileInfo.DirectoryName;
                if (!fileInfo.DirectoryName.ToUpper().EndsWith(".MDB"))
                {
                    mdbpath = fileInfo.DirectoryName.Substring(0, fileInfo.DirectoryName.ToUpper().LastIndexOf(".MDB"));
                }
                IWorkspaceFactory pFactory = new AccessWorkspaceFactoryClass();
                ESRI.ArcGIS.Geodatabase.IWorkspace pWorkspace = pFactory.OpenFromFile(mdbpath, 0);
                IFeatureWorkspace pFeatWorkspace = pWorkspace as IFeatureWorkspace;
                fc = pFeatWorkspace.OpenFeatureClass(fileInfo.Name);
            }
            else if (_outfilepath.ToUpper().Contains(".GDB"))
            {
                string gdbpath = fileInfo.DirectoryName;
                if (!fileInfo.DirectoryName.ToUpper().EndsWith(".GDB"))
                {
                    gdbpath = fileInfo.DirectoryName.Substring(0, fileInfo.DirectoryName.ToUpper().LastIndexOf(".GDB")+4);
                }
                IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();
                ESRI.ArcGIS.Geodatabase.IWorkspace workspace = workspaceFactory.OpenFromFile(gdbpath, 0);
                IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
                fc = featureWorkspace.OpenFeatureClass(fileInfo.Name);
            }
            return fc;

        }

 

转载自:https://blog.csdn.net/qq_33222460/article/details/86166145

You may also like...