如何方便地推导出Azure SDK csrun.exe的路径?

时间:2011-10-04 12:16:56

标签: windows visual-studio-2010 azure azure-compute-emulator

我有一些Azure Compute Emulator无法正常重启的问题。要解决此问题,我想将csrun /devfabric:stop调用添加到Visual Studio解决方案中的预构建步骤。

问题是csrun.exe位于我的计算机上的C:\Program Files\Windows Azure SDK\v1.4\bin,该路径不在%PATH%目录列表中。我不想在我的解决方案中对该路径进行硬编码。

是否有某种方法可以推断使用某些环境变量或类似的路径?

2 个答案:

答案 0 :(得分:6)

您可以按版本从注册表中读取Azure SDK路径。路径的最后一部分是版本......您的代码可以设置为版本,也可以迭代查找最新版本的v键。我建议您为支持的版本设置一个常量,并将新SDK作为预先请求。

HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Microsoft SDKs \ ServiceHosting \ v1.4

这些路径下有一个“InstallPath”键。

答案 1 :(得分:0)

我遇到了同样的问题,我制作了一个PowerShell脚本,该脚本使用SDK bin文件夹的路径设置环境变量。它将自动搜索注册表并找到最新安装的版本。它还具有备用注册表位置的回退,具体取决于您的脚本是以32位还是64位模式运行。希望它有所帮助!

免责声明:我在发布之前已从脚本中删除了一些内容,之后我没有对其进行测试,但我认为根据您的需要调试/调整它并不困难。

#the script attempts to perform the following:
#1. look for the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\ServiceHosting" registry key
#2. if the above key is present then read the child keys and retrieve the largest version number
#3. from the largest version number key retrieve the "InstallPath" string value to determine the path of the latest Azure SDK installation
#4. add an environment variable called "AzureSDKBin" (if not already added) with the path to the "bin" folder of the latest Azure SDK installation

#define the name of the config variable
$azureSDKPathVariable = 'AzureSDKBin'
$azureRegistryKey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\ServiceHosting'
$azureAlternateRegistryKey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SDKs\ServiceHosting' #this is in case the PowerShell runs in 32bit mode on a 64bit machine
$azureMatchedKey = ''

#check if the environment variable was already defined
if ([environment]::GetEnvironmentVariable($azureSDKPathVariable,"User").Length -eq 0) {
    'Variable ' + $azureSDKPathVariable + ' is not defined, proceeding...'

    #try reading the registry key
    $keyExists = Get-Item -Path Registry::$azureRegistryKey -ErrorAction SilentlyContinue

    $azureMatchedKey = $azureRegistryKey #make a note that we found this registry key

    #stop if the key does not exist
    if ($keyExists.Length -eq 0) {
        'Could not find registry key in primary location: ' + $azureRegistryKey + ', attempting search in alternate location: ' + $azureAlternateRegistryKey

        #search the alternate location
        $keyExists = Get-Item -Path Registry::$azureAlternateRegistryKey -ErrorAction SilentlyContinue

        $azureMatchedKey = $azureAlternateRegistryKey #make a note that we found this registry key

        if ($keyExists.Length -eq 0) {
            'Could not find registry key for determining Azure SDK installation: ' + $azureAlternateRegistryKey
            'Script failed...'
            exit 1
        }
    }

    'Found Azure SDK registry key: ' + $azureMatchedKey

    #logic for determining the install path of the latest Azure installation
    #1. get all child keys of the matched key
    #2. filter only keys that start with "v" (e.g. "v2.2", "v2.3")
    #3. sort the results by the "PSChildName" property from which we removed the starting "v" (i.e. only the version number), descending so we get the latest on the first position
    #4. only keep the first object
    #5. read the value named "InstallPath" under this object
    $installPath = (Get-ChildItem -Path Registry::$azureMatchedKey | Where-Object { $_.PSChildName.StartsWith("v") } | sort @{expression={ $_.PSChildName.TrimStart("v") }} -descending | Select-Object -first 1| Get-ItemProperty -name InstallPath).InstallPath

    'Detected this Azure SDK installation path: "' + $installPath + '"'

    #set the variable with the "bin" folder
    [Environment]::SetEnvironmentVariable($azureSDKPathVariable, $installPath + 'bin\', "User")

    'Assigned the value "' + [environment]::GetEnvironmentVariable($azureSDKPathVariable,"User") + '" to environment variable "' + $azureSDKPathVariable + '"'
}
else {
    'Environment variable "' + $azureSDKPathVariable + '" is already defined and has a value of "' + [environment]::GetEnvironmentVariable($azureSDKPathVariable,"User") + '"'
}