其中mcView即AxMapControl控件
private void addshp_Click(object sender, EventArgs e)
{
try
{
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
IFeatureWorkspace pFeatureWorkspace = null;
IFeatureLayer pFeatureLayer2 = new FeatureLayerClass();
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "shp文件(*.shp)|*.shp";
if (ofd.ShowDialog() == DialogResult.OK)
{
mcView.ClearLayers();
string fullpath = ofd.FileName;
int index = fullpath.LastIndexOf("\\");
string path = fullpath.Substring(0, index);
string name = fullpath.Substring(index + 1);
pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(path, 0);
pFeatureLayer2.FeatureClass = pFeatureWorkspace.OpenFeatureClass(name);
mcView.AddLayer(pFeatureLayer2);
mcView.Refresh();
}
}
catch (Exception)
{
}