/*说明:C#中添加多个shp文件,默认对象为axMap1,如果名称改变只需修改最后一句即可
时间:2006-08-17
作者:gisgeoboy
邮箱/MSN:gisskystar@126.com
注:需要引用System.Windows.Forms和ESRI.MapObjects2.Core;
*/
public void AddShpFile()
{
string FilePath ,FileNameNoExtend ;//定义文件路径,没有扩展的文件名称
int Position ; //路径中最后一个“\”的位置
DataConnection dc=new DataConnectionClass();//定义连接
MapLayer layer = null;
ESRI.MapObjects2.Core.GeoDataset gds = null;
OpenFileDialog openShpFile = new OpenFileDialog();
openShpFile.Multiselect=true;
openShpFile.Filter="shp文件(*.shp)|*.shp";
openShpFile.RestoreDirectory=true;
if( openShpFile.ShowDialog() == DialogResult.OK )
{
if(( openShpFile.OpenFile()) != null )
{
string[] fileNames=openShpFile.FileNames;
Position=fileNames[0].LastIndexOf( "\\" );
FilePath=fileNames[0].Substring( 0,Position );
dc.Database=FilePath;
dc.Connect();
if (dc.Connected == true)
{
foreach( string shpFileWithPath in fileNames )
{
FileNameNoExtend=shpFileWithPath.Substring(Position+1,shpFileWithPath.Length-Position-5);
gds = dc.FindGeoDataset(FileNameNoExtend);
if ( gds != null )
{
layer = new MapLayerClass();
layer.GeoDataset = gds;
axMap1.Layers.Add ( layer );
}
}
}
}
}
}