Apakah ada cara mudah untuk memeriksa versi .NET Framework?

86

Masalahnya adalah saya perlu tahu apakah itu versi 3.5 SP 1. Environment.Version()hanya kembali 2.0.50727.3053.

Saya menemukan solusi ini , tetapi saya pikir ini akan memakan lebih banyak waktu daripada nilainya, jadi saya mencari yang lebih sederhana. Apa itu mungkin?

Carlo
sumber
2
Tanpa investigasi lebih lanjut, Environment.Version()mungkin hanya mengembalikan versi CLR.
Cecil Memiliki Nama
Environment.Version () memberikan 2.0meskipun berjalan dengan 3.5
LCJ
Saya menggunakan pendekatan yang disebutkan di stackoverflow.com/questions/15128426/…
LCJ
1
Cara: Menentukan Versi .NET Framework Yang Dipasang (termasuk juga solusi programatik)
informatik01
2
@Lijo, untuk NET 3.5, versi CLR adalah pada kenyataannya 2.0.
Mathias Lykkegaard Lorenzen

Jawaban:

107

Sesuatu seperti ini harus melakukannya. Ambil saja nilainya dari registri

Untuk .NET 1-4 :

Frameworkadalah versi terinstal tertinggi, SPadalah paket layanan untuk versi tersebut.

RegistryKey installed_versions = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP");
string[] version_names = installed_versions.GetSubKeyNames();
//version names start with 'v', eg, 'v3.5' which needs to be trimmed off before conversion
double Framework = Convert.ToDouble(version_names[version_names.Length - 1].Remove(0, 1), CultureInfo.InvariantCulture);
int SP = Convert.ToInt32(installed_versions.OpenSubKey(version_names[version_names.Length - 1]).GetValue("SP", 0));

Untuk .NET 4.5+ (dari dokumentasi resmi ):

using System;
using Microsoft.Win32;


...


private static void Get45or451FromRegistry()
{
    using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\")) {
        int releaseKey = Convert.ToInt32(ndpKey.GetValue("Release"));
        if (true) {
            Console.WriteLine("Version: " + CheckFor45DotVersion(releaseKey));
        }
    }
}


...


// Checking the version using >= will enable forward compatibility,  
// however you should always compile your code on newer versions of 
// the framework to ensure your app works the same. 
private static string CheckFor45DotVersion(int releaseKey)
{
    if (releaseKey >= 461808) {
        return "4.7.2 or later";
    }
    if (releaseKey >= 461308) {
        return "4.7.1 or later";
    }
    if (releaseKey >= 460798) {
        return "4.7 or later";
    }
    if (releaseKey >= 394802) {
        return "4.6.2 or later";
    }
    if (releaseKey >= 394254) {
        return "4.6.1 or later";
    }
    if (releaseKey >= 393295) {
        return "4.6 or later";
    }
    if (releaseKey >= 393273) {
        return "4.6 RC or later";
    }
    if ((releaseKey >= 379893)) {
        return "4.5.2 or later";
    }
    if ((releaseKey >= 378675)) {
        return "4.5.1 or later";
    }
    if ((releaseKey >= 378389)) {
        return "4.5 or later";
    }
    // This line should never execute. A non-null release key should mean 
    // that 4.5 or later is installed. 
    return "No 4.5 or later version detected";
}
Factor Mystic
sumber
String versi panjang serta subkunci SP untuk setiap PERANGKAT LUNAK \ Microsoft \ NET Framework Setup \ NDP \ <. Versi net> harus menyediakan ini, yang dapat Anda ambil dengan metode GetValue.
Factor Mystic
21
Bukankah ini hanya akan mendapatkan versi .NET TERPASANG tertinggi di mesin - bukan versi yang dijalankan aplikasi?
Grinn
2
@FactorMystic: Hai, Saya telah menggunakan kode Anda untuk memeriksa versi terbaru, tetapi tidak mencantumkan versi 4.5 meskipun telah diinstal di PC saya. Tolong bimbing saya
Upendra Chaudhari
1
@FactorMystic dapatkah ini dilakukan pada akun PC non-admin? akankah saya memiliki akses ke nilai registri?
Jay Nirgudkar
2
@FactorMic apa gunanya if (true)cek? Mengapa tidak hanya panggilan ke Console.WriteLine()?
Broots Waymb
22

Tidak yakin mengapa tidak ada yang menyarankan mengikuti saran resmi dari Microsoft di sini .

Ini adalah kode yang mereka rekomendasikan. Tentu itu jelek, tapi berhasil.

Untuk .NET 1-4

private static void GetVersionFromRegistry()
{
     // Opens the registry key for the .NET Framework entry. 
        using (RegistryKey ndpKey = 
            RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "").
            OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
        {
            // As an alternative, if you know the computers you will query are running .NET Framework 4.5  
            // or later, you can use: 
            // using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,  
            // RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
        foreach (string versionKeyName in ndpKey.GetSubKeyNames())
        {
            if (versionKeyName.StartsWith("v"))
            {

                RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
                string name = (string)versionKey.GetValue("Version", "");
                string sp = versionKey.GetValue("SP", "").ToString();
                string install = versionKey.GetValue("Install", "").ToString();
                if (install == "") //no install info, must be later.
                    Console.WriteLine(versionKeyName + "  " + name);
                else
                {
                    if (sp != "" && install == "1")
                    {
                        Console.WriteLine(versionKeyName + "  " + name + "  SP" + sp);
                    }

                }
                if (name != "")
                {
                    continue;
                }
                foreach (string subKeyName in versionKey.GetSubKeyNames())
                {
                    RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
                    name = (string)subKey.GetValue("Version", "");
                    if (name != "")
                        sp = subKey.GetValue("SP", "").ToString();
                    install = subKey.GetValue("Install", "").ToString();
                    if (install == "") //no install info, must be later.
                        Console.WriteLine(versionKeyName + "  " + name);
                    else
                    {
                        if (sp != "" && install == "1")
                        {
                            Console.WriteLine("  " + subKeyName + "  " + name + "  SP" + sp);
                        }
                        else if (install == "1")
                        {
                            Console.WriteLine("  " + subKeyName + "  " + name);
                        }

                    }

                }

            }
        }
    }

}

Untuk .NET 4.5 dan yang lebih baru

// Checking the version using >= will enable forward compatibility, 
// however you should always compile your code on newer versions of
// the framework to ensure your app works the same.
private static string CheckFor45DotVersion(int releaseKey)
{
   if (releaseKey >= 393295) {
      return "4.6 or later";
   }
   if ((releaseKey >= 379893)) {
        return "4.5.2 or later";
    }
    if ((releaseKey >= 378675)) {
        return "4.5.1 or later";
    }
    if ((releaseKey >= 378389)) {
        return "4.5 or later";
    }
    // This line should never execute. A non-null release key should mean
    // that 4.5 or later is installed.
    return "No 4.5 or later version detected";
}

private static void Get45or451FromRegistry()
{
    using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\")) {
        if (ndpKey != null && ndpKey.GetValue("Release") != null) {
            Console.WriteLine("Version: " + CheckFor45DotVersion((int) ndpKey.GetValue("Release")));
        }
      else {
         Console.WriteLine("Version 4.5 or later is not detected.");
      } 
    }
}
Mathias Lykkegaard Lorenzen
sumber
Metode untuk .NET 1-4 memiliki bug: tidak akan mencetak rilis asli (tanpa SP) hingga 3.0
Lu55
1
Dan updatesuntuk NET Framework 4.7 dan NET Framework 4.7.1 ? Bukan 4.7 atau lebih baru. Ya 4.7 atau 4.7.1.
Kiquenet
@Kiquenet apakah mereka tidak bekerja untuk kode yang disediakan?
Mathias Lykkegaard Lorenzen
15

Metode alternatif di mana tidak ada hak untuk mengakses registri diperlukan, adalah dengan memeriksa keberadaan kelas yang diperkenalkan dalam pembaruan kerangka kerja tertentu.

private static bool Is46Installed()
{
    // API changes in 4.6: https://github.com/Microsoft/dotnet/blob/master/releases/net46/dotnet46-api-changes.md
    return Type.GetType("System.AppContext, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", false) != null;
}

private static bool Is461Installed()
{
    // API changes in 4.6.1: https://github.com/Microsoft/dotnet/blob/master/releases/net461/dotnet461-api-changes.md
    return Type.GetType("System.Data.SqlClient.SqlColumnEncryptionCngProvider, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", false) != null;
}

private static bool Is462Installed()
{
    // API changes in 4.6.2: https://github.com/Microsoft/dotnet/blob/master/releases/net462/dotnet462-api-changes.md
    return Type.GetType("System.Security.Cryptography.AesCng, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", false) != null;
}

private static bool Is47Installed()
{
    // API changes in 4.7: https://github.com/Microsoft/dotnet/blob/master/releases/net47/dotnet47-api-changes.md
    return Type.GetType("System.Web.Caching.CacheInsertOptions, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", false) != null;
}
mwijnands
sumber
Ada referensi untuk 4.5?
Vinicius Gonçalves
Saya suka ide ini
wexman
1
dan 4.7.1, 4.7.2 dan 4.8?
Kiquenet
Sangat berguna bagi kita yang memiliki masalah izin
FBryant87
10

Environment.Version()adalah memberikan jawaban yang benar untuk pertanyaan lain. Versi CLR yang sama digunakan di .NET 2.0, 3, dan 3.5. Saya kira Anda dapat memeriksa GAC untuk perpustakaan yang ditambahkan di setiap rilis berikutnya.

quillbreaker
sumber
2
Environment.Version () memberikan 2.0meskipun berjalan dengan 3.5
LCJ
5
Environment.Version () memberi Anda nama CLR. 3.5 bukanlah versi baru CLR. Ini 2.0 ditambah beberapa perpustakaan tambahan dengan kenaikan 1,5 yang didorong pemasaran.
quillbreaker
7
Jawabannya menyesatkan. CLR 2.0 digunakan untuk .NET 2.0, 3.0 dan 3.5. CLR telah berubah dengan versi 4. CLR 4.0 digunakan untuk .NET 4.0.
bouvierr
9

Dulu mudah, tetapi Microsoft memutuskan untuk membuat perubahan besar: Sebelum versi 4.5, setiap versi .NET berada di direktori sendiri di bawah ini C:\Windows\Microsoft.NET\Framework(subdirektori v1.0.3705, v1.1.4322, v2.0.50727, v3.0, v3.5dan v4.0.30319).

