Compile a separate C# file against older .NET framework

584 views Asked by At

I have a short C# script that uses various features of the languages and different .NET libraries:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Text;
using System.Threading;
using Microsoft.Win32.SafeHandles;

The full script can be found here.

I compile it with this command:

%WINDIR%\Microsoft.NET\Framework\v3.5\csc.exe code.cs /debug /nologo

But it turns out that .NET Framework v3.5 is not available on some distributions of Windows by default.

What would be the best way to compile this code so it runs on Windows Vista, 7, 8, 8.1 and Server 2008 without a need to download and install the .NET framework?

I tried looking for

%WINDIR%\Microsoft.NET\Framework\v3.0\csc.exe

binary, but it doesn't exist on my machine.

2

There are 2 answers

3
Scott Chamberlain On BEST ANSWER

In your app.config file you need to specify the supported runtimes you want your app to support. So if you wanted to support 3.0 or newer you would need to have.

<configuration>
   <startup>
      <supportedRuntime version="v2.0.50727"/>
      <supportedRuntime version="v4.0"/>
   </startup>
</configuration>

You also will need to make sure your project targets only .NET 3.0 in your project settings as that is the version that is bundled with Windows Vista.

If .NET is disabled in add-remove programs there is nothing you can do, you would need to have your user install some version either on their own or install it as part of your deployment (a setup installer or One-Click).

2
Patrick Hofman On

As far as I know, there is no compiler for .NET 3 since it is just.NET 2 with some extra assemblies. Hence, there is no need for another compiler than the one that was tjere: the .NET compiler.

So you should be able to compile your project using that compiler, which is located in WINDIR%\Microsoft.NET\Framework\v2.0.50727\csc.exe.

This will work as long as your code doesn't use .NET 4 specific features. Then you need to use that compiler, and install the .NET client on every system not having that version already.