OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Shapefiles (*.shp)|*.shp"; if (ofd.ShowDialog() == DialogResult.OK) { string path = ofd.FileName; fieldName = ofd.SafeFileName; try { ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass(); ESRI.ArcGIS.Geodatabase.IWorkspace workspace = workspaceFactory.OpenFromFile(ofd.FileName, 0); ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explict Cast ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(ofd.SafeFileName); IFields fields = featureClass.Fields; IField field = null; // On a zero based index, iterate through the fields in the collection. for (int i = 0; i < fields.FieldCount; i++) { // Get the field at the given index. field = fields.get_Field(i); if (field.Name != field.AliasName) { listBox1.Items.Add(field.Name); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); }
Solved! Go to Solution.
string shapefileLocation = openFileDialog.FileName; ... ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(shapefileLocation));
Hi,
I tried to figure this out alot but I am really stuck! I would like to list fields of a selected shapefile by using OpenFileDialog class but I am getting error on IFeatureClass featureClass part.I think should not use the OpenFeatureClass() method here but honestly I am just lost!
Can you please take a look at following code and point me what I am doing wrong?Do I really need to create all of Workspaces to access a shapefile in C#?OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Shapefiles (*.shp)|*.shp"; if (ofd.ShowDialog() == DialogResult.OK) { string path = ofd.FileName; fieldName = ofd.SafeFileName; try { ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass(); ESRI.ArcGIS.Geodatabase.IWorkspace workspace = workspaceFactory.OpenFromFile(ofd.FileName, 0); ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explict Cast ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(ofd.SafeFileName); IFields fields = featureClass.Fields; IField field = null; // On a zero based index, iterate through the fields in the collection. for (int i = 0; i < fields.FieldCount; i++) { // Get the field at the given index. field = fields.get_Field(i); if (field.Name != field.AliasName) { listBox1.Items.Add(field.Name); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); }
string shapefileLocation = openFileDialog.FileName; ... ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(shapefileLocation));
Hi,
I tried to figure this out alot but I am really stuck! I would like to list fields of a selected shapefile by using OpenFileDialog class but I am getting error on IFeatureClass featureClass part.I think should not use the OpenFeatureClass() method here but honestly I am just lost!
Can you please take a look at following code and point me what I am doing wrong?Do I really need to create all of Workspaces to access a shapefile in C#?OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Shapefiles (*.shp)|*.shp"; if (ofd.ShowDialog() == DialogResult.OK) { string path = ofd.FileName; fieldName = ofd.SafeFileName; try { ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass(); ESRI.ArcGIS.Geodatabase.IWorkspace workspace = workspaceFactory.OpenFromFile(ofd.FileName, 0); ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explict Cast ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(ofd.SafeFileName); IFields fields = featureClass.Fields; IField field = null; // On a zero based index, iterate through the fields in the collection. for (int i = 0; i < fields.FieldCount; i++) { // Get the field at the given index. field = fields.get_Field(i); if (field.Name != field.AliasName) { listBox1.Items.Add(field.Name); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); }
... // Create a new ShapefileWorkspaceFactory CoClass to create a new workspace ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass(); // System.IO.Path.GetDirectoryName(shapefileLocation) returns the directory part of the string. Example: "C:\test\" ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(shapefileLocation), 0); // Explicit Cast // System.IO.Path.GetFileNameWithoutExtension(shapefileLocation) returns the base filename (without extension). Example: "cities" ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(shapefileLocation)); ....
Hi Feng
Thanks for your comment.I will test tour point tomorrow.Do you think I still need to instantiate workspaceFactory and featureWorkspace form IWorkspaceFactory and IWorkspace? Can I just instantiate the IFeatureClass and retrieve the fields of the shapefile?
Thanks again for your comment
Dim pGxDialog As New ESRI.ArcGIS.CatalogUI.GxDialog Dim pFilter As ESRI.ArcGIS.Catalog.IGxObjectFilter = New ESRI.ArcGIS.Catalog.GxFilterShapefiles Dim pSelection As ESRI.ArcGIS.Catalog.IEnumGxObject Dim pGxObject As ESRI.ArcGIS.Catalog.IGxObject Dim pFClass As ESRI.ArcGIS.Geodatabase.IFeatureClass With pGxDialog .AllowMultiSelect = False .ObjectFilter = pFilter End With If pGxDialog.DoModalOpen(My.ArcMap.Application.hWnd, pSelection) Then pSelection.Reset() pGxObject = pSelection.Next pFClass = pGxObject.InternalObjectName.Open End If