0

I'm trying to create a simple console application that would run only if .NET 4.7.1 is installed, for testing purposes.

I made a console application project and defined Target framework as .NET Framework 4.7.1.

After compiling the project, if I run it on a machine without .NET 4.7.1, it still runs.

Then, I tried to add some .NET 4.7.1 specific code that I found on the internet, and it still runs on a machine without .NET 4.7.1 installed on it.

class Class1
{
    // This method return type will have an attribute (in IL) of type `IsReadOnly`
    public ref readonly int Method2() { throw null; }

    void Serialize(System.ValueTuple<int, string> tuple1, (int, string) tuple2, Stream output)
    {
        IFormatter formatter = new BinaryFormatter();
        formatter.Serialize(output, tuple1);
        formatter.Serialize(output, tuple2);
    }

}

What else can I do to make the application will only run if .NET 4.7.1 is installed on the machine?

Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
  • 1
    So what *is* installed on the machine? – nvoigt Apr 11 '18 at 08:30
  • On the machine I was testing it - .NET 4.6.2 – Alex Weitz Apr 11 '18 at 08:31
  • 1
    Does it need to be a hard (probably only understandable if you are computer savvy) error, or would it be ok if you [checked](https://stackoverflow.com/questions/951856/is-there-an-easy-way-to-check-the-net-framework-version) it yourself and raised an error from your application if it's not what you expect? – nvoigt Apr 11 '18 at 08:33
  • 2
    I hate to point out the obvious, but if the exe still runs without .Net 4.7.1 then it doesn't require it. – Reinstate Monica Cellio Apr 11 '18 at 08:36
  • 1
    @Archer Then I misread the comment, sorry. – Fildor Apr 11 '18 at 08:41
  • The problem is: We have 1 machine where .NET 4.7.1 is supposedly installed, but the claim is that the installation was corrupted, and just the registry was changed. I would like to run a .NET 4.7.1 specific program to make sure it was not corrupted in any way. – Alex Weitz Apr 11 '18 at 08:42
  • @AlexWeitz So this is an XY Problem then. Doing what you suggest will not validate .Net 4.7.1 in totality. Just uninstall it and reinstall it. – Reinstate Monica Cellio Apr 11 '18 at 08:43
  • furthermore ` – Alex Weitz Apr 11 '18 at 08:45
  • @Archer but I still wouldn't know if the installation was corrupted or not. – Alex Weitz Apr 11 '18 at 08:48
  • @AlexWeitz The problem is not going to be solved by running an application that tests a method specific to 4.7.1 is available though - you do see that don't you? It's akin to ensuring your car is running fine by making sure the windscreen wipers work. Use the tools that were built for the purpose - installers. – Reinstate Monica Cellio Apr 11 '18 at 08:50
  • Or just search for something... https://www.google.co.uk/search?q=validate+.net+installation&oq=validate+.net+installation&aqs=chrome..69i57j0.4161j0j7&sourceid=chrome&ie=UTF-8 – Reinstate Monica Cellio Apr 11 '18 at 08:51
  • @Archer For the very specific problem that the OP is facing, it might actually work. It's not ensuring your car is running fine by making sure the windscreen wipers work, it's attempting to conclude that the car is *not* fine by testing that the windscreen wipers *don't* work. –  Apr 11 '18 at 08:54
  • @hvd You've misunderstood the analogy. What if the windscreen wipers *do* work, but you have no wheels? Validating the *whole* installation is the only way to be sure. A smaller check is almost worthless. – Reinstate Monica Cellio Apr 11 '18 at 08:57
  • 1
    @Archer I understand the analogy. If the windscreen wipers *do* work, *then* you need further testing. If they don't work, you can stop right there. And the OP is expecting they won't work. The OP is expecting that no .NET 4.7.1 file is actually installed on the file system. Testing *anything* that requires *any* bit in the .NET 4.7.1 files and having it fail would support that theory and would prove something is seriously wrong in the installation. –  Apr 11 '18 at 09:02
  • @hvd yes, so far I didn't find any reliable method to check whether or not it was actually installed. In the `%windir%\Microsoft.NET\Framework` you still have the same `v4.0.30319` Folder, and it doesn't contain any files that were recently changed. – Alex Weitz Apr 11 '18 at 09:05
  • 1
    What made it actually work is invoking the method: ` var b = ValueTuple.Create();`. – Alex Weitz Apr 11 '18 at 09:15

0 Answers0