简体   繁体   中英

Compiling C# code from the command line gives error

I am following this tutorial:

http://www.csharp-station.com/Tutorials/Lesson01.aspx

I pasted this into a text file, named it Welcome.cs:

// Namespace Declaration
using System;

// Program start class
class WelcomeCSS
{
    // Main begins program execution.
    static void Main()
    {
        // Write to console
        Console.WriteLine("Welcome to the C# Station Tutorial!"); 
    }
}

Then I went into the command prompt, and pointed to the file's directory. I typed csc.exe Welcome.cs and got this error message:

csc.exe is not recognized as internal or external command

I am using Visual Studio 2008

I tried moving csc.exe to the Windows directory, and now I am getting this error:

fatal error cs2018: unable to find messages file 'cscompui.dll'

How can I compile my C# code from the command line?

csc.exe isn't in your path. Try fully-qualifying it:

C:\WINDOWS\Microsoft.NET\Framework\v3.5\csc.exe

(replace 3.5 with whatever framework you're using, of course.)

Note that Visual Studio isn't being used here. Doesn't matter what version of that you're using, because you're not using it when you compile from the command line. The command line compiler is part of the framework itself.

You're probably running cmd (Windows command prompt) instead of the Visual Studio Command Prompt which is available in the Visual Studio Tools folder.

Look at folder:

C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Microsoft Visual Studio 2008\\Visual Studio Tools

This is most likely because the directory containing csc.exe is not in your path, given you're simply running the standard Windows command prompt.

Visual Studio 2008 (as all versions) comes with its own command prompt. This is essentially cmd.exe with some set up scripts that make all the Visual Studio tools accessible (puts the relevant directories in your path, for a start).

The console is easily accessible via the Start menu and should be called Visual Studio 2008 Command Prompt (under the Visual Studio Tools folder).

If you want to be able to use CSC.exe and other framework tools from anywhere on the command line go add these to your Path environment variable.

... Depending on framework version and if you have the Windows SDK installed these may be slightly different. (Also if you are on a 64bit system that would be different as well)

  • c:\\WINDOWS\\Microsoft.NET\\Framework\\v4.0.30319
  • C:\\WINDOWS\\Microsoft.NET\\Framework\\v3.5;
  • c:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727;
  • c:\\Program Files\\Microsoft SDKs\\Windows\\v6.0A\\bin;

To enable command line invocation of compiler program csc.exe of the Microsoft .NET Framework, add this to the end of your PATH environment variable.

Right-click on "My Computer " to select Properties.

Click the " Advanced " tab.
Click the " Environment Variable " button.
Scroll down the " System variables " list to the " Path " variable.
Double click on " Path "
Press the " End " key
Type (beginning with a semicolon separator)

;C:\Windows\Microsoft.NET\Framework\v4.0.30319

(be sure to match the right version of Framework from C:\\Windows\\Microsoft.NET\\Framework )

Click " OK "
Click " OK " to close the System Properties window.

Verify the change:

open CMD and Type a command such as "csc"
you'll get something like this

C:\\Users\\Jerry\\Documents\\coding>csc
Microsoft (R) Visual C# Compiler version 4.0.30319.33440 for Microsoft (R) .NET Framework 4.5 Copyright (C) Microsoft Corporation. All rights reserved.

A few options: 1) use the "Visual Studio 2008 Command Prompt" that is in your Visual Studio Tools folder. This loads up the command prompt with the pathing set correctly so it recognizes the tool name.

2) bypass this, and "Rebuild Solution" from the Build Menu. your bin directory should then have your exe in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM