Steps:
1) create a workspace Factory
2) Open the shapefile workspace.
3) Open the element class
4) create a element layer
5) Associate layers and element classes
6) Add to map Space
Code (The following figure shows the corresponding steps ):
Iworkspacefactory pworkspacefactory = new shapefileworkspacefactory (); // 1
Openfiledialog1.filter = "shaperfile (*. SHP) | *. SHP ";
Openfiledialog1.initialdirectory = @ "E: \ test \ document and data \ data ";
Openfiledialog1.multiselect = false;
Dialogresult pdialogresult = openfiledialog1.showdialog ();
If (pdialogresult! = Dialogresult. OK)
Return;
String ppath = openfiledialog1. filename;
String pfolder = path. getdirectoryname (ppath );
String pfilename = path. getfilename (ppath );
Iworkspace pworkspace = pworkspacefactory. openfromfile (pfolder, 0); // 2
Ifeatureworkspace pfeatureworkspace = pworkspace as ifeatureworkspace;
Ifeatureclass PFC = pfeatureworkspace. openfeatureclass (pfilename); // 3
Ifeaturelayer pflayer = new featurelayerclass (); // 4
Pflayer. featureclass = PFC;
Pflayer. Name = PFC. aliasname; // 5
Ilayer player = pflayer as ilayer;
IMAP pmap = axmapcontrol1.map;
Pmap. addlayer (player); // 6
Axmapcontrol1.activeview. Refresh ();
Bytes -----------------------------------------------------------------------------------------------------------
// Add the ArcGIS namespace
Using ESRI. ArcGIS. CARTO;
Using ESRI. ArcGIS. geometry;
Using ESRI. ArcGIS. Geodatabase;
Using ESRI. ArcGIS. datasourcesfile;
Using ESRI. ArcGIS. datasourcesraster;
// Store the full path of open files
String fullfilepath;
// Set the openfiledialog attribute so that it can open multiple types of files
Openfiledialog openfile = new openfiledialog ();
Openfile. Filter = "shape file (*. SHP) | *. SHP | ";
Openfile. Filter + = "raster data (*. jpg, *. BMP, *. Tiff) | *. jpg; *. BMP; *. Tiff | ";
Openfile. Filter + = "map document (*. mxd, *. MXT, *. JMF) | *. mxd; *. MXT; *. JMF ";
Openfile. Title = "Open File ";
Try
{
If (openfile. showdialog () = dialogresult. OK)
{
Fullfilepath = openfile. filename;
// Obtain the file path
Int Index = fullfilepath. lastindexof ("\\");
String filepath = fullfilepath. substring (0, index );
// Obtain the file name
String filenam = fullfilepath. substring (index + 1 );
// Load the shape File
If (openfile. filterindex = 1)
{
// Open the workspace Factory
Iworkspacefactory workspcfac = new shapefileworkspacefactory ();
Ifeatureworkspace featureworkspc;
Ifeaturelayer featurelay = new featurelayerclass ();
// Open path
Featureworkspc = workspcfac. openfromfile (filepath, 0) as ifeatureworkspace;
// Open the class Element
Featurelay. featureclass = featureworkspc. openfeatureclass (filenam );
// Clear the Layer
Axmapcontrol1.clearlayers ();
// Add a Layer
Axmapcontrol1.addlayer (featurelay );
Axmapcontrol1.refresh ();
}
// Load the Raster image
Else if (openfile. filterindex = 2)
{
Iworkspacefactory workspcfac = new rasterworkspacefactory ();
Irasterworkspace rasterworkspc;
Irasterdataset rasterdatst = new rasterdatasetclass ();
Irasterlayer rasterlay = new rasterlayerclass ();
Rasterworkspc = workspcfac. openfromfile (filepath, 0) as irasterworkspace;
Rasterdatst = rasterworkspc. openrasterdataset (filenam );
Rasterlay. createfromdataset (rasterdatst );
Axmapcontrol1.clearlayers ();
Axmapcontrol1.addlayer (rasterlay );
Axmapcontrol1.refresh ();
}
// Load the map document
Else if (openfile. filterindex = 3)
{
Imapdocument mapdoc = new mapdocumentclass ();
Mapdoc. Open (filepath ,"");
Axmapcontrol1.clearlayers ();
For (INT I = 0; I <mapdoc. mapcount-1; I ++)
{
Axmapcontrol1.map = mapdoc. get_map (I );
}
Iactiveview activeviw = axmapcontrol1.map as iactiveview;
Activeviw. extent = axmapcontrol1.fullextent;
Axmapcontrol1.refresh ();
}
}
}
Catch (exception ex)
{
MessageBox. Show (ex. Message. tostring ());
}
}
}