7

我有一些 Azure Compute Emulator 无法正确重新启动的问题。为了解决这个问题,我想csrun /devfabric:stop在 Visual Studio 解决方案中添加对预构建步骤的调用。

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

有什么方法可以推断出路径,比如使用一些环境变量或类似的东西?

4

2 回答 2

6

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

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

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

于 2011-10-04T12:24:31.550 回答
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") + '"'
}
于 2014-11-16T11:58:37.050 回答