About the Working with packages in ArcGIS Engine Sample
[C#]
FrmMapControl.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.Win32; using System.Net.Sockets; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Carto; using System.IO; /// note: need to implement test is connected /// namespace WorkingWithPackages { public partial class FrmMapControl : Form { private string packageLocation = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\ArcGIS\Packages"; private string webMapLocation = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\ArcGIS\Web Maps"; private string myLayerPackageName = @"Bing Maps Aerial"; private string myMapPackageName = "Events"; private string myWebMapName = @"USA Topo Maps"; private enum PackageType { LayerPackage, MapPackage }; private FileInfo[] layerFiles; public FrmMapControl() { InitializeComponent(); } private void btnLoadlpk_Click(object sender, EventArgs e) { try { ILayerFile layerFile = new LayerFileClass(); // Test to see if we can connect to ArcGIS.com if (IsConnected()) { // If so, open the Layer Package from ArcGIS.com, this will get the most recent data. // for the package. If there is no change, the data will not get re-downloaded, // just use what is stored on disk. layerFile.Open(txtLayerPackage.Text.ToString()); } else { // If we cannot connect to ArcGIS.com use what was previously downloaded. if (DoesPackageExist(PackageType.LayerPackage)) { foreach (FileInfo layerpackage in layerFiles) { // Layer packages can have multiple layers included in them. However, // the LayerFile and MapDocument classes will only get the first one. // Here the sample is using an array so we can get all the layer files // and not worry about the name. layerFile.Open(layerpackage.FullName); } } } ILayer layer = layerFile.Layer; axMapControl1.AddLayer(layer); } catch (Exception ex) { MessageBox.Show("Failed to open Layer Package!", ex.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void btnLoadmpk_Click(object sender, EventArgs e) { try { IMapDocument mapDocument = new MapDocumentClass(); if (IsConnected()) { mapDocument.Open(txtMapPackage.Text, ""); } else { if (DoesPackageExist(PackageType.MapPackage)) { mapDocument.Open(packageLocation, ""); } } axMapControl1.Map = mapDocument.get_Map(0); } catch (Exception ex) { MessageBox.Show("Failed to open Map Package!", ex.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void btnWebMap_Click(object sender, EventArgs e) { if (!(IsConnected())) { // Since Web Maps require and internet connection anyways // just fail if there isn't one. MessageBox.Show("Cannot Connect to ArcGIS.com!", "Failed to open Web Map.", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { IMapDocument mapDocument = new MapDocumentClass(); string webMapMxd = webMapLocation + @"\" + myWebMapName + @"\" + myWebMapName + ".mxd"; if (File.Exists(webMapMxd)) mapDocument.Open(webMapMxd, ""); else mapDocument.Open(txtWebMap.Text, ""); axMapControl1.Map = mapDocument.get_Map(0); } catch (Exception ex) { MessageBox.Show("Failed to open Web Map!", ex.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error); } } private bool DoesPackageExist(PackageType packageType) { // Packages and Web Maps are handled by the FileHandler in Common Files\ArcGIS\bin // The default package location can be controled be the user. RegistryKey regKey = Registry.CurrentUser; RegistryKey subKey = regKey.OpenSubKey(@"Software\ESRI\ArcGIS File Handler\Settings"); int option = (int)subKey.GetValue("PackageLocationOption", 0); if (1 == option) { // Change the package location if different otherwise use the default. string fileHandlerUserSetting = subKey.GetValue(@"PackageLocation", "").ToString(); if ("" != fileHandlerUserSetting ) { packageLocation = fileHandlerUserSetting; } } switch (packageType) { case PackageType.LayerPackage: { // Layer Packages can have two different directories, depending on if the // package contains a single layer or multiple layers. DirectoryInfo directoryInfo; string layerPackageLocation = packageLocation + @"\" + myLayerPackageName.Replace(" ", "_"); if (Directory.Exists(layerPackageLocation)) { directoryInfo = new DirectoryInfo(layerPackageLocation); layerFiles = directoryInfo.GetFiles("*.lyr"); } else { layerPackageLocation = layerPackageLocation + @".lpk"; if (Directory.Exists(layerPackageLocation)) { directoryInfo = new DirectoryInfo(layerPackageLocation); layerFiles = directoryInfo.GetFiles("*.lyr"); } } if (layerFiles.Length > 0) { packageLocation = layerPackageLocation; return true; } break; } case PackageType.MapPackage: { packageLocation = packageLocation + @"\" + myMapPackageName.Replace(" ", "_") + @"\v10\" + myMapPackageName + ".mxd"; return File.Exists(packageLocation); } default: { return false; } } return false; } private bool IsConnected() { try { // Test to see if we can connect to ArcGIS.com TcpClient tcpClient = new TcpClient("www.arcgis.com", 80); tcpClient.Close(); return true; } catch (System.Exception ex) { return false; } } } }
[Visual Basic .NET]
FrmMapControl.vb
Option Explicit On Option Strict On Imports Microsoft.Win32 Imports System.IO Imports System.Net.Sockets Imports ESRI.ArcGIS.esriSystem Imports ESRI.ArcGIS.Carto Public Class FrmMapControl Private packageLocation As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\ArcGIS\Packages" Private webMapLocation As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\ArcGIS\Web Maps" Private myLayerPackageName As String = "Bing Maps Aerial" Private myMapPackageName As String = "Events" Private myWebMapName As String = "USA Topo Maps" Private layerFiles() As FileInfo Private Enum PackageType LayerPackage MapPackage End Enum Private Sub btnLoadlpk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadlpk.Click Try Dim layerFile As ILayerFile = New LayerFileClass() ' Test to see if we can connect to ArcGIS.com If IsConnected() Then ' If so, open the Layer Package from ArcGIS.com, this will get the most recent data. ' for the package. If there is no change, the data will not get re-downloaded, ' just use what is stored on disk. layerFile.Open(txtLayerPackage.Text) Else ' If we cannot connect to ArcGIS.com use what was previously downloaded. If DoesPackageExist(PackageType.LayerPackage) Then For Each layerpackage As FileInfo In layerFiles ' Layer packages can have multiple layers included in them. However, ' the LayerFile and MapDocument classes will only get the first one. ' Here the sample is using an array so we can get all the layer files ' and not worry about the name. layerFile.Open(layerpackage.FullName) Next End If End If Dim layer As ILayer = layerFile.Layer axMapControl1.AddLayer(layer) Catch ex As Exception MessageBox.Show("Failed to open Layer Package!", ex.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub Private Sub btnLoadmpk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadmpk.Click Try Dim mapDocument As IMapDocument = New MapDocument() If IsConnected() Then mapDocument.Open(txtMapPackage.Text, "") Else If DoesPackageExist(PackageType.MapPackage) Then mapDocument.Open(packageLocation, "") End If End If axMapControl1.Map = mapDocument.Map(0) Catch ex As Exception MessageBox.Show("Failed to open Map Package!", ex.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub Private Sub btnWebMap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWebMap.Click If Not IsConnected() Then ' Since Web Maps require and internet connection anyways ' just fail if there isn't one. MessageBox.Show("Cannot Connect to ArcGIS.com!", "Failed to open Web Map.", MessageBoxButtons.OK, MessageBoxIcon.Error) Return End If Try Dim mapDocument As IMapDocument = New MapDocument() Dim webMapMxd As String = webMapLocation + "\" + myWebMapName + "\" + myWebMapName + ".mxd" If File.Exists(webMapMxd) Then mapDocument.Open(webMapMxd, "") Else mapDocument.Open(txtWebMap.Text, "") End If axMapControl1.Map = mapDocument.Map(0) Catch ex As Exception MessageBox.Show("Failed to open Web Map!", ex.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub Private Function DoesPackageExist(ByVal packageType As PackageType) As Boolean ' Packages and Web Maps are handled by the FileHandler in Common Files\ArcGIS\bin ' The default package location can be controled be the user. Dim regKey As RegistryKey = Registry.CurrentUser Dim subKey As RegistryKey = regKey.OpenSubKey("Software\ESRI\ArcGIS File Handler\Settings") Dim regOption As Integer = CType(subKey.GetValue("PackageLocationOption", 0), Integer) If regOption = 1 Then ' Change the package location if different otherwise use the default. Dim fileHandlerUserSetting As String = subKey.GetValue("PackageLocation", "").ToString() If Not fileHandlerUserSetting = "" Then packageLocation = fileHandlerUserSetting End If End If Select Case packageType Case FrmMapControl.PackageType.LayerPackage ' Layer Packages can have two different directories, depending on if the ' package contains a single layer or multiple layers. Dim directoryInfo As DirectoryInfo Dim layerPackageLocation As String = packageLocation + "\" + myLayerPackageName.Replace(" ", "_") If Directory.Exists(layerPackageLocation) Then directoryInfo = New DirectoryInfo(layerPackageLocation) layerFiles = directoryInfo.GetFiles("*.lyr") Else layerPackageLocation = layerPackageLocation + ".lpk" If Directory.Exists(layerPackageLocation) Then directoryInfo = New DirectoryInfo(layerPackageLocation) layerFiles = directoryInfo.GetFiles("*.lyr") End If End If If (layerFiles.Length > 0) Then packageLocation = layerPackageLocation Return True End If Case FrmMapControl.PackageType.MapPackage packageLocation = packageLocation + "\" + myMapPackageName.Replace(" ", "_") + "\v10\" + myMapPackageName + ".mxd" Return File.Exists(packageLocation) Case Else Return False End Select Return False End Function Private Function IsConnected() As Boolean Try ' Test to see if we can connect to ArcGIS.com Dim tcpClient As TcpClient = New TcpClient("www.arcgis.com", 80) tcpClient.Close() Return True Catch ex As Exception Return False End Try End Function End Class