如何以编程方式找到ArcGIS版本?


9

是否可以使用ArcObjects.net找出计算机上安装了哪个版本的ArcGIS(即9.3。,10.0、10.1)?


甚至注册表位置都将有所帮助。我只需要该程序一种方法来确定用户已安装的ArcGIS版本。文件路径不起作用,因为ArcGIS似乎没有卸载AppData文件夹中的旧文件夹
尼克

Answers:


8

在ArcObjects .NET中,使用RuntimeManager,例如:

列出所有已安装的运行时:

var runtimes = RuntimeManager.InstalledRuntimes;
foreach (RuntimeInfo runtime in runtimes)
{
  System.Diagnostics.Debug.Print(runtime.Path);
  System.Diagnostics.Debug.Print(runtime.Version);
  System.Diagnostics.Debug.Print(runtime.Product.ToString());
}

或者,仅获取当前活动的运行时:

bool succeeded = ESRI.ArcGIS.RuntimeManager.Bind(ProductCode.EngineOrDesktop);
if (succeeded)
{
  RuntimeInfo activeRunTimeInfo = RuntimeManager.ActiveRuntime;
  System.Diagnostics.Debug.Print(activeRunTimeInfo.Product.ToString());
}

同样在arcpy中,您可以使用GetInstallInfo


否决的理由?
blah238 2013年

我给了+1,所以当我也回头看时也很惊讶地看到0-我也喜欢您的ArcPy提醒。
PolyGeo

IIRC RuntimeManager是ArcGIS 10.0引入的,因此不能用于检测早期的ArcGIS版本。
stakx

ArcPy也是如此-在10.0之前的版本中尚不存在。
stakx

3

在Win7 64位PC上,此注册表项可能会有所帮助。我已经安装了10.0,它显示为10.0.2414。

\ HKLM \软件\ wow6432Node \ esri \ Arcgis \ RealVersion


1
当ArcObjects不可用时,此选项很有用,我在构建安装程序时使用。
凯利·托马斯

2
在32位上,此密钥为HKLM \ SOFTWARE \ ESRI \ ArcGIS \ RealVersion
mwalker 2013年

@mwalker同样在10.1的64位上,我看到HKLM \ SOFTWARE \ ESRI \ ArcGIS \ RealVersion,我想知道此密钥是否存在于10.0吗?
柯克·库肯达尔

@Kirk,我没有在10.1上的64位密钥—想知道为什么不这样做。
blah238 2013年

@ blah238我已经安装了台式机和服务器的10.1 sp1。不确定哪个安装创建了密钥。
Kirk Kuykendall


0

您还可以通过查询AfCore.dll的版本来获取ArcGIS版本。这需要知道ArcGIS安装目录,您可以通过查询注册表或通过硬编码获得该目录(大多数用户使用C:\ Program Files(x86)\ ArcGIS \ Desktop10.3 \)。

/// <summary>
/// Find the version of the currently installed ArcGIS Desktop
/// </summary>
/// <returns>Version as a string in the format x.x or empty string if not found</returns>
internal string GetArcGISVersion()
{
    try
    {
        FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(Path.Combine(Path.Combine(GetInstallDir(), "bin"), "AfCore.dll"));
        return string.Format("{0}.{1}", fvi.FileMajorPart, fvi.FileMinorPart);
    }
    catch (FileNotFoundException ex)
    {
        Console.WriteLine(string.Format("Could not get ArcGIS version: {0}. {1}", ex.Message, ex.StackTrace));
    }

    return "";
}

/// <summary>
/// Look in the registry to find the install directory of ArcGIS Desktop.
/// Searches in reverse order, so latest version is returned.
/// </summary>
/// <returns>Dir name or empty string if not found</returns>
private string GetInstallDir()
{
    string installDir = "";
    string esriKey = @"Software\Wow6432Node\ESRI";

    foreach (string subKey in GetHKLMSubKeys(esriKey).Reverse())
    {
        if (subKey.StartsWith("Desktop"))
        {
            installDir = GetRegValue(string.Format(@"HKEY_LOCAL_MACHINE\{0}\{1}", esriKey, subKey), "InstallDir");
            if (!string.IsNullOrEmpty(installDir))
                return installDir;
        }
    }

    return "";
}

/// <summary>
/// Returns all the subkey names for a registry key in HKEY_LOCAL_MACHINE
/// </summary>
/// <param name="keyName">Subkey name (full path excluding HKLM)</param>
/// <returns>An array of strings or an empty array if the key is not found</returns>
private string[] GetHKLMSubKeys(string keyName)
{
    using (RegistryKey tempKey = Registry.LocalMachine.OpenSubKey(keyName))
    {
        if (tempKey != null)
            return tempKey.GetSubKeyNames();

        return new string[0];
    }
}

/// <summary>
/// Reads a registry key and returns the value, if found.
/// Searches both 64bit and 32bit registry keys for chosen value
/// </summary>
/// <param name="keyName">The registry key containing the value</param>
/// <param name="valueName">The name of the value</param>
/// <returns>string representation of the actual value or null if value is not found</returns>
private string GetRegValue(string keyName, string valueName)
{
    object regValue = null;

    regValue = Registry.GetValue(keyName, valueName, null);
    if (regValue != null)
    {
        return regValue.ToString();
    }

    // try again in 32bit reg
    if (keyName.Contains("HKEY_LOCAL_MACHINE"))
        regValue = Registry.GetValue(keyName.Replace(@"HKEY_LOCAL_MACHINE\Software", @"HKEY_LOCAL_MACHINE\Software\Wow6432Node"), valueName, null);
    else if (keyName.Contains("HKEY_CURRENT_USER"))
        regValue = Registry.GetValue(keyName.Replace(@"HKEY_CURRENT_USER\Software", @"HKEY_CURRENT_USER\Software\Wow6432Node"), valueName, null);
    if (regValue != null)
    {
        return regValue.ToString();
    }

    return "";
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.