This document is archived and information here might be outdated.  Recommended version.


Open Network Dataset Snippet (ArcObjects .NET 10.5 SDK)
ArcObjects Library Reference

Open Network Dataset Snippet

Opens and returns a network dataset.

[C#]
///<summary>Opens and returns a network dataset.</summary>
///
///<param name="networkDatasetWorkspace">An IWorkspace interface that contains the network dataset</param>
///<param name="networkDatasetName">A System.String that is the name of the network dataset. Example: "roads"</param>
///<param name="featureDatasetName">A System.String that is the name of the feature dataset that contains the network dataset. This name is only required for geodatabase workspaces. An empty string may be passed in for shapefile/SDC workspaces. Example: "Highways" or "".</param>
///
///<returns>The INetworkDataset interface of the opened network dataset</returns>
/// 
///<remarks></remarks>
public ESRI.ArcGIS.Geodatabase.INetworkDataset OpenNetworkDataset(ESRI.ArcGIS.Geodatabase.IWorkspace networkDatasetWorkspace, System.String networkDatasetName, System.String featureDatasetName)
{
  if (networkDatasetWorkspace == null || networkDatasetName == "" || featureDatasetName == null)
  {
    return null;
  }

  ESRI.ArcGIS.Geodatabase.IDatasetContainer3 datasetContainer3=null;
  switch (networkDatasetWorkspace.Type)
  {
    case ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriFileSystemWorkspace:

      // Shapefile or SDC network dataset workspace
      ESRI.ArcGIS.Geodatabase.IWorkspaceExtensionManager workspaceExtensionManager=networkDatasetWorkspace as ESRI.ArcGIS.Geodatabase.IWorkspaceExtensionManager; // Dynamic Cast
      ESRI.ArcGIS.esriSystem.UID networkID=new ESRI.ArcGIS.esriSystem.UIDClass();

      networkID.Value="esriGeoDatabase.NetworkDatasetWorkspaceExtension";
      ESRI.ArcGIS.Geodatabase.IWorkspaceExtension workspaceExtension=workspaceExtensionManager.FindExtension(networkID);
      datasetContainer3=workspaceExtension as ESRI.ArcGIS.Geodatabase.IDatasetContainer3; // Dynamic Cast
      break;

    case ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriLocalDatabaseWorkspace:

      // Personal Geodatabase or File Geodatabase network dataset workspace

    case ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriRemoteDatabaseWorkspace:

      // SDE Geodatabase network dataset workspace
      ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace=networkDatasetWorkspace as ESRI.ArcGIS.Geodatabase.IFeatureWorkspace; // Dynamic Cast
      ESRI.ArcGIS.Geodatabase.IFeatureDataset featureDataset=featureWorkspace.OpenFeatureDataset(featureDatasetName);
      ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtensionContainer featureDatasetExtensionContainer=featureDataset as ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtensionContainer; // Dynamic Cast
      ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtension featureDatasetExtension=featureDatasetExtensionContainer.FindExtension(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTNetworkDataset);
      datasetContainer3=featureDatasetExtension as ESRI.ArcGIS.Geodatabase.IDatasetContainer3; // Dynamic Cast
      break;
  }

  if (datasetContainer3 == null)
    return null;

  ESRI.ArcGIS.Geodatabase.IDataset dataset=datasetContainer3.get_DatasetByName(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTNetworkDataset, networkDatasetName);

  return dataset as ESRI.ArcGIS.Geodatabase.INetworkDataset; // Dynamic Cast
}
[Visual Basic .NET]
'''<summary>Opens and returns a network dataset.</summary>
'''
'''<param name="networkDatasetWorkspace">An IWorkspace interface that contains the network dataset</param>
'''<param name="networkDatasetName">A System.String that is the name of the network dataset. Example: "roads"</param>
'''<param name="featureDatasetName">A System.String that is the name of the feature dataset that contains the network dataset. This name is only required for geodatabase workspaces. An empty string may be passed in for shapefile/SDC workspaces. Example: "Highways" or "".</param>
'''
'''<returns>The INetworkDataset interface of the opened network dataset</returns>
''' 
'''<remarks></remarks>
Public Shared Function OpenNetworkDataset(ByVal networkDatasetWorkspace As ESRI.ArcGIS.Geodatabase.IWorkspace, ByVal networkDatasetName As System.String, ByVal featureDatasetName As System.String) As ESRI.ArcGIS.Geodatabase.INetworkDataset

  If networkDatasetWorkspace Is Nothing OrElse networkDatasetName="" OrElse featureDatasetName Is Nothing Then
    Return Nothing
  End If

  Dim datasetContainer3 As ESRI.ArcGIS.Geodatabase.IDatasetContainer3=Nothing
  Select Case networkDatasetWorkspace.Type
    Case ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriFileSystemWorkspace

      ' Shapefile or SDC network dataset workspace
      Dim workspaceExtensionManager As ESRI.ArcGIS.Geodatabase.IWorkspaceExtensionManager=TryCast(networkDatasetWorkspace, ESRI.ArcGIS.Geodatabase.IWorkspaceExtensionManager) ' Dynamic Cast
      Dim networkID As ESRI.ArcGIS.esriSystem.UID=New ESRI.ArcGIS.esriSystem.UIDClass()
      networkID.Value="esriGeoDatabase.NetworkDatasetWorkspaceExtension"

      Dim workspaceExtension As ESRI.ArcGIS.Geodatabase.IWorkspaceExtension=workspaceExtensionManager.FindExtension(networkID)
      datasetContainer3=TryCast(workspaceExtension, ESRI.ArcGIS.Geodatabase.IDatasetContainer3) ' Dynamic Cast

    Case ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriLocalDatabaseWorkspace, ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriRemoteDatabaseWorkspace

      ' Personal Geodatabase or File Geodatabase network dataset workspace

      ' SDE Geodatabase network dataset workspace
      Dim featureWorkspace As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace=TryCast(networkDatasetWorkspace, ESRI.ArcGIS.Geodatabase.IFeatureWorkspace) ' Dynamic Cast
      Dim featureDataset As ESRI.ArcGIS.Geodatabase.IFeatureDataset=featureWorkspace.OpenFeatureDataset(featureDatasetName)
      Dim featureDatasetExtensionContainer As ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtensionContainer=TryCast(featureDataset, ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtensionContainer) ' Dynamic Cast
      Dim featureDatasetExtension As ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtension=featureDatasetExtensionContainer.FindExtension(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTNetworkDataset)
      datasetContainer3=TryCast(featureDatasetExtension, ESRI.ArcGIS.Geodatabase.IDatasetContainer3) ' Dynamic Cast

  End Select

  If datasetContainer3 Is Nothing Then
    Return Nothing
  End If

  Dim dataset As ESRI.ArcGIS.Geodatabase.IDataset=datasetContainer3.DatasetByName(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTNetworkDataset, networkDatasetName)

  Return TryCast(dataset, ESRI.ArcGIS.Geodatabase.INetworkDataset) ' Dynamic Cast

End Function

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.Geodatabase
  • ESRI.ArcGIS.System