Sejak versi 4.5 ini telah diubah: Setiap versi .NET (yaitu 4.5.x, 4.6.x, 4.7.x, 4.8.x, ...) sedang diinstal di subdirektori yang sama v4.0.30319- jadi Anda tidak dapat lagi periksa versi .NET yang diinstal dengan melihat ke Microsoft.NET\Framework.

Untuk memeriksa versi .NET, Microsoft telah menyediakan dua contoh skrip yang berbeda tergantung pada versi .NET yang sedang diperiksa, tetapi saya tidak suka memiliki dua skrip C # yang berbeda untuk ini. Jadi saya mencoba menggabungkannya menjadi satu, berikut skrip yang GetDotNetVersion.cssaya buat (dan memperbaruinya untuk framework 4.7.1):

using System;
using Microsoft.Win32;
public class GetDotNetVersion
{
    public static void Main(string[] args)
    {
        Console.WriteLine((args != null && args.Length > 0) 
                          ? "Command line arguments: " + string.Join(",", args) 
                          : "");

        string maxDotNetVersion = GetVersionFromRegistry();
        if (String.Compare(maxDotNetVersion, "4.5") >= 0)
        {
            string v45Plus = GetDotNetVersion.Get45PlusFromRegistry();
            if (v45Plus != "") maxDotNetVersion = v45Plus;
        }
        Console.WriteLine("*** Maximum .NET version number found is: " + maxDotNetVersion + "***");
    }

    private static string Get45PlusFromRegistry()
    {
        String dotNetVersion = "";
        const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
        using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
        {
            if (ndpKey != null && ndpKey.GetValue("Release") != null)
            {
                dotNetVersion = CheckFor45PlusVersion((int)ndpKey.GetValue("Release"));
                Console.WriteLine(".NET Framework Version: " + dotNetVersion);
            }
            else
            {
                Console.WriteLine(".NET Framework Version 4.5 or later is not detected.");
            }
        }
        return dotNetVersion;
    }

    // Checking the version using >= will enable forward compatibility.
    private static string CheckFor45PlusVersion(int releaseKey)
    {
        if (releaseKey >= 528040) return "4.8 or later";
        if (releaseKey >= 461808) return "4.7.2";
        if (releaseKey >= 461308) return "4.7.1";
        if (releaseKey >= 460798) return "4.7";
        if (releaseKey >= 394802) return "4.6.2";
        if (releaseKey >= 394254) return "4.6.1";
        if (releaseKey >= 393295) return "4.6";
        if ((releaseKey >= 379893)) return "4.5.2";
        if ((releaseKey >= 378675)) return "4.5.1";
        if ((releaseKey >= 378389)) return "4.5";

        // This code should never execute. A non-null release key should mean
        // that 4.5 or later is installed.
        return "No 4.5 or later version detected";
    }

    private static string GetVersionFromRegistry()
    {
        String maxDotNetVersion = "";
        // Opens the registry key for the .NET Framework entry.
        using (RegistryKey ndpKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "")
                                        .OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
        {
            // As an alternative, if you know the computers you will query are running .NET Framework 4.5 
            // or later, you can use:
            // using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, 
            // RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
            foreach (string versionKeyName in ndpKey.GetSubKeyNames())
            {
                if (versionKeyName.StartsWith("v"))
                {
                    RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
                    string name = (string)versionKey.GetValue("Version", "");
                    string sp = versionKey.GetValue("SP", "").ToString();
                    string install = versionKey.GetValue("Install", "").ToString();
                    if (install == "") //no install info, must be later.
                    {
                        Console.WriteLine(versionKeyName + "  " + name);
                        if (String.Compare(maxDotNetVersion, name) < 0) maxDotNetVersion = name;
                    }
                    else
                    {
                        if (sp != "" && install == "1")
                        {
                            Console.WriteLine(versionKeyName + "  " + name + "  SP" + sp);
                            if (String.Compare(maxDotNetVersion, name) < 0) maxDotNetVersion = name;
                        }

                    }
                    if (name != "")
                    {
                        continue;
                    }
                    foreach (string subKeyName in versionKey.GetSubKeyNames())
                    {
                        RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
                        name = (string)subKey.GetValue("Version", "");
                        if (name != "")
                        {
                            sp = subKey.GetValue("SP", "").ToString();
                        }
                        install = subKey.GetValue("Install", "").ToString();
                        if (install == "")
                        {
                            //no install info, must be later.
                            Console.WriteLine(versionKeyName + "  " + name);
                            if (String.Compare(maxDotNetVersion, name) < 0) maxDotNetVersion = name;
                        }
                        else
                        {
                            if (sp != "" && install == "1")
                            {
                                Console.WriteLine("  " + subKeyName + "  " + name + "  SP" + sp);
                                if (String.Compare(maxDotNetVersion, name) < 0) maxDotNetVersion = name;
                            }
                            else if (install == "1")
                            {
                                Console.WriteLine("  " + subKeyName + "  " + name);
                                if (String.Compare(maxDotNetVersion, name) < 0) maxDotNetVersion = name;
                            } // if
                        } // if
                    } // for
                } // if
            } // foreach
        } // using
        return maxDotNetVersion;
    }
    
} // class

Di mesin saya itu menghasilkan:

v2.0.50727 2.0.50727.4927 SP2
v3.0 3.0.30729.4926 SP2
v3.5 3.5.30729.4926 SP1
v4
Klien 4.7.03056
Penuh 4.7.03056
v4.0
Klien 4.0.0.0
.NET Framework Versi: 4.7.2 atau yang lebih baru
*** * Nomor versi .NET maksimum yang ditemukan adalah: 4.7.2 atau yang lebih baru ****

Satu-satunya hal yang perlu dipertahankan dari waktu ke waktu adalah nomor build setelah versi .NET lebih besar dari 4.7.1 keluar - itu dapat dilakukan dengan mudah dengan memodifikasi fungsi CheckFor45PlusVersion, Anda perlu mengetahui kunci rilis untuk versi baru, lalu Anda bisa menambahkannya. Sebagai contoh:

if (releaseKey >= 461308) return "4.7.1 or later";

Kunci rilis ini masih yang terbaru dan berlaku untuk update Kejatuhan Kreator Windows 10. Jika Anda masih menjalankan versi lainnya (yang lebih tua) Windows, ada satu lagi sebagai per ini dokumentasi dari Microsoft:

.NET Framework 4.7.1 diinstal pada semua versi OS Windows 461310

Jadi, jika Anda membutuhkannya juga, Anda harus menambahkan

if (releaseKey >= 461310) return "4.7.1 or later";

ke atas fungsi CheckFor45PlusVersion. Juga berfungsi untuk versi yang lebih baru. Misalnya, saya telah menambahkan centang untuk 4.8 baru-baru ini. Anda biasanya dapat menemukan nomor build tersebut di Microsoft.


Catatan: Anda tidak perlu menginstal Visual Studio, bahkan PowerShell - Anda dapat menggunakan csc.exeuntuk mengkompilasi dan menjalankan skrip di atas, yang telah saya jelaskan di sini.


Pembaruan: Pertanyaannya adalah tentang .NET Framework, demi kelengkapan, saya ingin menyebutkan cara menanyakan versi .NET Core juga - dibandingkan dengan yang di atas, itu mudah: Buka shell perintah dan ketik:

dotnet --info Enter

dan itu akan mencantumkan nomor versi .NET Core, versi Windows dan versi dari setiap DLL runtime terkait juga. Output sampel:

.NET Core SDK (mencerminkan global.json apa pun):
  Versi: 2.1.300
  Komit: adab45bf0c

Lingkungan Waktu Proses :
  Nama
  OS: Versi OS Windows : 10.0.15063
  Platform OS: Windows
  RID: win10-x64
  Jalur Dasar: C: \ Program Files \ dotnet \ sdk \ 2.1.300


Host (berguna untuk dukungan):
  Versi: 2.1.0
  Commit: caa7b7e2ba

.NET Core SDKs diinstal:
  1.1.9 [C: \ Program Files \ dotnet \ sdk]
  2.1.102 [C: \ Program Files \ dotnet \ sdk]
  ...
  2.1.300 [C: \ Program Files \ dotnet \ sdk]   Microsoft.NETCore.App 2.1.0 [C: \ Program Files \ dotnet \ shared \ Microsoft.NETCore.App] Ke instal runtime atau SDK .NET Core tambahan:

.NET Core runtimes diinstal:
  Microsoft.AspNetCore.All 2.1.0 [C: \ Program
  Files \ dotnet \ shared \ Microsoft .AspNetCore.All]
  ...



  https://aka.ms/dotnet-download

Catat itu

  • jika Anda hanya membutuhkan nomor versi tanpa semua informasi tambahan, Anda dapat menggunakan
    dotnet --version.

  • pada PC Windows 64 bit , dimungkinkan untuk menginstal versi x86 berdampingan dengan versi x64 dari .NET Core. Jika demikian, Anda hanya akan mendapatkan versi yang lebih dulu dalam variabel lingkungan PATH. Itu sangat penting jika Anda perlu terus memperbaruinya dan ingin mengetahui setiap versi. Untuk menanyakan kedua versi, gunakan:

C: \ Program Files \ dotnet \ dotnet.exe --version
3.0.100-preview6-012264
C: \ Program Files (x86) \ dotnet \ dotnet.exe --version
3.0.100-preview6-012264

Pada contoh di atas, keduanya sama, tetapi jika Anda lupa memperbarui kedua contoh, Anda mungkin mendapatkan hasil yang berbeda! Perhatikan itu Program Filesuntuk versi 64bit , dan Program Files (x86)merupakan versi 32bit .


Pembaruan: Jika Anda lebih suka menyimpan nomor build dalam daftar daripada dalam rangkaian pernyataan if (seperti yang disarankan Microsoft), Anda dapat menggunakan kode ini untuk CheckFor45PlusVersion:

private static string CheckFor45PlusVersion(int releaseKey)
{
    var release = new Dictionary<int, string>()
    {
            { 378389, "4.5" },
            { 378675, "4.5.1" }, { 379893, "4.5.2" },
            { 393295, "4.6" },
            { 394254, "4.6.1" }, { 394802, "4.6.2" },
            { 460798, "4.7" },
            { 461308, "4.7.1" }, { 461808, "4.7.2" },
            { 528040, "4.8 or later" }
    };
    int result = -1;
    foreach(var k in release.OrderBy(k=>k.Key))
    {
        if (k.Key <= releaseKey) result = k.Key; else break;
    };
    return (result > 0) ? release[result] : "No 4.5 or later version detected";
}
Matt
sumber
6

