Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compile .NET 3.5 C# code on a system without Visual Studio?

Tags:

c#

.net-3.5

I have some C# code that uses some constructs specific to .NET 3.5. When you install the .NET Framework distribution, you get the C# compiler installed with it (csc.exe). Even if I specify the csc.exe in C:\Windows\Microsoft.NET\Framework\v3.5, I cannot compile the code on a computer with only the .NET Framework installed, but not Visual Studio. I am able to compile code that uses v2.0 constructs without difficulty. How can I accomplish this?

Here is a sample that demonstrates my problem:

using System;
class Program
{
    public static void Main()
    {
        // The MacOSX value to the PlatformID enum was added after
        // .NET v2.0
        if (Environment.OSVersion.Platform == PlatformID.MacOSX)
        {
            Console.WriteLine("Found mac");
        }
        Console.WriteLine("Simple program");
    }
}

When compiling this code using csc.exe, I receive the following error:

test.cs(9, 58): error CS0117: 'System.PlatformID' does not contain a definition for 'MacOSX'

When executing csc.exe /? I receive the banner:

Microsoft (R) Visual C# 2008 Compiler version 3.5.21022.8
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

like image 760
JimEvans Avatar asked Feb 17 '11 14:02

JimEvans


2 Answers

Microsoft (R) Visual C# 2008 Compiler version 3.5.21022.8

That's old, original .NET 3.5 release. Service Pack 1 has a rather unfortunate name, there were a great many changes. I don't have the time machine to check if it added the MacOSX member. Timing is about right for coinciding with Silverlight.

Enable Windows Update or install SP1 directly.

like image 151
Hans Passant Avatar answered Oct 21 '22 20:10

Hans Passant


Regarding your error:

The PlatformId.Xbox and PlatformId.MaxOSX values were introduced in .NET Framework 2.0 SP2, 3.0 SP2 and 3.5 SP1.

That is probably why you cannont compile your example using the command line. When having SP1 installed your version number for 3.5 SP 1 should look like this:

Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.4926

There's a whole section on MSDN about Command-Line building.

If you are working on a computer that only has the .NET Framework SDK, you can use the C# compiler at the command line if you use the SDK Command Prompt, which is available from the Microsoft .NET Framework SDK menu option.

It also states what happens if you have multiple versions of .NET Framework installed:

The csc.exe executable is usually located in the Microsoft.NET\Framework\ folder under the system directory. Its location may vary depending on the exact configuration on any individual computer. Multiple versions of this executable will be present on the computer if more than one version of the .NET Framework is installed on the computer. For more information about such installations, see Determining Which Version of the .NET Framework Is Installed.

Example

csc File.cs

In my case I can do:

cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319>

csc somefile.cs

This should output something like this:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>csc

Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1

Copyright (C) Microsoft Corporation. All rights reserved.

like image 25
Filip Ekberg Avatar answered Oct 21 '22 22:10

Filip Ekberg