【问题标题】:C# Windows how ro run exe after detect .Net versionC# Windows如何在检测.Net版本后运行exe
【发布时间】:2017-05-03 10:26:08
【问题描述】:

我有三个EXE

EXE1 使用 3.5 网络框架构建

EXE2 使用 4.5 网络框架构建

EXE3 使用 4.6 网络框架构建

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

如果安装了 3.5 运行(EXE1)

如果安装了 4.5 运行(EXE2)

如果安装了 4.6 运行(EXE3)

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

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

所以我需要一种按平台运行 exe 的方法,因为每个平台都有 他们自己的 .Net 框架

【问题讨论】:

  • 这有什么具体原因,为什么不直接用4.6呢? (如果需要 XP,则为 4.0)
  • 在这篇文章中,您可以看到如何检索当前安装的 .net 版本:stackoverflow.com/questions/951856/…
  • @AlexK.i 想在 Windows 7,8 和 10 上运行 exe .. 但我想在没有安装先决条件的情况下运行 exe(.Net 框架).. 我知道如何从 exe 中检测框架但是那个exe(检测框架)也需要框架......明白吗?所以我需要一种按照 platworm 运行 exe 的方法,因为每个平台都有自己的框架……有什么问题吗?
  • > 所以我需要一种按平台运行 exe 的方法,因为每个平台都有 > 他们自己的 .Net 框架

标签: c# .net installation wix


【解决方案1】:

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

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

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

【讨论】:

  • 你能举个例子吗?
  • 我通过代码更新了我的答案,有 3 个示例尝试使用它们;-)
  • 实际上我需要批处理文件的示例“在此版本运行 exe 之后使用浴文件检测 .net 版本”我回答你
  • 嘿抱歉,我是批处理文件的新手,你能告诉我检查框架后如何启动 exe 文件
  • 查看此链接makeuseof.com/tag/write-simple-batch-bat-file 您只需创建 newt ext 并将其更改为 *.txt ---> *.bat 如果运行它,请快速执行代码,如果您也想查看打印您需要通过cmd运行
【解决方案2】:

如果您使用 Windows 安装程序(例如通过 wix),您可以使用 MsiNetAssemblySupport property 来设置启动可执行文件的自定义操作。如果您专门使用 wix,则 WixNetfxExtension 可以是asked to set other properties,这可能更容易使用。这里是wix's list of .NET properties

但是我怀疑这是错误的方法。编写针对您支持的最低框架版本的单个 .NET 可执行文件应该更容易,并包含一个带有 supportedRuntime 元素的 .config 文件1,使其能够在更广泛的范围内运行的框架。 (见How to: Configure an App to Support .NET Framework 4 or 4.5

此外,根据可执行文件的功能,您最好编写一个 C++ 可执行文件、一个 C++ DLL(以便它可以与 Windows Installer 交互),或者甚至利用本机 Windows Installer 功能而不是自定义操作。 (如果适用,最后一个选项是理想的。)


1请注意,Windows Installer EXE 对临时文件的自定义操作支持只会将单个文件提取到临时文件夹,因此在这些配置中不会找到您的 .config 文件。将需要其他方法;各种 Windows Installer 工具都有自己的方法来缓解这个问题。

【讨论】:

  • 我在配置文件中尝试了这个 supportedRuntime 元素 我在 2.0 上构建 exe
  • 但是当我在 windows 8 x64 位上运行它时,我被要求在 windows 8 x64 上安装 .net 3.5 框架。 .Net 4.5 已经安装了为什么它不能在 4.5 上运行我添加了supportedRuntime?
猜你喜欢
  • 1970-01-01
  • 2011-02-18
  • 1970-01-01
  • 1970-01-01
  • 2016-03-17
  • 1970-01-01
  • 1970-01-01
  • 2017-04-12
  • 1970-01-01
相关资源
最近更新 更多