2

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.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
imslavko
  • 6,596
  • 2
  • 34
  • 43
  • The problem is not that you need to go to a older framework, the problem is the newer stuff only works on a newer framework. So you would need `%WINDIR%\Microsoft.NET\Framework\v4.0.30319\csc.exe` – Scott Chamberlain Nov 25 '14 at 22:19
  • @ScottChamberlain but .NET is not shipped to Windows Vista, for example, by default? – imslavko Nov 25 '14 at 22:21
  • It is in Add-Remove programs. But it may or may not be enabled, your program can do nothing about that without a setup.exe – Scott Chamberlain Nov 25 '14 at 22:22
  • You need to set the `TargetFramework` attribute to the desired version inside your .proj file. See [this](http://stackoverflow.com/questions/21869544/multi-targeting-in-command-line-compiler-csc-exe) – Yuval Itzchakov Nov 25 '14 at 22:23

2 Answers2

3

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.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Thank you very much for your answer. I will try this. Is .NET 2.0 the common denominator of most Windows versions? – imslavko Nov 25 '14 at 22:38
  • The .NET 2 compiler is just capable of compiling code up to 3.5. If you only use references that use to a max of 3.5 there is no problem. – Patrick Hofman Nov 25 '14 at 22:40
2

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).

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • where do I create the app.config file? And how do I make "csc.exe" executable aware of it? – imslavko Nov 25 '14 at 22:31
  • you put the app.config file wherever you need to, you tell csc about it by using the [`/appconfig`](http://msdn.microsoft.com/en-us/library/ee523958.aspx) flag: `csc.exe code.cs /debug /nologo /appconfig:app.config` – Scott Chamberlain Nov 25 '14 at 22:33
  • Thanks! As someone who doesn't work with .NET that much, it is not very easy to google all the information myself. – imslavko Nov 25 '14 at 22:37