AFAIK tidak ada metode bawaan dalam kerangka kerja yang memungkinkan Anda melakukan ini. Anda dapat memeriksa posting ini untuk mendapatkan saran tentang menentukan versi kerangka kerja dengan membaca nilai registri windows.

Darin Dimitrov
sumber
5

Kelas ini memungkinkan aplikasi Anda untuk mengeluarkan pesan notifikasi yang anggun daripada crash and burn jika tidak dapat menemukan versi .NET yang tepat. Yang perlu Anda lakukan adalah ini di kode utama Anda:

[STAThread]
static void Main(string[] args)
{
    if (!DotNetUtils.IsCompatible())
        return;
   . . .
}

Secara default dibutuhkan 4.5.2, tetapi Anda dapat menyesuaikannya sesuai keinginan Anda, kelas (jangan ragu untuk mengganti MessageBox dengan Konsol):

Diperbarui untuk 4.8:

public class DotNetUtils
{
    public enum DotNetRelease
    {
        NOTFOUND,
        NET45,
        NET451,
        NET452,
        NET46,
        NET461,
        NET462,
        NET47,
        NET471,
        NET472,
        NET48,
    }

    public static bool IsCompatible(DotNetRelease req = DotNetRelease.NET452)
    {
        DotNetRelease r = GetRelease();
        if (r < req)
        {
            MessageBox.Show(String.Format("This this application requires {0} or greater.", req.ToString()));
            return false;
        }
        return true;
    }

    public static DotNetRelease GetRelease(int release = default(int))
    {
        int r = release != default(int) ? release : GetVersion();
        if (r >= 528040) return DotNetRelease.NET48;
        if (r >= 461808) return DotNetRelease.NET472;
        if (r >= 461308) return DotNetRelease.NET471;
        if (r >= 460798) return DotNetRelease.NET47;
        if (r >= 394802) return DotNetRelease.NET462;
        if (r >= 394254) return DotNetRelease.NET461;
        if (r >= 393295) return DotNetRelease.NET46;
        if (r >= 379893) return DotNetRelease.NET452;
        if (r >= 378675) return DotNetRelease.NET451;
        if (r >= 378389) return DotNetRelease.NET45;
        return DotNetRelease.NOTFOUND;
    }

    public static int GetVersion()
    {
        int release = 0;
        using (RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)
                                            .OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\"))
        {
            release = Convert.ToInt32(key.GetValue("Release"));
        }
        return release;
    }
}

Dapat diperpanjang dengan mudah saat mereka menambahkan versi baru nanti. Saya tidak peduli dengan apa pun sebelum 4,5 tetapi Anda mengerti.

