Ejemplo n.º 1
0
        internal static string GetRuntimeVersion(string path)
        {
#if FEATURE_MSCOREE
            if (NativeMethodsShared.IsWindows)
            {
#if DEBUG
                // Just to make sure and exercise the code that uses dwLength to allocate the buffer
                // when GetRequestedRuntimeInfo fails due to insufficient buffer size.
                int bufferLength = 1;
#else
                int bufferLength = 11; // 11 is the length of a runtime version and null terminator v2.0.50727/0
#endif

                unsafe
                {
                    // Allocate an initial buffer
                    char *runtimeVersion = stackalloc char[bufferLength];

                    // Run GetFileVersion, this should succeed using the initial buffer.
                    // It also returns the dwLength which is used if there is insufficient buffer.
                    uint hresult = NativeMethods.GetFileVersion(path, runtimeVersion, bufferLength, out int dwLength);

                    if (hresult == NativeMethodsShared.ERROR_INSUFFICIENT_BUFFER)
                    {
                        // Allocate new buffer based on the returned length.
                        char *runtimeVersion2 = stackalloc char[dwLength];
                        runtimeVersion = runtimeVersion2;

                        // Get the RuntimeVersion in this second call.
                        bufferLength = dwLength;
                        hresult      = NativeMethods.GetFileVersion(path, runtimeVersion, bufferLength, out dwLength);
                    }

                    return(hresult == NativeMethodsShared.S_OK ? new string(runtimeVersion, 0, dwLength - 1) : string.Empty);
                }
            }
            else
            {
                return(ManagedRuntimeVersionReader.GetRuntimeVersion(path));
            }
#else
            return(ManagedRuntimeVersionReader.GetRuntimeVersion(path));
#endif
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Given a path get the CLR runtime version of the file
        /// </summary>
        /// <param name="path">path to the file</param>
        /// <returns>The CLR runtime version or empty if the path does not exist.</returns>
        internal static string GetRuntimeVersion(string path)
        {
#if FEATURE_MSCOREE
            if (NativeMethodsShared.IsWindows)
            {
                StringBuilder runtimeVersion   = null;
                uint          hresult          = 0;
                uint          actualBufferSize = 0;
#if _DEBUG
                // Just to make sure and exercise the code that doubles the size
                // every time GetRequestedRuntimeInfo fails due to insufficient buffer size.
                int bufferLength = 1;
#else
                int bufferLength = 11; // 11 is the length of a runtime version and null terminator v2.0.50727/0
#endif
                do
                {
                    runtimeVersion = new StringBuilder(bufferLength);
                    hresult        = NativeMethods.GetFileVersion(path, runtimeVersion, bufferLength, out actualBufferSize);
                    bufferLength   = bufferLength * 2;
                } while (hresult == NativeMethodsShared.ERROR_INSUFFICIENT_BUFFER);

                if (hresult == NativeMethodsShared.S_OK && runtimeVersion != null)
                {
                    return(runtimeVersion.ToString());
                }
                else
                {
                    return(String.Empty);
                }
            }
            else
            {
                return(ManagedRuntimeVersionReader.GetRuntimeVersion(path));
            }
#else
            return(ManagedRuntimeVersionReader.GetRuntimeVersion(path));
#endif
        }