2

I Have a Three EXE's

EXE1 build with 3.5 net framework

EXE2 build with 4.5 net framework

EXE3 build with 4.6 net framework

I want to run exe after detect which.Net version already installed and according to that start exe

if 3.5 installed RUN(EXE1)

if 4.5 installed RUN(EXE2)

if 4.6 installed RUN(EXE3)

i think about wix setup, iexpress but didnt get any thing so how can we do this ?

or its is possible? if yes then how and if no then so can we do this with the help of third party software?

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

  • 1
    Is there a specific reason for this, why not just use 4.6? (or 4.0 if you need XP) – Alex K. May 03 '17 at 10:33
  • In this post you can see how to retrive the current installed .net version: http://stackoverflow.com/questions/951856/is-there-an-easy-way-to-check-the-net-framework-version – Martin Godzina May 03 '17 at 10:43
  • @AlexK.i want to run exe on windows 7,8 and 10 .. but i want to run exe without install prerequisite(.Net framework).. I know how to detect framework from exe but that exe(that detect framework) also need the framework... understand? so i need a way to run exe as per platworm because each platform have there own framwork ... any question ? – user7913261 May 03 '17 at 10:56
  • > so I need a way to run exe as per platform because each platform has > their own .Net framework – user7913261 May 03 '17 at 11:00

2 Answers2

1

there is 2 way: using bath file to detect .net version after that run exe for this version or build a porogram exe depend .net 2 after that this exe decide witch file must run

Update: this samples give you version of .net freamwork are inistaled

for.net 4 and older

 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);
                        }
                    }
                }
            }
        }
    }
}

and for get .net 4.5 and upper

    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

and by this sample you can run exe file

  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);
            }
        }
    }
}

Update2: by this Update you get latest version of .net at batch File

@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%

and by this code you can run program but dont forget the bat file must be in folder the exe files are there

start myProgram.exe 
exit
Shahrooz Ansari
  • 2,637
  • 1
  • 13
  • 21
0

If you use Windows Installer (such as through wix), you can use the MsiNetAssemblySupport property to condition a custom action that launches your executable. If you use wix specifically, the WixNetfxExtension can be asked to set other properties that may be easier to use. Here is wix's list of .NET properties.

However I suspect this is the wrong approach. It should be much easier to write a single .NET executable that targets the lowest framework version you support, and include a .config file1 with a supportedRuntime element that enables it to run on a wider spread of frameworks. (See How to: Configure an App to Support .NET Framework 4 or 4.5.

Also, depending on what the executable does, you may be better off writing a C++ executable, a C++ DLL (so it can interact with Windows Installer), or even leveraging native Windows Installer functionality instead of custom actions. (If applicable, the last option is ideal.)


1Note that Windows Installer EXE custom action support for temporary files will only extract a single file to the temp folder, so your .config file would not be found in these configurations. Alternate approaches would be required; various Windows Installer tools have their own approaches for mitigating this problem.

Michael Urman
  • 15,737
  • 2
  • 28
  • 44
  • I tried this supportedRuntime element inside the Config file i build exe on 2.0 – user7913261 May 03 '17 at 13:06
  • but when i run this on windows 8 x64 bit i got prometed to install .net 3.5 framwork ... on windows 8 x64 . .Net 4.5 already installed why its not run on 4.5 i added supportedRuntime ? – user7913261 May 03 '17 at 13:06