codenamezero
sumber
1
Diperbarui untuk 4.7.1, nomor revisi diambil dari docs.microsoft.com/en-us/dotnet/framework/migration-guide/…
codenamezero
Ide bagus IsCompatible()- kompatibilitas kemungkinan merupakan alasan paling umum untuk menanyakan versi .Net dari semua.
spacer
3
public class DA
{
    public static class VersionNetFramework
    {
        public static string GetVersion()
        {
            return Environment.Version.ToString();
        }
        public static string GetVersionDicription()
        {
            int Major = Environment.Version.Major;
            int Minor = Environment.Version.Minor;
            int Build = Environment.Version.Build;
            int Revision = Environment.Version.Revision;

            //http://dzaebel.net/NetVersionen.htm
            //http://stackoverflow.com/questions/12971881/how-to-reliably-detect-the-actual-net-4-5-version-installed

            //4.0.30319.42000 = .NET 4.6 on Windows 8.1 64 - bit
            if ((Major >=4) && (Minor >=0) && (Build >= 30319) && (Revision >= 42000))
                return @".NET 4.6 on Windows 8.1 64 - bit or later";
            //4.0.30319.34209 = .NET 4.5.2 on Windows 8.1 64 - bit
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 34209))
                return @".NET 4.5.2 on Windows 8.1 64 - bit or later";
            //4.0.30319.34209 = .NET 4.5.2 on Windows 7 SP1 64 - bit
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 34209))
                return @".NET 4.5.2 on Windows 7 SP1 64 - bit or later";
            //4.0.30319.34014 = .NET 4.5.1 on Windows 8.1 64 - bit
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 34014))
                return @".NET 4.5.1 on Windows 8.1 64 - bit or later";
            //4.0.30319.18444 = .NET 4.5.1 on Windows 7 SP1 64 - bit(with MS14 - 009 security update)
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 18444))
                return @".NET 4.5.1 on Windows 7 SP1 64 - bit(with MS14 - 009 security update) or later";
            //4.0.30319.18408 = .NET 4.5.1 on Windows 7 SP1 64 - bit
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 18408))
                return @".NET 4.5.1 on Windows 7 SP1 64 - bit or later";
            //4.0.30319.18063 = .NET 4.5 on Windows 7 SP1 64 - bit(with MS14 - 009 security update)
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 18063))
                return @".NET 4.5 on Windows 7 SP1 64 - bit(with MS14 - 009 security update) or later";
            //4.0.30319.18052 = .NET 4.5 on Windows 7 SP1 64 - bit
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 18052))
                return @".NET 4.5 on Windows 7 SP1 64 - bit or later";
            //4.0.30319.18010 = .NET 4.5 on Windows 8
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 18010))
                return @".NET 4.5 on Windows 8 or later";
            //4.0.30319.17929 = .NET 4.5 RTM
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 17929))
                return @".NET 4.5 RTM or later";
            //4.0.30319.17626 = .NET 4.5 RC
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 17626))
                return @".NET 4.5 RC or later";
            //4.0.30319.17020.NET 4.5 Preview, September 2011
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 17020))
                return @".NET 4.5 Preview, September 2011 or later";
            //4.0.30319.2034 = .NET 4.0 on Windows XP SP3, 7, 7 SP1(with MS14 - 009 LDR security update)
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 2034))
                return @".NET 4.0 on Windows XP SP3, 7, 7 SP1(with MS14 - 009 LDR security update) or later";
            //4.0.30319.1026 = .NET 4.0 on Windows XP SP3, 7, 7 SP1(with MS14 - 057 GDR security update)
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 1026))
                return @".NET 4.0 on Windows XP SP3, 7, 7 SP1(with MS14 - 057 GDR security update) or later";
            //4.0.30319.1022 = .NET 4.0 on Windows XP SP3, 7, 7 SP1(with MS14 - 009 GDR security update)
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 1022))
                return @".NET 4.0 on Windows XP SP3, 7, 7 SP1(with MS14 - 009 GDR security update) or later";
            //4.0.30319.1008 = .NET 4.0 on Windows XP SP3, 7, 7 SP1(with MS13 - 052 GDR security update)
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 1008))
                return @".NET 4.0 on Windows XP SP3, 7, 7 SP1(with MS13 - 052 GDR security update) or later";
            //4.0.30319.544 = .NET 4.0 on Windows XP SP3, 7, 7 SP1(with MS12 - 035 LDR security update)
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 544))
                return @".NET 4.0 on Windows XP SP3, 7, 7 SP1(with MS12 - 035 LDR security update) or later";
            //4.0.30319.447   yes built by: RTMLDR, .NET 4.0 Platform Update 1, April 2011
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 447))
                return @"built by: RTMLDR, .NET 4.0 Platform Update 1, April 2011 or later";
            //4.0.30319.431   yes built by: RTMLDR, .NET 4.0 GDR Update, March 2011 / with VS 2010 SP1 / or.NET 4.0 Update
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 431))
                return @"built by: RTMLDR, .NET 4.0 GDR Update, March 2011 / with VS 2010 SP1 / or.NET 4.0 Update or later";
            //4.0.30319.296 = .NET 4.0 on Windows XP SP3, 7(with MS12 - 074 GDR security update)
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 296))
                return @".NET 4.0 on Windows XP SP3, 7(with MS12 - 074 GDR security update) or later";
            //4.0.30319.276 = .NET 4.0 on Windows XP SP3 (4.0.3 Runtime update)
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 276))
                return @".NET 4.0 on Windows XP SP3 (4.0.3 Runtime update) or later";
            //4.0.30319.269 = .NET 4.0 on Windows XP SP3, 7, 7 SP1(with MS12 - 035 GDR security update)
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 269))
                return @".NET 4.0 on Windows XP SP3, 7, 7 SP1(with MS12 - 035 GDR security update) or later";
            //4.0.30319.1 yes built by: RTMRel, .NET 4.0 RTM Release, April 2010
            if ((Major >= 4) && (Minor >= 0) && (Build >= 30319) && (Revision >= 1))
                return @"built by: RTMRel, .NET 4.0 RTM Release, April 2010 or later";

            //4.0.30128.1     built by: RC1Rel, .NET 4.0 Release Candidate, Feb 2010
            if ((Major >=4) && (Minor >=0) && (Build >= 30128) && (Revision >= 1))
                return @"built by: RC1Rel, .NET 4.0 Release Candidate, Feb 2010 or later";
            //4.0.21006.1     built by: B2Rel, .NET 4.0 Beta2, Oct 2009
            if ((Major >=4) && (Minor >=0) && (Build >= 21006) && (Revision >=1))
                return @"built by: B2Rel, .NET 4.0 Beta2, Oct 2009 or later";
            //4.0.20506.1     built by: Beta1, .NET 4.0 Beta1, May 2009
            if ((Major >=4) && (Minor >=0) && (Build >= 20506) && (Revision >=1))
                return @"built by: Beta1, .NET 4.0 Beta1, May 2009 or later";
            //4.0.11001.1     built by: CTP2 VPC, .NET 4.0 CTP, October 2008
            if ((Major >=4) && (Minor >=0) && (Build >= 11001) && (Revision >=1))
                return @"built by: CTP2 VPC, .NET 4.0 CTP, October 2008 or later";

            //3.5.30729.5420  yes built by: Win7SP1, .NET 3.5.1 Sicherheits - Update, 12 April 2011
            if ((Major >=3) && (Minor >=5) && (Build >= 30729) && (Revision >= 5420))
                return @"built by: Win7SP1, .NET 3.5.1 Sicherheits - Update, 12 April 2011 or later";
            //3.5.30729.5004  yes built by: NetFXw7 / Windows 7..Rel., Jan 2010 / +Data functions KB976127 .NET 3.5 SP1
            if ((Major >=3) && (Minor >=5) && (Build >= 30729) && (Revision >= 5004))
                return @"built by: NetFXw7 / Windows 7..Rel., Jan 2010 / +Data functions KB976127 .NET 3.5 SP1 or later";
            //3.5.30729.4466  yes built by: NetFXw7 / Windows XP..Rel. , Jan 2010 / +Data functions KB976127 .NET 3.5 SP1
            if ((Major >=3) && (Minor >=5) && (Build >= 30729) && (Revision >= 4466))
                return @"built by: NetFXw7 / Windows XP..Rel. , Jan 2010 / +Data functions KB976127 .NET 3.5 SP1 or later";
            //3.5.30729.4926  yes built by: NetFXw7 / Windows 7 Release, Oct 2009 / .NET 3.5 SP1 + Hotfixes
            if ((Major >=3) && (Minor >=5) && (Build >= 30729) && (Revision >= 4926))
                return @"built by: NetFXw7 / Windows 7 Release, Oct 2009 / .NET 3.5 SP1 + Hotfixes or later";
            //3.5.30729.4918      built by: NetFXw7 / Windows 7 Release Candidate, June 2009
            if ((Major >=3) && (Minor >=5) && (Build >= 30729) && (Revision >= 4918))
                return @"built by: NetFXw7 / Windows 7 Release Candidate, June 2009 or later";
            //3.5.30729.196   yes built by: QFE, .NET 3.5 Family Update Vista / W2008, Dec 2008
            if ((Major >= 3) && (Minor >= 5) && (Build >= 30729) && (Revision >=196))
                return @"built by: QFE, .NET 3.5 Family Update Vista / W2008, Dec 2008 or later";
            //3.5.30729.1 yes built by: SP, .NET 3.5 SP1, Aug 2008
            if ((Major >= 3) && (Minor >= 5) && (Build >= 30729) && (Revision >=1))
                return @"built by: SP, .NET 3.5 SP1, Aug 2008 or later";

            //3.5.30428.1         built by: SP1Beta1, .NET 3.5 SP1 BETA1, May 2008
            if ((Major >=3) && (Minor >=5) && (Build >= 30428) && (Revision >=1))
                return @"built by: SP1Beta1, .NET 3.5 SP1 BETA1, May 2008 or later";
            //3.5.21022.8 yes built by: RTM, Jan 2008
            if ((Major >=3) && (Minor >=5) && (Build >= 21022) && (Revision >= 8))
                return @"built by: RTM, Jan 2008 or later";
            //3.5.20706.1     built by: Beta2, Orcas Beta2, Oct 2007
            if ((Major >=3) && (Minor >=5) && (Build >= 20706) && (Revision >= 1))
                return @"built by: Beta2, Orcas Beta2, Oct 2007 or later";
            //3.5.20526.0     built by: MCritCTP, Orcas Beta1, Mar 2007
            if ((Major >=3) && (Minor >=5) && (Build >= 20526) && (Revision >=0))
                return @"built by: MCritCTP, Orcas Beta1, Mar 2007 or later";

            //3.0.6920.1500   yes built by: QFE, Family Update Vista / W2008, Dez 2008, KB958483
            if ((Major >=3) && (Minor >=0) && (Build >= 6920) && (Revision >= 1500))
                return @"built by: QFE, Family Update Vista / W2008, Dez 2008, KB958483 or later";
            //3.0.4506.4926   yes(NetFXw7.030729 - 4900) / Windows 7 Release, Oct 2009
            if ((Major >=3) && (Minor >=0) && (Build >= 4506) && (Revision >= 4926))
                return @"(NetFXw7.030729 - 4900) / Windows 7 Release, Oct 2009 or later";
            //3.0.4506.4918(NetFXw7.030729 - 4900) / Windows 7 Release Candidate, June 2009
            if ((Major >=3) && (Minor >=5) && (Build >= 4506) && (Revision >= 4918))
                return @"(NetFXw7.030729 - 4900) / Windows 7 Release Candidate, June 2009 or later";
            //3.0.4506.2152       3.0.4506.2152(SP.030729 - 0100) / .NET 4.0 Beta1 / May 2009
            if ((Major >= 3) && (Minor >= 5) && (Build >= 4506) && (Revision >= 2152))
                return @"3.0.4506.2152(SP.030729 - 0100) / .NET 4.0 Beta1 / May 2009 or later";
            //3.0.4506.2123   yes(NetFX.030618 - 0000).NET 3.0 SP2, Aug 2008
            if ((Major >= 3) && (Minor >= 5) && (Build >= 4506) && (Revision >= 2123))
                return @"s(NetFX.030618 - 0000).NET 3.0 SP2, Aug 2008 or later";
            //3.0.4506.2062(SP1Beta1.030428 - 0100), .NET 3.0 SP1 BETA1, May 2008
            if ((Major >= 3) && (Minor >= 5) && (Build >= 4506) && (Revision >= 2062))
                return @"(SP1Beta1.030428 - 0100), .NET 3.0 SP1 BETA1, May 2008 or later";
            //3.0.4506.590(winfxredb2.004506 - 0590), Orcas Beta2, Oct 2007
            if ((Major >= 3) && (Minor >= 5) && (Build >= 4506) && (Revision >= 590))
                return @"(winfxredb2.004506 - 0590), Orcas Beta2, Oct 2007 or later";
            //3.0.4506.577(winfxred.004506 - 0577), Orcas Beta1, Mar 2007
            if ((Major >= 3) && (Minor >= 5) && (Build >= 4506) && (Revision >= 577))
                return @"(winfxred.004506 - 0577), Orcas Beta1, Mar 2007 or later";
            //3.0.4506.30 yes Release (.NET Framework 3.0) Nov 2006
            if ((Major >= 3) && (Minor >= 5) && (Build >= 4506) && (Revision >= 30))
                return @"Release (.NET Framework 3.0) Nov 2006 or later";
            //3.0.4506.25 yes(WAPRTM.004506 - 0026) Vista Ultimate, Jan 2007
            if ((Major >= 3) && (Minor >= 5) && (Build >= 4506) && (Revision >= 25))
                return @"(WAPRTM.004506 - 0026) Vista Ultimate, Jan 2007 or later";

            //2.0.50727.4927  yes(NetFXspW7.050727 - 4900) / Windows 7 Release, Oct 2009
            if ((Major >=2) && (Minor >=0) && (Build >= 50727) && (Revision >= 4927))
                return @"(NetFXspW7.050727 - 4900) / Windows 7 Release, Oct 2009 or later";
            //2.0.50727.4918(NetFXspW7.050727 - 4900) / Windows 7 Release Candidate, June 2009
            if ((Major >= 2) && (Minor >= 0) && (Build >= 50727) && (Revision >= 4918))
                return @"(NetFXspW7.050727 - 4900) / Windows 7 Release Candidate, June 2009 or later";
            //2.0.50727.4200  yes(NetFxQFE.050727 - 4200).NET 2.0 SP2, KB974470, Securityupdate, Oct 2009
            if ((Major >= 2) && (Minor >= 0) && (Build >= 50727) && (Revision >= 4200))
                return @"(NetFxQFE.050727 - 4200).NET 2.0 SP2, KB974470, Securityupdate, Oct 2009 or later";
            //2.0.50727.3603(GDR.050727 - 3600).NET 4.0 Beta 2, Oct 2009
            if ((Major >= 2) && (Minor >= 0) && (Build >= 50727) && (Revision >= 3603))
                return @"(GDR.050727 - 3600).NET 4.0 Beta 2, Oct 2009 or later";
            //2.0.50727.3082  yes(QFE.050727 - 3000), .NET 3.5 Family Update XP / W2003, Dez 2008, KB958481
            if ((Major >= 2) && (Minor >= 0) && (Build >= 50727) && (Revision >= 3082))
                return @"(QFE.050727 - 3000), .NET 3.5 Family Update XP / W2003, Dez 2008, KB958481 or later";
            //2.0.50727.3074  yes(QFE.050727 - 3000), .NET 3.5 Family Update Vista / W2008, Dez 2008, KB958481
            if ((Major >= 2) && (Minor >= 0) && (Build >= 50727) && (Revision >= 3074))
                return @"(QFE.050727 - 3000), .NET 3.5 Family Update Vista / W2008, Dez 2008, KB958481 or later";
            //2.0.50727.3053  yes(netfxsp.050727 - 3000), .NET 2.0 SP2, Aug 2008
            if ((Major >= 2) && (Minor >= 0) && (Build >= 50727) && (Revision >= 3053))
                return @"yes(netfxsp.050727 - 3000), .NET 2.0 SP2, Aug 2008 or later";
            //2.0.50727.3031(netfxsp.050727 - 3000), .NET 2.0 SP2 Beta 1, May 2008
            if ((Major >= 2) && (Minor >= 0) && (Build >= 50727) && (Revision >= 3031))
                return @"(netfxsp.050727 - 3000), .NET 2.0 SP2 Beta 1, May 2008 or later";
            //2.0.50727.1434  yes(REDBITS.050727 - 1400), Windows Server 2008 and Windows Vista SP1, Dez 2007
            if ((Major >= 2) && (Minor >= 0) && (Build >= 50727) && (Revision >= 1434))
                return @"(REDBITS.050727 - 1400), Windows Server 2008 and Windows Vista SP1, Dez 2007 or later";
            //2.0.50727.1433  yes(REDBITS.050727 - 1400), .NET 2.0 SP1 Release, Nov 2007, http://www.microsoft.com/downloads/details.aspx?FamilyID=79bc3b77-e02c-4ad3-aacf-a7633f706ba5
            if ((Major >= 2) && (Minor >= 0) && (Build >= 50727) && (Revision >= 1433))
                return @"(REDBITS.050727 - 1400), .NET 2.0 SP1 Release, Nov 2007 or later";
            //2.0.50727.1378(REDBITSB2.050727 - 1300), Orcas Beta2, Oct 2007
            if ((Major >= 2) && (Minor >= 0) && (Build >= 50727) && (Revision >= 1378))
                return @"(REDBITSB2.050727 - 1300), Orcas Beta2, Oct 2007 or later";
            //2.0.50727.1366(REDBITS.050727 - 1300), Orcas Beta1, Mar 2007
            if ((Major >= 2) && (Minor >= 0) && (Build >= 50727) && (Revision >= 1366))
                return @"(REDBITS.050727 - 1300), Orcas Beta1, Mar 2007 or later";
            //2.0.50727.867   yes(VS Express Edition 2005 SP1), Apr 2007, http://www.microsoft.com/downloads/details.aspx?FamilyId=7B0B0339-613A-46E6-AB4D-080D4D4A8C4E
            if ((Major >= 2) && (Minor >= 0) && (Build >= 50727) && (Revision >= 867))
                return @"(VS Express Edition 2005 SP1), Apr 2007 or later";
            //2.0.50727.832(Fix x86 VC++2005), Apr 2007, http://support.microsoft.com/kb/934586/en-us
            if ((Major >= 2) && (Minor >= 0) && (Build >= 50727) && (Revision >= 832))
                return @"(Fix x86 VC++2005), Apr 2007 or later";
            //2.0.50727.762   yes(VS TeamSuite SP1), http://www.microsoft.com/downloads/details.aspx?FamilyId=BB4A75AB-E2D4-4C96-B39D-37BAF6B5B1DC
            if ((Major >= 2) && (Minor >= 0) && (Build >= 50727) && (Revision >= 762))
                return @"(VS TeamSuite SP1) or later";
            //2.0.50727.312   yes(rtmLHS.050727 - 3100) Vista Ultimate, Jan 2007
            if ((Major >= 2) && (Minor >= 0) && (Build >= 50727) && (Revision >= 312))
                return @"(rtmLHS.050727 - 3100) Vista Ultimate, Jan 2007 or later";
            //2.0.50727.42    yes Release (.NET Framework 2.0) Oct 2005
            if ((Major >= 2) && (Minor >= 0) && (Build >= 50727) && (Revision >= 42))
                return @"Release (.NET Framework 2.0) Oct 2005 or later";
            //2.0.50727.26        Version 2.0(Visual Studio Team System 2005 Release Candidate) Oct 2005
            if ((Major >= 2) && (Minor >= 0) && (Build >= 50727) && (Revision >= 26))
                return @"Version 2.0(Visual Studio Team System 2005 Release Candidate) Oct 2005 or later";

            //2.0.50712       Version 2.0(Visual Studio Team System 2005(Drop3) CTP) July 2005
            if ((Major >=2) && (Minor >=0) && (Build >= 50712))
                return @"Version 2.0(Visual Studio Team System 2005(Drop3) CTP) July 2005 or later";
            //2.0.50215       Version 2.0(WinFX SDK for Indigo / Avalon 2005 CTP) July 2005
            if ((Major >=2) && (Minor >=0) && (Build >= 50215))
                return @"Version 2.0(WinFX SDK for Indigo / Avalon 2005 CTP) July 2005 or later";
            //2.0.50601.0     Version 2.0(Visual Studio.NET 2005 CTP) June 2005
            if ((Major >=2) && (Minor >=0) && (Build >= 50601) && (Revision >=0))
                return @"Version 2.0(Visual Studio.NET 2005 CTP) June 2005 or later";
            //2.0.50215.44        Version 2.0(Visual Studio.NET 2005 Beta 2, Visual Studio Express Beta 2) Apr 2005
            if ((Major >=2) && (Minor >=0) && (Build >= 50215) && (Revision >= 44))
                return @"Version 2.0(Visual Studio.NET 2005 Beta 2, Visual Studio Express Beta 2) Apr 2005 or later";
            //2.0.50110.28        Version 2.0(Visual Studio.NET 2005 CTP, Professional Edition) Feb 2005
            if ((Major >=2) && (Minor >=0) && (Build >= 50110) && (Revision >=28))
                return @"Version 2.0(Visual Studio.NET 2005 CTP, Professional Edition) Feb 2005 or later";
            //2.0.41115.19        Version 2.0(Visual Studio.NET 2005 Beta 1, Team System Refresh) Dec 2004
            if ((Major >=2 ) && (Minor >=0 ) && (Build >= 41115) && (Revision >= 19))
                return @"Version 2.0(Visual Studio.NET 2005 Beta 1, Team System Refresh) Dec 2004 or later";
            //2.0.40903.0         Version 2.0(Whidbey CTP, Visual Studio Express) Oct 2004
            if ((Major >=2) && (Minor >=0) && (Build >= 40903) && (Revision >=0))
                return @"Version 2.0(Whidbey CTP, Visual Studio Express) Oct 2004 or later";
            //2.0.40607.85        Version 2.0(Visual Studio.NET 2005 Beta 1, Team System Refresh) Aug 2004 *
            if ((Major >=2) && (Minor >=0) && (Build >= 40607) && (Revision >= 85))
                return @"Version 2.0(Visual Studio.NET 2005 Beta 1, Team System Refresh) Aug 2004 * or later";
            //2.0.40607.42        Version 2.0(SQL Server Yukon Beta 2) July 2004
            if ((Major >=2) && (Minor >=0) && (Build >= 40607) && (Revision >= 42))
                return @"Version 2.0(SQL Server Yukon Beta 2) July 2004 or later";
            //2.0.40607.16        Version 2.0(Visual Studio.NET 2005 Beta 1, TechEd Europe 2004) June 2004
            if ((Major >=2) && (Minor >=0) && (Build >= 40607) && (Revision >= 16))
                return @"Version 2.0(Visual Studio.NET 2005 Beta 1, TechEd Europe 2004) June 2004 or later";
            //2.0.40301.9         Version 2.0(Whidbey CTP, WinHEC 2004) March 2004 *
            if ((Major >=0) && (Minor >=0) && (Build >= 40301) && (Revision >=9))
                return @"Version 2.0(Whidbey CTP, WinHEC 2004) March 2004 * or later";

            //1.2.30703.27        Version 1.2(Whidbey Alpha, PDC 2004) Nov 2003 *
            if ((Major >=1) && (Minor >=2) && (Build >= 30703) && (Revision >= 27))
                return @"Version 1.2(Whidbey Alpha, PDC 2004) Nov 2003 * or later";
            //1.2.21213.1     Version 1.2(Whidbey pre - Alpha build) *
            if ((Major >=1) && (Minor >=2) && (Build >= 21213) && (Revision >=1))
                return @"Version 1.2(Whidbey pre - Alpha build) * or later";

            //1.1.4322.2443   yes Version 1.1 Servicepack 1, KB953297, Oct 2009
            if ((Major >=1) && (Minor >=1) && (Build >= 4322) && (Revision >=2443))
                return @"Version 1.1 Servicepack 1, KB953297, Oct 2009 or later";
            //1.1.4322.2407   yes Version 1.1 RTM
            if ((Major >=1) && (Minor >=1) && (Build >= 4322) && (Revision >= 2407))
                return @"Version 1.1 RTM or later";
            //1.1.4322.2407       Version 1.1 Orcas Beta2, Oct 2007
            if ((Major >=1) && (Minor >=1) && (Build >= 4322) && (Revision >= 2407))
                return @"Version 1.1 Orcas Beta2, Oct 2007 or later";
            //1.1.4322.2379       Version 1.1 Orcas Beta1, Mar 2007
            if ((Major >=1) && (Minor >=1) && (Build >= 4322) && (Revision >= 2379))
                return @"Version 1.1 Orcas Beta1, Mar 2007 or later";
            //1.1.4322.2032   yes Version 1.1 SP1 Aug 2004
            if ((Major >=1) && (Minor >=1) && (Build >= 4322) && (Revision >= 2032))
                return @"Version 1.1 SP1 Aug 2004 or later";
            //1.1.4322.573    yes Version 1.1 RTM(Visual Studio.NET 2003 / Windows Server 2003) Feb 2003 *
            if ((Major >=1) && (Minor >=1) && (Build >= 4322) && (Revision >= 573))
                return @"Version 1.1 RTM(Visual Studio.NET 2003 / Windows Server 2003) Feb 2003 * or later";
            //1.1.4322.510        Version 1.1 Final Beta Oct 2002 *
            if ((Major >=1) && (Minor >=1) && (Build >= 4322) && (Revision >= 510))
                return @"Version 1.1 Final Beta Oct 2002 * or later";

            //1.0.3705.6018   yes Version 1.0 SP3 Aug 2004
            if ((Major >=1) && (Minor >=0) && (Build >= 3705) && (Revision >= 6018))
                return @"Version 1.0 SP3 Aug 2004 or later";
            //1.0.3705.288    yes Version 1.0 SP2 Aug 2002 *
            if ((Major >=1) && (Minor >=0) && (Build >= 3705) && (Revision >= 288))
                return @"Version 1.0 SP2 Aug 2002 * or later";
            //1.0.3705.209    yes Version 1.0 SP1 Mar 2002 *
            if ((Major >=1) && (Minor >=0) && (Build >= 3705) && (Revision >=209))
                return @"Version 1.0 SP1 Mar 2002 * or later";
            //1.0.3705.0  yes Version 1.0 RTM(Visual Studio.NET 2002) Feb 2002 *
            if ((Major >=1) && (Minor >=0) && (Build >= 3705) && (Revision >=0))
                return @"Version 1.0 RTM(Visual Studio.NET 2002) Feb 2002 * or later";
            //1.0.3512.0      Version 1.0 Pre - release RC3(Visual Studio.NET 2002 RC3)
            if ((Major >=1) && (Minor >=0) && (Build >= 3512) && (Revision >=0))
                return @"Version 1.0 Pre - release RC3(Visual Studio.NET 2002 RC3) or later";
            //1.0.2914.16     Version 1.0 Public Beta 2 Jun 2001 *
            if ((Major >=1) && (Minor >=0) && (Build >= 2914) && (Revision >= 16))
                return @"Version 1.0 Public Beta 2 Jun 2001 * or later";
            //1.0.2204.21         Version 1.0 Public Beta 1 Nov 2000 *
            if ((Major >=1) && (Minor >=0) && (Build >= 2204) && (Revision >=21))
                return @"Version 1.0 Public Beta 1 Nov 2000 * or later";

            return @"Unknown .NET version";
        }
    }
}
PANAHAN
sumber
batas tubuh 30000 karakter semua kode adalah 34045 karakter bagian kode yang paling berat ini
DARTS
Itu bagus, tetapi bisakah Anda memotongnya menjadi sesuatu yang cocok sebagai satu jawaban?
Flexo
Namun bagian kode ini hanya menyediakan satu metode DA.VersionNetFramework.GetVersionDicription ()
DARTS
FYI, saya membangun ini sebagai aplikasi 2.0, melemparkannya ke konsol dan mengembalikan "2.0.50727.5485 '" pada sistem yang memiliki 4.6 jadi terlihat seperti Environment. Versi mungkin tidak mengembalikan apa yang diinstal sebanyak apa yang digunakan untuk menjalankan aplikasi Sepertinya satu-satunya cara aman untuk melakukannya adalah dengan memeriksa registri.
P. Roe
Dan untuk NET Framework 4.7 dan NET Framework 4.7.1 ?
Kiquenet
3

