C# Windows 如何在检测到 .Net 版本后运行 exe

标签 c# .net installation wix

我有三个 EXE

EXE1 使用 3.5 net 框架构建

EXE2 使用 4.5 net 框架构建

EXE3 使用 4.6 net 框架构建

我想在检测已经安装了哪个.Net版本后运行exe,并根据那个启动exe

如果安装了 3.5 运行(EXE1)

如果安装了 4.5 运行(EXE2)

如果安装了 4.6 运行(EXE3)

我考虑过 wix 设置,iexpress,但没有得到任何东西,那么我们该怎么做呢?

或者它是可能的?如果是那么如何,如果不是那么我们可以借助第三方软件来做到这一点吗?

so I need a way to run exe as per platform because each platform has their own .Net framework

最佳答案

有两种方法:使用 bath 文件检测 .net 版本,然后为此版本运行 exe 或者构建一个 porogram exe 依赖 .net 2 之后这个 exe 决定女巫文件必须运行

更新: 此示例为您提供了已安装的 .net freamwork 版本

for.net 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);
                        }
                    }
                }
            }
        }
    }
}

获取 .net 4.5 及更高版本

    using System;
using Microsoft.Win32;

public class GetDotNetVersion
{
   public static void Get45PlusFromRegistry()
   {
      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) {
            Console.WriteLine(".NET Framework Version: " + CheckFor45PlusVersion((int) ndpKey.GetValue("Release")));
        }
         else {
            Console.WriteLine(".NET Framework Version 4.5 or later is not detected.");
         } 
    }
   }

   // Checking the version using >= will enable forward compatibility.
   private static string CheckFor45PlusVersion(int releaseKey)
   {
      if (releaseKey >= 460798) {
         return "4.7 or later";
      }
      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";
   }
}   
// Calling the GetDotNetVersion.Get45PlusFromRegistry method produces 
// output like the following:
//       .NET Framework Version: 4.6.1

通过这个例子你可以运行exe文件

  using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    class MyProcess
    {
        public static void Main()
        {
            Process myProcess = new Process();

            try
            {
                myProcess.StartInfo.UseShellExecute = false;
                // You can start any process, HelloWorld is a do-nothing example.
                myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
                myProcess.StartInfo.CreateNoWindow = true;
                myProcess.Start();
                // This code assumes the process you are starting will terminate itself.
                // Given that is is started without a window so you cannot terminate it
                // on the desktop, it must terminate itself or you can do it programmatically
                // from this application using the Kill method.
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

更新 2: 通过此更新,您可以在批处理文件中获得最新版本的 .net

@echo off
 setlocal
@SET INSTALLUTILDIR=
@for /F "tokens=1,2*" %%i in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" /v "Version"') DO (
    if "%%i"=="Version" (
        SET .NetVer=%%k
    )
)

@for /F "tokens=1,2*" %%i in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" /v "Version"') DO (
    if "%%i"=="Version" (
        SET .NetVer=%%k
    )
)

    ECHO The most current version of Net in use is %.NetVer%

通过这段代码你可以运行程序但是不要忘记bat文件必须在exe文件所在的文件夹中

start myProgram.exe 
exit

关于C# Windows 如何在检测到 .Net 版本后运行 exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43757441/

相关文章:

c# - 为什么 System.Threading.Timer 会自行停止?

c# - 使用类型转换器进行应用程序设置时出错

c# - 具有枚举角色的 ASP.NET Core 中的自定义授权

c# - 如何让 Visual Studio Intellisense 推荐方法的异步变体

c# - 如果元素来自 ItemSsource,如何在 WPF TreeView 中聚焦元素?

javascript - 产品: Aptana Studio -- Error 4155. 无法正确获取installer_nodejs_windows.msi

c# - 如何在异步多线程爬虫中锁定回调类?

c# - 在 C# 中分段下载?

security - 安装 msi 后启动 exe,但使用当前用户权限

wix - 静默安装 MSI 包