当AE二次开发与其他平台一起使用的时候,有时候会不方便加LicenseControl控件,需要用代码初始化许可:
在构造函数中初始化许可:
1 public Form1() 2 { 3 4 InitializeComponent(); 5 ESRI.ArcGIS.esriSystem.IAoInitialize AoIni = new AoInitializeClass(); 6 bool Successful;//返回是否初始化成功 7 esriLicenseProductCode production = esriLicenseProductCode.esriLicenseProductCodeArcInfo;//这里选择的是ArcInfo 8 esriLicenseStatus status = AoIni.IsProductCodeAvailable(production); 9 if (status == esriLicenseStatus.esriLicenseAvailable) 10 { 11 status = AoIni.Initialize(production); 12 if (status == esriLicenseStatus.esriLicenseAlreadyInitialized || status == esriLicenseStatus.esriLicenseCheckedOut) 13 { 14 //SpatialAnalyst 15 esriLicenseExtensionCode extension1 = esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst; 16 esriLicenseStatus status1 = AoIni.IsExtensionCodeAvailable(production, extension1); 17 if (status1 == esriLicenseStatus.esriLicenseAvailable) 18 { 19 status1 = AoIni.CheckOutExtension(extension1); 20 } 21 22 //DataInteroperability 23 esriLicenseExtensionCode extension2 = esriLicenseExtensionCode.esriLicenseExtensionCodeDataInteroperability; 24 esriLicenseStatus status2 = AoIni.IsExtensionCodeAvailable(production, extension2); 25 if (status2 == esriLicenseStatus.esriLicenseAvailable) 26 { 27 status2 = AoIni.CheckOutExtension(extension2); 28 } 29 30 //if (status1 == esriLicenseStatus.esriLicenseCheckedOut && status2 == esriLicenseStatus.esriLicenseCheckedOut) 31 if (status2 == esriLicenseStatus.esriLicenseCheckedOut) 32 { 33 Successful = true; 34 } 35 } 36 } 37 }
以上代码相当于添加LicenseControl控件,并设置如下属性:
后记:用代码初始化许可的话,记得在程序关闭时添加如下代码,否则程序进程在程序关闭后还存在。
public static void ShutDown()
{
ESRI.ArcGIS.ADF.COMSupport.AOUninitialize.Shutdown();
AoInit.Shutdown(); //AoInit为初始化许可时定义的ESRI.ArcGIS.esriSystem.IAoInitialize AoIni
}