Saya mengalami situasi di mana perpustakaan kelas .NET 4.0 saya mungkin dipanggil dari rakitan versi .NET 4.0 atau yang lebih tinggi.

Panggilan metode tertentu hanya akan berfungsi jika dijalankan dari perakitan 4.5+.

Untuk memutuskan apakah saya harus memanggil metode atau tidak, saya perlu menentukan versi kerangka kerja saat ini dan ini adalah solusi yang cukup bagus untuk itu

var frameworkName = new System.Runtime.Versioning.FrameworkName(
    AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName
);

if (frameworkName.Version >= new Version(4, 5)) 
{
    // run code
}
Jürgen Steinblock
sumber
Setelah menelusuri semua jawaban yang menggunakan registri ini, saya pikir ini harus ditandai sebagai jawaban yang benar berdasarkan permintaan asli. Dia ingin mengetahui versi CLR dari perakitan yang sedang dijalankan, bukan versi maksimum yang diinstal yang belum tentu sama. Satu-satunya peringatan untuk solusi ini adalah bahwa ia tidak berfungsi di .NET 3.5 atau di bawahnya, tetapi saya senang menggunakan Environment.Version dalam kasus tersebut.
Stuart Welch
2

Terima kasih atas posting ini yang cukup berguna. Saya harus sedikit men-tweaknya untuk memeriksa framework 2.0 karena kunci registri tidak dapat langsung diubah menjadi ganda. Ini kodenya:

