Here's a few subroutines I use in creating new feature classes with an existing template and adding fields.
A few notes about the code. I have created a global variable for the geoprocessor (Globals.GP) and initiated that elsewhere. Also you should be able to use an IFeatureClass as the input for the template property of the CreateFeatureclass tool, but the bug NIM068724 (which may be fixed in 10.1) currently prevents this and you have to use the path name for the feature class.
Public Sub AddField(ByVal Input As Object, ByVal FieldName As String, ByVal FieldType As String, Optional ByVal Precision As Long = 0,
Optional ByVal Scale As Long = 0, Optional ByVal Length As Long = 0, Optional ByVal FieldAlias As String = Nothing,
Optional ByVal Nullable As String = "NULLABLE", Optional ByVal Required As String = "NON_REQUIRED", Optional ByVal Domain As String = Nothing)
Dim AddField As New ESRI.ArcGIS.DataManagementTools.AddField
Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2
Try
Using releaser As New ESRI.ArcGIS.ADF.ComReleaser
releaser.ManageLifetime(AddField)
AddField.in_table = Input
AddField.field_name = FieldName
AddField.field_type = FieldType
AddField.field_precision = Precision
AddField.field_scale = Scale
AddField.field_length = Length
AddField.field_alias = FieldAlias
AddField.field_is_nullable = Nullable
AddField.field_is_required = Required
AddField.field_domain = Domain
Result = RunTool(AddField, Nothing)
If Result Is Nothing Then
System.Windows.Forms.MessageBox.Show("Could not add field")
End If
End Using
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.ToString, "Add Field error")
End Try
End Sub
Private Function CreateFeatureClass(ByVal PathString As String, ByVal Name As String, ByVal pTemplateFClass As ESRI.ArcGIS.Geodatabase.IFeatureClass) As ESRI.ArcGIS.Geodatabase.IFeatureClass
Dim CreateFC As New ESRI.ArcGIS.DataManagementTools.CreateFeatureclass
Dim ShapeType As String
Dim dataset As ESRI.ArcGIS.Geodatabase.IDataset
Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2
Dim Path As String
Try
Using releaser As New ESRI.ArcGIS.ADF.ComReleaser
releaser.ManageLifetime(CreateFC)
Select Case pTemplateFClass.ShapeType
Case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint
ShapeType = "Point"
Case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline
ShapeType = "Line"
Case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon
ShapeType = "Polygon"
Case Else
Return Nothing
End Select
dataset = pTemplateFClass
Path = dataset.Workspace.PathName & "\" & dataset.Name
If dataset.Workspace.WorkspaceFactory.WorkspaceType = ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriFileSystemWorkspace Then
If TypeOf dataset.Workspace.WorkspaceFactory Is ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory Then
Path = Path & ".shp"
End If
End If
Dim pSR As ESRI.ArcGIS.Geometry.ISpatialReference3 = GetSpatialReferenceFromDataset(pTemplateFClass)
CreateFC.out_path = PathString
CreateFC.out_name = Name
CreateFC.geometry_type = ShapeType
CreateFC.template = Path
CreateFC.spatial_reference = GetSpatialReferenceFromDataset(pTemplateFClass)
Result = RunTool(CreateFC, Nothing)
If Result Is Nothing Then
System.Windows.Forms.MessageBox.Show("Could not create dataset")
Return Nothing
End If
Return ReturnObjectfromResult(Result, "Feature Class")
End Using
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.ToString, "Create InMemory Featureclass error")
Return Nothing
End Try
End Function
Public Function GetSpatialReferenceFromDataset(ByVal dataset As ESRI.ArcGIS.Geodatabase.IDataset) As ESRI.ArcGIS.Geometry.ISpatialReference3
'If the dataset supports IGeoDataset
If TypeOf dataset Is ESRI.ArcGIS.Geodatabase.IGeoDataset Then
'then grab the spatial reference information and return it.
Dim geoDataset As ESRI.ArcGIS.Geodatabase.IGeoDataset = CType(dataset, ESRI.ArcGIS.Geodatabase.IGeoDataset)
Return geoDataset.SpatialReference
Else
Return Nothing 'otherwise return nothing
End If
End Function
Friend Function ReturnObjectfromResult(ByVal result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2, ByVal Type As String) As Object
Dim GPVal As ESRI.ArcGIS.Geodatabase.IGPValue
Dim InMemFC As String
Dim GPUtil As ESRI.ArcGIS.Geoprocessing.IGPUtilities3 = New ESRI.ArcGIS.Geoprocessing.GPUtilities
Try
GPVal = result.GetOutput(0)
InMemFC = GPVal.GetAsText()
Select Case Type
Case "Feature Class"
Return GPUtil.OpenFeatureClassFromString(InMemFC)
Case "Table"
Return GPUtil.OpenTableFromString(InMemFC)
End Select
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.ToString, "Return FeatureClass error")
Return Nothing
End Try
End Function
Friend Function RunTool(ByVal Process As ESRI.ArcGIS.Geoprocessor.IGPProcess, ByVal TC As ESRI.ArcGIS.esriSystem.ITrackCancel2) As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2
Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2
Try
Result = CType(Globals.GP.Execute(Process, Nothing), ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2)
If Result.Status <> ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded Then ReturnMessages(Result, "Geoprocessing Error")
Globals.GP.ClearMessages()
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.ToString, "Run Geoprocessor")
End Try
Return Result
End Function