我有三个EXE

带有3.5 net框架的EXE1版本

带有4.5 net框架的EXE2构建

带有4.6 NET框架的EXE3构建

我要在检测到已安装哪个.Net版本并根据该启动exe后运行exe

如果已安装3.5
   运行(EXE1)

如果安装了4.5
   运行(EXE2)

如果安装了4.6
   运行(EXE3)

我考虑过wix的安装程序,但iexpress却没得到任何东西,所以我们怎么做呢?

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


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

最佳答案

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

更新:
该示例为您提供了.net构想版本。

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# - C#Windows检测到.Net版本后如何运行exe,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43757441/

10-14 16:27
查看更多