string[] version_names = rk.GetSubKeyNames();
//version names start with 'v', eg, 'v3.5'
//we also need to take care of strings like v2.0.50727...
string sCurrent = version_names[version_names.Length - 1].Remove(0, 1);
if (sCurrent.LastIndexOf(".") > 1)
{
    string[] sSplit = sCurrent.Split('.');
    sCurrent = sSplit[0] + "." + sSplit[1] + sSplit[2];
}
double dCurrent = Convert.ToDouble(sCurrent, System.Globalization.CultureInfo.InvariantCulture);
double dExpected = Convert.ToDouble(sExpectedVersion);
if (dCurrent >= dExpected)
pasx
sumber
3
Anda sebaiknya tidak menyimpan nomor versi ganda. Mereka rentan terhadap pembulatan. Gunakan desimal.
Pemburu Instance
2
public class DA {
  public static class VersionNetFramework {
    public static string Get45or451FromRegistry()
    {//https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx
        using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\"))
        {
            int releaseKey = Convert.ToInt32(ndpKey.GetValue("Release"));
            if (true)
            {
                return (@"Version: " + CheckFor45DotVersion(releaseKey));
            }
        }
    }
    // Checking the version using >= will enable forward compatibility, 
    // however you should always compile your code on newer versions of
    // the framework to ensure your app works the same.
    private static string CheckFor45DotVersion(int releaseKey)
    {//https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx
        if (releaseKey >= 394271)
            return "4.6.1 installed on all other Windows OS versions or later";
        if (releaseKey >= 394254)
            return "4.6.1 installed on Windows 10 or later";
        if (releaseKey >= 393297)
            return "4.6 installed on all other Windows OS versions or later";
        if (releaseKey >= 393295)
            return "4.6 installed with Windows 10 or later";
        if (releaseKey >= 379893)
            return "4.5.2 or later";
        if (releaseKey >= 378758)
            return "4.5.1 installed on Windows 8, Windows 7 SP1, or Windows Vista SP2 or later";
        if (releaseKey >= 378675)
            return "4.5.1 installed with Windows 8.1 or later";
        if (releaseKey >= 378389)
            return "4.5 or later";

        return "No 4.5 or later version detected";
    }
    public static string GetVersionFromRegistry()
    {//https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx
        string res = @"";
        // Opens the registry key for the .NET Framework entry.
        using (RegistryKey ndpKey =
            RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "").
            OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
        {
            // As an alternative, if you know the computers you will query are running .NET Framework 4.5 
            // or later, you can use:
            // using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, 
            // RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
            foreach (string versionKeyName in ndpKey.GetSubKeyNames())
            {
                if (versionKeyName.StartsWith("v"))
                {

                    RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
                    string name = (string)versionKey.GetValue("Version", "");
                    string sp = versionKey.GetValue("SP", "").ToString();
                    string install = versionKey.GetValue("Install", "").ToString();
                    if (install == "") //no install info, must be later.
                        res += (versionKeyName + "  " + name) + Environment.NewLine;
                    else
                    {
                        if (sp != "" && install == "1")
                        {
                            res += (versionKeyName + "  " + name + "  SP" + sp) + Environment.NewLine;
                        }

                    }
                    if (name != "")
                    {
                        continue;
                    }
                    foreach (string subKeyName in versionKey.GetSubKeyNames())
                    {
                        RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
                        name = (string)subKey.GetValue("Version", "");
                        if (name != "")
                            sp = subKey.GetValue("SP", "").ToString();
                        install = subKey.GetValue("Install", "").ToString();
                        if (install == "") //no install info, must be later.
                            res += (versionKeyName + "  " + name) + Environment.NewLine;
                        else
                        {
                            if (sp != "" && install == "1")
                            {
                                res += ("  " + subKeyName + "  " + name + "  SP" + sp) + Environment.NewLine;
                            }
                            else if (install == "1")
                            {
                                res += ("  " + subKeyName + "  " + name) + Environment.NewLine;
                            }
                        }
                    }
                }
            }
        }
        return res;
    }
    public static string GetUpdateHistory()
    {//https://msdn.microsoft.com/en-us/library/hh925567(v=vs.110).aspx
        string res=@"";
        using (RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\Updates"))
        {
            foreach (string baseKeyName in baseKey.GetSubKeyNames())
            {
                if (baseKeyName.Contains(".NET Framework") || baseKeyName.StartsWith("KB") || baseKeyName.Contains(".NETFramework"))
                {

                    using (RegistryKey updateKey = baseKey.OpenSubKey(baseKeyName))
                    {
                        string name = (string)updateKey.GetValue("PackageName", "");
                        res += baseKeyName + "  " + name + Environment.NewLine;
                        foreach (string kbKeyName in updateKey.GetSubKeyNames())
                        {
                            using (RegistryKey kbKey = updateKey.OpenSubKey(kbKeyName))
                            {
                                name = (string)kbKey.GetValue("PackageName", "");
                                res += ("  " + kbKeyName + "  " + name) + Environment.NewLine;

                                if (kbKey.SubKeyCount > 0)
                                {
                                    foreach (string sbKeyName in kbKey.GetSubKeyNames())
                                    {
                                        using (RegistryKey sbSubKey = kbKey.OpenSubKey(sbKeyName))
                                        {
                                            name = (string)sbSubKey.GetValue("PackageName", "");
                                            if (name == "")
                                                name = (string)sbSubKey.GetValue("Description", "");
                                            res += ("    " + sbKeyName + "  " + name) + Environment.NewLine;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return res;
    }
}

menggunakan kelas DA.VersionNetFramework

private void Form1_Shown(object sender, EventArgs e)
{
    //
    // Current OS Information
    //
    richTextBox1.Text = @"Current OS Information:";
    richTextBox1.AppendText(Environment.NewLine +
                            "Machine Name: " + Environment.MachineName);
    richTextBox1.AppendText(Environment.NewLine +
                            "Platform: " + Environment.OSVersion.Platform.ToString());
    richTextBox1.AppendText(Environment.NewLine +
                            Environment.OSVersion);
    //
    // .NET Framework Environment Information
    //
    richTextBox1.AppendText(Environment.NewLine + Environment.NewLine +
                                       ".NET Framework Environment Information:");
    richTextBox1.AppendText(Environment.NewLine +
                            "Environment.Version " + Environment.Version);
    richTextBox1.AppendText(Environment.NewLine + 
                            DA.VersionNetFramework.GetVersionDicription());
    //
    // .NET Framework Information From Registry
    //
    richTextBox1.AppendText(Environment.NewLine + Environment.NewLine +
                                       ".NET Framework Information From Registry:");
    richTextBox1.AppendText(Environment.NewLine +
                            DA.VersionNetFramework.GetVersionFromRegistry());
    //
    // .NET Framework 4.5 or later Information From Registry
    //
    richTextBox1.AppendText(Environment.NewLine + 
                                       ".NET Framework 4.5 or later Information From Registry:");
    richTextBox1.AppendText(Environment.NewLine +
                            DA.VersionNetFramework.Get45or451FromRegistry());
    //
    // Update History
    //
    richTextBox1.AppendText(Environment.NewLine + Environment.NewLine +
                            "Update History");
    richTextBox1.AppendText(Environment.NewLine + 
                            DA.VersionNetFramework.GetUpdateHistory());
    //
    // Setting Cursor to first character of textbox
    //
    if (!richTextBox1.Text.Equals(""))
    {
        richTextBox1.SelectionStart = 1;
    }
}

Hasil:

Informasi OS Saat Ini: Nama Mesin: D1 Platform: Win32NT Microsoft Windows NT 6.2.9200.0

Informasi Lingkungan .NET Framework: Environment.Version 4.0.30319.42000 .NET 4.6 di Windows 8.1 64 - bit atau yang lebih baru

.NET Framework Informasi Dari Registri: v2.0.50727 2.0.50727.4927 SP2 v3.0 3.0.30729.4926 SP2 v3.5 3.5.30729.4926 SP1


Klien v4 4.6.00079 Penuh 4.6.00079 v4.0
Klien 4.0.0.0

.NET Framework 4.5 atau yang lebih baru Informasi Dari Registri: Versi: 4.6 diinstal dengan Windows 10 atau yang lebih baru

Sejarah Pembaruan Microsoft .NET Framework 4 Client Profil
KB2468871
KB2468871v2
KB2478063
KB2533523
KB2544514
KB2600211
KB2600217
Microsoft .NET Framework 4 diperpanjang
KB2468871
KB2468871v2
KB2478063
KB2533523
KB2544514
KB2600211
KB2600217
Microsoft .NET Framework 4 Multi-Targeting Pack
KB2504637 pembaruan untuk (KB2504637)

PANAHAN
sumber
batas tubuh 30000 karakter semua kode adalah 34045 karakter bagian kode yang paling ringan ini
DARTS
Dan untuk NET Framework 4.7 dan NET Framework 4.7.1 ?
Kiquenet
2

Saya telah mengubah kelas Matt sehingga dapat digunakan kembali dalam proyek apa pun tanpa mencetak semua ceknya di Konsol dan mengembalikan string sederhana dengan versi maks yang benar terpasang.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace MyNamespace
{
    public class DotNetVersion
    {
        protected bool printVerification;

        public DotNetVersion(){
            this.printVerification=false;
        }
        public DotNetVersion(bool printVerification){
            this.printVerification=printVerification;
        }


        public string getDotNetVersion(){
            string maxDotNetVersion = getVersionFromRegistry();
            if(String.Compare(maxDotNetVersion, "4.5") >= 0){
                string v45Plus = get45PlusFromRegistry();
                if(!string.IsNullOrWhiteSpace(v45Plus)) maxDotNetVersion = v45Plus;
            }
            log("*** Maximum .NET version number found is: " + maxDotNetVersion + "***");

            return maxDotNetVersion;
        }

        protected string get45PlusFromRegistry(){
            String dotNetVersion = "";
            const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
            using(RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey)){
                if(ndpKey != null && ndpKey.GetValue("Release") != null){
                    dotNetVersion = checkFor45PlusVersion((int)ndpKey.GetValue("Release"));
                    log(".NET Framework Version: " + dotNetVersion);
                }else{
                    log(".NET Framework Version 4.5 or later is not detected.");
                }
            }
            return dotNetVersion;
        }

        // Checking the version using >= will enable forward compatibility.
        protected string checkFor45PlusVersion(int releaseKey){
            if(releaseKey >= 461308) return "4.7.1 or later";
            if(releaseKey >= 460798) return "4.7";
            if(releaseKey >= 394802) return "4.6.2";
            if(releaseKey >= 394254) return "4.6.1";
            if(releaseKey >= 393295) return "4.6";
            if((releaseKey >= 379893)) return "4.5.2";
            if((releaseKey >= 378675)) return "4.5.1";
            if((releaseKey >= 378389)) return "4.5";

            // This code should never execute. A non-null release key should mean
            // that 4.5 or later is installed.
            log("No 4.5 or later version detected");
            return "";
        }

        protected string getVersionFromRegistry(){
            String maxDotNetVersion = "";
            // Opens the registry key for the .NET Framework entry.
            using(RegistryKey ndpKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "")
                                            .OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\")){
                // As an alternative, if you know the computers you will query are running .NET Framework 4.5 
                // or later, you can use:
                // using(RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, 
                // RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
                string[] subKeyNnames = ndpKey.GetSubKeyNames();
                foreach(string versionKeyName in subKeyNnames){
                    if(versionKeyName.StartsWith("v")){
                        RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
                        string name =(string)versionKey.GetValue("Version", "");
                        string sp = versionKey.GetValue("SP", "").ToString();
                        string install = versionKey.GetValue("Install", "").ToString();
                        if(string.IsNullOrWhiteSpace(install)){ //no install info, must be later.
                            log(versionKeyName + "  " + name);
                            if(String.Compare(maxDotNetVersion, name) < 0) maxDotNetVersion = name;
                        }else{
                            if(!string.IsNullOrWhiteSpace(sp) && "1".Equals(install)){
                                log(versionKeyName + "  " + name + "  SP" + sp);
                                if(String.Compare(maxDotNetVersion, name) < 0) maxDotNetVersion = name;
                            }

                        }
                        if(!string.IsNullOrWhiteSpace(name)){
                            continue;
                        }

                        string[] subKeyNames = versionKey.GetSubKeyNames();
                        foreach(string subKeyName in subKeyNames){
                            RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
                            name =(string)subKey.GetValue("Version", "");
                            if(!string.IsNullOrWhiteSpace(name)){
                                sp = subKey.GetValue("SP", "").ToString();
                            }
                            install = subKey.GetValue("Install", "").ToString();
                            if(string.IsNullOrWhiteSpace(install)){
                                //no install info, must be later.
                                log(versionKeyName + "  " + name);
                                if(String.Compare(maxDotNetVersion, name) < 0) maxDotNetVersion = name;
                            }else{
                                if(!string.IsNullOrWhiteSpace(sp) && "1".Equals(install)){
                                    log("  " + subKeyName + "  " + name + "  SP" + sp);
                                    if(String.Compare(maxDotNetVersion, name) < 0) maxDotNetVersion = name;
                                }
                                else if("1".Equals(install)){
                                    log("  " + subKeyName + "  " + name);
                                    if(String.Compare(maxDotNetVersion, name) < 0) maxDotNetVersion = name;
                                } // if
                            } // if
                        } // for
                    } // if
                } // foreach
            } // using
            return maxDotNetVersion;
        }

        protected void log(string message){
            if(printVerification) Console.WriteLine(message);
        }

    } // class
}
Hikari
sumber
2

Saya mencoba menggabungkan semua jawaban menjadi satu kesatuan.

Menggunakan:

NetFrameworkUtilities.GetVersion()akan mengembalikan Versi .NET Framework yang saat ini tersedia saat ini atau null jika tidak ada.

Contoh ini berfungsi di .NET Framework versi 3.5+. Itu tidak membutuhkan hak administrator.

Saya ingin mencatat bahwa Anda dapat memeriksa versi menggunakan operator <dan> seperti ini:

var version = NetFrameworkUtilities.GetVersion();
if (version != null && version < new Version(4, 5))
{
    MessageBox.Show("Your .NET Framework version is less than 4.5");
}

Kode:

using System;
using System.Linq;
using Microsoft.Win32;

namespace Utilities
{
    public static class NetFrameworkUtilities
    {
        public static Version GetVersion() => GetVersionHigher4() ?? GetVersionLowerOr4();

        private static Version GetVersionLowerOr4()
        {
            using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP"))
            {
                var names = key?.GetSubKeyNames();

                //version names start with 'v', eg, 'v3.5' which needs to be trimmed off before conversion
                var text = names?.LastOrDefault()?.Remove(0, 1);
                if (string.IsNullOrEmpty(text))
                {
                    return null;
                }

                return text.Contains('.')
                    ? new Version(text)
                    : new Version(Convert.ToInt32(text), 0);
            }
        }

        private static Version GetVersionHigher4()
        {
            using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full"))
            {
                var value = key?.GetValue("Release");
                if (value == null)
                {
                    return null;
                }

                // Checking the version using >= will enable forward compatibility,  
                // however you should always compile your code on newer versions of 
                // the framework to ensure your app works the same. 
                var releaseKey = Convert.ToInt32(value);
                if (releaseKey >= 461308) return new Version(4, 7, 1);
                if (releaseKey >= 460798) return new Version(4, 7);
                if (releaseKey >= 394747) return new Version(4, 6, 2);
                if (releaseKey >= 394254) return new Version(4, 6, 1);
                if (releaseKey >= 381029) return new Version(4, 6);
                if (releaseKey >= 379893) return new Version(4, 5, 2);
                if (releaseKey >= 378675) return new Version(4, 5, 1);
                if (releaseKey >= 378389) return new Version(4, 5);

                // This line should never execute. A non-null release key should mean 
                // that 4.5 or later is installed. 
                return new Version(4, 5);
            }
        }
    }
}
Konstantin S.
sumber
1

Perbarui dengan .NET 4.6.2. Periksa nilai Rilis di registri yang sama seperti di tanggapan sebelumnya:

RegistryKey registry_key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full");
if (registry_key == null)
    return false;
var val = registry_key.GetValue("Release", 0);
UInt32 Release = Convert.ToUInt32(val);

if (Release >= 394806) // 4.6.2 installed on all other Windows (different than Windows 10)
                return true;
if (Release >= 394802) // 4.6.2 installed on Windows 10 or later
                return true;
Alt-WN
sumber
Dan updatesuntuk NET Framework 4.7 dan NET Framework 4.7.1 ?
Kiquenet
0

Agak besar, tetapi sepertinya ini terbaru dengan keanehan Microsoft:

    public static class Versions
    {
        static Version 
            _NET;

        static SortedList<String,Version>
            _NETInstalled;

#if NET40
#else
        public static bool VersionTry(String S, out Version V)
        {
            try
            { 
                V=new Version(S); 
                return true;
            }
            catch
            {
                V=null;
                return false;
            }
        }
#endif
        const string _NetFrameWorkKey = "SOFTWARE\\Microsoft\\NET Framework Setup\\NDP";
        static void FillNetInstalled()
        {
            if (_NETInstalled == null)
            {
                _NETInstalled = new SortedList<String, Version>(StringComparer.InvariantCultureIgnoreCase);
                RegistryKey
                    frmks = Registry.LocalMachine.OpenSubKey(_NetFrameWorkKey);
                string[]
                    names = frmks.GetSubKeyNames();
                foreach (string name in names)
                {
                    if (name.StartsWith("v", StringComparison.InvariantCultureIgnoreCase) && name.Length > 1)
                    {
                        string
                            f, vs;
                        Version
                            v;
                        vs = name.Substring(1);
                        if (vs.IndexOf('.') < 0)
                            vs += ".0";
#if NET40
                        if (Version.TryParse(vs, out v))
#else
                        if (VersionTry(vs, out v))
#endif
                        {
                            f = String.Format("{0}.{1}", v.Major, v.Minor);
#if NET40
                            if (Version.TryParse((string)frmks.OpenSubKey(name).GetValue("Version"), out v))
#else
                            if (VersionTry((string)frmks.OpenSubKey(name).GetValue("Version"), out v))
#endif
                            {
                                if (!_NETInstalled.ContainsKey(f) || v.CompareTo(_NETInstalled[f]) > 0)
                                    _NETInstalled[f] = v;
                            }
                            else
                            { // parse variants
                                Version
                                    best = null;
                                if (_NETInstalled.ContainsKey(f))
                                    best = _NETInstalled[f];
                                string[]
                                    varieties = frmks.OpenSubKey(name).GetSubKeyNames();
                                foreach (string variety in varieties)
#if NET40
                                    if (Version.TryParse((string)frmks.OpenSubKey(name + '\\' + variety).GetValue("Version"), out v))
#else
                                    if (VersionTry((string)frmks.OpenSubKey(name + '\\' + variety).GetValue("Version"), out v))
#endif
                                    {
                                        if (best == null || v.CompareTo(best) > 0)
                                        {
                                            _NETInstalled[f] = v;
                                            best = v;
                                        }
                                        vs = f + '.' + variety;
                                        if (!_NETInstalled.ContainsKey(vs) || v.CompareTo(_NETInstalled[vs]) > 0)
                                            _NETInstalled[vs] = v;
                                    }
                            }
                        }
                    }
                }
            }
        } // static void FillNetInstalled()

        public static Version NETInstalled
        {
            get
            {
                FillNetInstalled();
                return _NETInstalled[_NETInstalled.Keys[_NETInstalled.Count-1]];
            }
        } // NETInstalled

        public static Version NET
        {
            get
            {
                FillNetInstalled();
                string
                    clr = String.Format("{0}.{1}", Environment.Version.Major, Environment.Version.Minor);
                Version
                    found = _NETInstalled[_NETInstalled.Keys[_NETInstalled.Count-1]];
                if(_NETInstalled.ContainsKey(clr))
                    return _NETInstalled[clr];

                for (int i = _NETInstalled.Count - 1; i >= 0; i--)
                    if (_NETInstalled.Keys[i].CompareTo(clr) < 0)
                        return found;
                    else
                        found = _NETInstalled[_NETInstalled.Keys[i]];
                return found;
            }
        } // NET
    }
zDougie
sumber
0

Pada versi 4.5, Microsoft mengubah cara menyimpan indikator .NET Framework di registri. Ada panduan resmi tentang cara mengambil kerangka .NET dan versi CLR dapat ditemukan di sini: https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx

Saya menyertakan versi modifikasi dari kode mereka untuk menjawab pertanyaan karunia tentang bagaimana Anda menentukan kerangka .NET untuk 4.5 dan lebih tinggi di sini:

using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;

namespace stackoverflowtesting
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<int, String> mappings = new Dictionary<int, string>();

            mappings[378389] = "4.5";
            mappings[378675] = "4.5.1 on Windows 8.1";
            mappings[378758] = "4.5.1 on Windows 8, Windows 7 SP1, and Vista";
            mappings[379893] = "4.5.2";
            mappings[393295] = "4.6 on Windows 10";
            mappings[393297] = "4.6 on Windows not 10";

            using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\"))
            {
                int releaseKey = Convert.ToInt32(ndpKey.GetValue("Release"));
                if (true)
                {
                    Console.WriteLine("Version: " + mappings[releaseKey]);
                }
            }
            int a = Console.Read();
        }
    }
}
Titik koma dan lakban
sumber
Dan updatesuntuk NET Framework 4.7 dan NET Framework 4.7.1 ?
Kiquenet
0

Ini adalah solusi yang saya temukan:

private static string GetDotNetVersion()
{
  var v4 = (string)Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full", false)?.GetValue("Version");
  if(v4 != null)
    return v4;
  var v35 = (string)Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5", false)?.GetValue("Version");
  if(v35 != null)
    return v35;
  var v3 = (string)Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0", false)?.GetValue("Version");
  return v3 ?? "< 3";
}
Morten Nilsen
sumber
0
        public static class DotNetHelper
{
    public static Version InstalledVersion
    {
        get
        {
            string framework = null;

            try
            {
                using (var ndpKey =
            Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\"))
                {
                    if (ndpKey != null)
                    {
                        var releaseKey = ndpKey.GetValue("Release");
                        if (releaseKey != null)
                        {
                            framework = CheckFor45PlusVersion(Convert.ToInt32(releaseKey));
                        }
                        else
                        {
                            string[] versionNames = null;
                            using (var installedVersions =
                                Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP"))
                            {
                                if (installedVersions != null) versionNames = installedVersions.GetSubKeyNames();
                            }


                            try
                            {
                                if (versionNames != null && versionNames.Length > 0)
                                {
                                    framework = versionNames[versionNames.Length - 1].Remove(0, 1);
                                }
                            }
                            catch (FormatException)
                            {

                            }
                        }
                    }
                }
            }
            catch (SecurityException)
            {
            }

            return framework != null ? new Version(framework) : null;
        }
    }

    private static string CheckFor45PlusVersion(int releaseKey)
    {
        if (releaseKey >= 460798)
            return "4.7";
        if (releaseKey >= 394802)
            return "4.6.2";
        if (releaseKey >= 394254)
            return "4.6.1";
        if (releaseKey >= 393295)
            return "4.6";
        if (releaseKey >= 379893)
            return "4.5.2";
        if (releaseKey >= 378675)
            return "4.5.1";
        // This code should never execute. A non-null release key should mean
        // that 4.5 or later is installed.
        return releaseKey >= 378389 ? "4.5" : null;
    }
}
Martin.Martinsson
sumber
Dan updatesuntuk NET Framework 4.7 dan NET Framework 4.7.1 ?
Kiquenet
0

Jika mesin Anda terhubung ke internet, pergi ke tinydotnet , mengunduh dan menjalankan .NET Checker mungkin adalah cara termudah.

Jika Anda memerlukan metode aktual untuk menentukan versi, lihat its sumbernya di github, esp. yang Constants.cs yang akan membantu Anda untuk Net 4.5 dan kemudian, di mana bagian Revisi adalah salah satu relvant:

                           { int.MinValue, "4.5" },
                           { 378389, "4.5" },
                           { 378675, "4.5.1" },
                           { 378758, "4.5.1" },
                           { 379893, "4.5.2" },
                           { 381029, "4.6 Preview" },
                           { 393273, "4.6 RC1" },
                           { 393292, "4.6 RC2" },
                           { 393295, "4.6" },
                           { 393297, "4.6" },
                           { 394254, "4.6.1" },
                           { 394271, "4.6.1" },
                           { 394747, "4.6.2 Preview" },
                           { 394748, "4.6.2 Preview" },
                           { 394757, "4.6.2 Preview" },
                           { 394802, "4.6.2" },
                           { 394806, "4.6.2" },
mbx
sumber
Dan updatesuntuk NET Framework 4.7 dan NET Framework 4.7.1 ?
Kiquenet
@Kiquenet Sayangnya 4.7 dan 4.7.1 tidak disertakan dalam kode sumber tetapi ada masalah untuk kunci rilis yang dapat Anda gunakan - dan / atau berikan permintaan tarik sendiri ;-)
mbx
PS: sebelum Anda menginvestasikan waktu itu untuk permintaan tarik, ketahuilah bahwa setidaknya ada satu PR dalam pipa untuk sementara waktu - saya kira Scott terlalu sibuk akhir-akhir ini.
mbx
0

Saya merasa lebih mudah untuk bekerja dengan Versionkelas:

using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace System.Runtime.InteropServices {
  public static class RuntimeInformationEx {

    public static Version? GetDotnetFrameworkVersion() {
      using var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full");
      if (key is object) {
        if (!Int32.TryParse(key.GetValue("Release", "").ToString(), out var release)) return null;
        return release switch
        {
          378389 => new Version(4, 5, 0),
          378675 => new Version(4, 5, 1),
          378758 => new Version(4, 5, 1),
          379893 => new Version(4, 5, 2),
          393295 => new Version(4, 6, 0),
          393297 => new Version(4, 6, 0),
          394254 => new Version(4, 6, 1),
          394271 => new Version(4, 6, 1),
          394802 => new Version(4, 6, 2),
          394806 => new Version(4, 6, 2),
          460798 => new Version(4, 7, 0),
          460805 => new Version(4, 7, 0),
          461308 => new Version(4, 7, 1),
          461310 => new Version(4, 7, 1),
          461808 => new Version(4, 7, 2),
          461814 => new Version(4, 7, 2),
          528040 => new Version(4, 8, 0),
          528209 => new Version(4, 8, 0),
          528049 => new Version(4, 8, 0),
          _ => null
        };

      } else { // .NET version < 4.5
        using var key1 = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP");
        if (key1 is null) return null;
        var parts = key1.GetSubKeyNames()[^1].Substring(1).Split('.'); // get lastsub key, remove 'v' prefix, and split
        if (parts.Length == 2) parts = new string[] { parts[0], parts[1], "0" };
        if (parts.Length != 3) return null;
        Func<string, int> Parse = s => Int32.TryParse(s, out var i) ? i : -1;
        try { return new Version(Parse(parts[0]), Parse(parts[1]), Parse(parts[2])); } catch { return null; }
      }
    }

    public static Version? GetDotnetCoreVersion() {
      if (!RuntimeInformation.FrameworkDescription.StartsWith(".NET Core ")) return null;
      var parts = RuntimeInformation.FrameworkDescription.Substring(10).Split('.');
      if (parts.Length == 2) parts = new string[] { parts[0], parts[1], "0" };
      if (parts.Length != 3) return null;
      Func<string, int> Parse = s => Int32.TryParse(s, out var i) ? i : -1;
      try { return new Version(Parse(parts[0]), Parse(parts[1]), Parse(parts[2])); } catch { return null; }
    }
  }
}

(kode adalah C # 8)

kofifus.dll
sumber
1
Jawaban dari dua tahun lalu ini sudah melakukan pemetaan dari "Release"nilai ke Versions. Saya pikir titik kunci di mana informasi ini disimpan di registri dan dalam format apa yang terdokumentasi dengan baik di sini; apakah yang disampaikan kepada penelepon sebagai string, int, enum, atau Versionsemua hanya menempatkan pembungkus yang berbeda pada hal yang sama.
Lance U. Matthews
ya sejauh yang saya bisa melihat jawaban saya berfungsi sama dengan yang satu tetapi memiliki semua versi .NET dalam satu fungsi .. menggunakan Versi memungkinkan perbandingan yang mudah dll yaituvar v=RuntimeInformationEx .GetDotnetFrameworkVersion(); if (v<new Version(4,8,0) ...
kofifus
0

Anda bisa mendapatkan string yang berguna tanpa harus menyentuh registri atau rakitan referensi yang mungkin dimuat atau tidak. mscorlib.dll dan rakitan Sistem lainnya memiliki AssemblyFileVersionAttribute yang ditentukan, dan tampaknya unik untuk setiap versi .NET, berdasarkan rakitan referensi yang disediakan dengan Visual Studio.

string version = (typeof(string).Assembly
    .GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)
    .FirstOrDefault() as AssemblyFileVersionAttribute)?.Version;

Versi 4.5 sedikit tidak aktif, karena itu ditandai 4.0 dalam versi itu, tetapi 4.6 dan seterusnya tampaknya memiliki kecocokan versi minor. Melakukannya dengan cara ini sepertinya akan menjadi bukti yang lebih baik di masa mendatang daripada bergantung pada beberapa kunci registri penginstal dan harus membandingkan dengan kumpulan nilai tetap.

Saya menemukan rakitan referensi di sini:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework

Di mana saya mendapatkan versi berikut:

4.0 = 4.0.30319.1
4.5 = 4.0.30319.18020
4.5.1 = 4.0.30319.18402
4.5.2 = 4.0.30319.34211
4.6 = 4.6.81.0
4.6.1 = 4.6.1055.0
4.7.2 = 4.7.3062.0
4.8 = 4.8.3761.0
Bryce Wagner
sumber
-1

Coba yang ini:

string GetFrameWorkVersion()
    {
        return System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion();
    }
pengguna885959
sumber
2
Ini memberi 2.0meskipun berjalan dengan3.5
LCJ
RuntimeEnvironment.GetSystemVersion()tampaknya mengembalikan versi CLR, bukan versi .NET. MSDN Page
marchica