gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:28:19 24 4
gpt4 key购买 nike

我有三个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com