AnsweredAssumed Answered

How to set IFeatureClass instance onOpenFileDialog()

Question asked by bgeoca on Jul 25, 2012
Latest reply on Jul 30, 2012 by kenbuja
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());                 } 

Outcomes