得到SDK 运行时库的位置的Code

本文介绍了一种通过C#代码获取指定.NET运行时版本安装目录的方法。利用GetRequestedRuntimeInfo函数,结合枚举参数和字符串构建器,可以返回.NET运行时的安装目录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们在编程时候,有时候要得到SDK运行时的所在目录。

请看下面:

 [Flags]
        private enum RuntimeInfo : uint
        {
            UpgradeVersion = 0x1,           // RUNTIME_INFO_UPGRADE_VERSION
            RequestIA64 = 0x2,              // RUNTIME_INFO_REQUEST_IA64
            RequestAmd64 = 0x4,             // RUNTIME_INFO_REQUEST_AMD64
            RequestX86 = 0x8,               // RUNTIME_INFO_REQUEST_X86
            DoNotReturnDirectory = 0x10,    // RUNTIME_INFO_DONT_RETURN_DIRECTORY
            DoNotReturnVersion = 0x20,      // RUNTIME_INFO_DONT_RETURN_VERSION
            DoNotShowErrorDialog = 0x40     // RUNTIME_INFO_DONT_SHOW_ERROR_DIALOG
        }

 

    [DllImport("mscoree.dll", CharSet = CharSet.Unicode, ExactSpelling = true, PreserveSig = true, SetLastError = false)]
        private static extern int /* [HRESULT] */ GetRequestedRuntimeInfo(
            string /* [LPCWSTR] */ pExe,
            string /* [LPCWSTR] */ pwszVersion,
            string /* [LPCWSTR] */ pConfigurationFile,
            uint /* [DWORD] */ startupFlags,
            RuntimeInfo /* [DWORD] */ runtimeInfoFlags,
            StringBuilder /* [LPWSTR] */ pDirectory,
            uint /* [DWORD] */ dwDirectory,
            out uint /* [DWORD *] */ dwDirectoryLength,
            StringBuilder /* [LPWSTR] */ pVersion,
            uint /* [DWORD] */ cchBuffer,
            out uint /* [DWORD *] */ dwLength
            );

 

/// <summary>
        /// Returns the installation directory of the specified .NET runtime.
        /// </summary>
        /// <param name="version">
        /// The version of the runtime.
        /// </param>
        /// <param name="upgradeVersion">
        /// True to return the installation directory of the nearest compatible runtime version, or false for an exact match.
        /// </param>
        /// <returns>
        /// The .NET runtime installation directory.
        /// </returns>
        private static string GetRuntimeInstallationDirectory(Version version, bool upgradeVersion)
        {
            string versionString = "v" + version.ToString(3);
            RuntimeInfo runtimeInfo = RuntimeInfo.DoNotShowErrorDialog;
            if (upgradeVersion)
                runtimeInfo |= RuntimeInfo.UpgradeVersion;

            StringBuilder runtimeDirectory = new StringBuilder(270);
            StringBuilder runtimeVersion = new StringBuilder("v65535.65535.65535".Length);
            uint runtimeDirectoryLength;
            uint runtimeVersionLength;
            int errorCode = GetRequestedRuntimeInfo(null, versionString, null, 0, runtimeInfo, runtimeDirectory, (uint)runtimeDirectory.Capacity, out runtimeDirectoryLength, runtimeVersion, (uint)runtimeVersion.Capacity, out runtimeVersionLength);
            Marshal.ThrowExceptionForHR(errorCode);
            return Path.Combine(runtimeDirectory.ToString(), runtimeVersion.ToString());
        }

 

样例可以是:

Version version = new Version(2, 0, 50727);

GetRuntimeInstallationDirectory(version, true) ;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值