How can I compile .NET 3.5 C# code on a system without Visual Studio?
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.
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.
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.
Have you heard of MSBuild? =)
Visual Studio project files are (under the hood) MSBuild files, and it can also transform solution files into MSBuild files, effectively meaning that you can use MSBuild from the command line to build a Visual Studio solution.
For example, say I have a project located in C:\Repositories\Work\MyProject\
with an associated solution file of MyProject.sln
and I can do the following at a command line:
C:\Users\Me\> CD\
C:\> CD Windows\Microsoft.NET\Framework\v3.5
C:\Windows\Microsoft.NET\Framework\v3.5\> MSBuild /p:Configuration=debug
MSBuild will then build any projects in that solution that are set to build for the debug
configuration, and output something similar to the following:
Microsoft (R) Build Engine Version 3.5.30729.4926 [Microsoft .NET Framework, Version 2.0.50727.4952] Copyright (C) Microsoft Corporation 2007. All rights reserved.
Build started 17/02/2011 14:55:49. Project "c:\repositories\work\MyProject\MyProject.sln" on node 0 (default targets).
Building solution configuration "Debug|.NET". Project "c:\repositories\work\MyProject\MyProject.sln" (1) is building "c :\repositories\work\MyProject\MyProject\MyProject.csproj" (2) on node 0 (default targets).
Processing 0 EDMX files. Finished processing 0 EDMX files. CoreCompile: Skipping target "CoreCompile" because all output files are up-to-date with resp ect to the input files. CopyFilesToOutputDirectory: Copying file from "obj\Debug\MyProject.dll" to "bin\MyProject.dll". MyProject -> c:\repositories\work\MyProject\MyProject\bin\Ultra.Clie nt.dll Copying file from "obj\Debug\MyProject.pdb" to "bin\MyProject.pdb". Done Building Project "c:\repositories\work\MyProject\MyProject\Ultra.Cli ent.csproj" (default targets).Ultra_Client_Setup: The project "MyProject.Setup" is not selected for building in solution con figuration "Debug|.NET". Done Building Project "c:\repositories\work\MyProject\MyProject.sln" (def ault targets).
Build succeeded. 0 Warning(s) 0 Error(s)
Time Elapsed 00:00:00.92
Caveat: MSBuild isn't capable of processing setup projects
Chances are you have an environment variable that points to the wrong version of the framework
精彩评论