How to set IFeatureClass instance onOpenFileDialog()

2502
5
Jump to solution
07-25-2012 02:42 PM
BernardoG_
New Contributor II
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());                 } 
0 Kudos
1 Solution

Accepted Solutions
FengZhang2
Occasional Contributor
Please refer to the following code snippets:

-- Add Shapefile Using OpenFileDialog Snippet --
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#//00490000005m000000

Basically, you do like this:

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());                 } 

View solution in original post

0 Kudos
5 Replies
FengZhang2
Occasional Contributor
Please refer to the following code snippets:

-- Add Shapefile Using OpenFileDialog Snippet --
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#//00490000005m000000

Basically, you do like this:

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());                 } 
0 Kudos
BernardoG_
New Contributor II
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
0 Kudos
FengZhang2
Occasional Contributor
Yes, you still need to create IWorkspaceFactory and IFeatureWorkspace objects as shown in the sample code.

...
// 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
0 Kudos
BernardoG_
New Contributor II
Perfect,
It works now.
Thanks,
0 Kudos
KenBuja
MVP Esteemed Contributor
Have you thought about using the IGxDialog class to open the dataset? This way, you don't have to worry about getting the correct workspace and factory. Here's an example in VB.NET

        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