简体   繁体   中英

Language version in Developer Command Prompt for VS 2017

I created the following program

public class Program
{
    static void Main()
    {
        System.Console.WriteLine("Hello, World!");
    }
}

When compiling the code in the command prompt ( c:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\csc hello.cs ) which returns

Microsoft (R) Visual C# Compiler version 4.7.3056.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

The program still compiles so I thought no big deal. Based on other searches I've done I it seemed as if this message is only informational.

However, for the program

public class Program
{
    static void Main()
    {
        System.Console.WriteLine(DateTime.Now.DayOfWeek);
    }
}

the return is

Microsoft (R) Visual C# Compiler version 4.7.3056.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

hello.cs(5,28): error CS0103: The name 'DateTime' does not exist in the current context

which should work fine according to the pluralsight videos I'm following along with. After some searches many of the answers refer to changing the version of C# being used in the project but can't seem to translate that to working in the command prompt. What needs to change?

There are several versions of C# compiler (csc.exe) available on the system. So you have

  1. Microsoft Compilers as Part of The Framework, like c:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\csc.exe
  2. Microsoft Compilers (Roslyn) as Part of Visual Studio, like C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\MSBuild\\15.0\\Bin\\Roslyn\\csc.exe

When you compiles with

c:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\csc

You are using the C# compiler that shipped with .NET Framework, and this compiler only supports language features up to C# 5.0 .

For example, it cannot compile the following program which uses the new C# 6.0 language feature Await in catch/finally blocks .

class Program
{
    static void Main(string[] args)
    {
    }

    async Task<int> GetVAsync()
    {
        try
        {
        }
        catch
        {
            //gives error CS1985: Cannot await in the body of a catch clause
            await Task.Delay(1000); 
        }            
        return 3;
    }
}

So the warning message means ,

the compiler you choose to use (the one shipped with .NET Framework) is not capable of compiling new features introduced after C# 5.0. The compilation may succeed if don't use any new feature, just like your example. But you have better use the newer compilers - Roslyn that shipped with Visual Studio - to take advantage of the full power of the compiler.

The solution

Use this in your command line (the path may differs based on your VS edition)

C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\MSBuild\\15.0\\Bin\\Roslyn\\csc.exe hello.cs

Reference

Stackoverflow user Lex Li gave an excellent explanation on the versioning history of the C# compilers in this post .

Check the revised code/comments:

using System; //Add this to your code, it should work well

public class Program
{
    static void Main()
    {
        System.Console.WriteLine("Hello, World!");
        System.Console.WriteLine(DateTime.Now.DayOfWeek);
    }
}

Step 1 - CLR code: The first thing we need to do is to write the CLR code. This could be written in either C#.NET . In this example we are using C#.

[Microsoft.SqlServer.Server.SqlProcedure()]
public static void spSendMail(string recipients, string subject, string fromto, string body)
{

    MailMessage message = new MailMessage(fromto, recipients);
    SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
    string msg = string.Empty;
    try
    {
        message.From = new MailAddress(fromto);
        message.To.Add(recipients);
        message.Subject = subject;
        message.IsBodyHtml = true;
        message.Body = body;
        smtpClient.Port = 587;
        smtpClient.Credentials = new System.Net.NetworkCredential(fromto, "779957082");
        smtpClient.EnableSsl = true;
        smtpClient.Send(message);
        msg = "Message ENvoyé !";
        Console.WriteLine(msg);
        //Console.ReadKey();
    }
    catch (Exception ex)
    {
        msg = ex.Message;
        Console.WriteLine(msg);
        //Console.ReadKey();
    }

}

Step 2 - Compile CLR Code In order to use this code, the code has to be compiled.

The following command is run from a command line to compile the CLR code using the csc.exe application So from a command line run the command as follows: cd C:\\yourfolder C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\vbc /target:library SendEmail.cs After this you have this : The code should now be compiled in a file called: C:\\yourfolder\\SendEmail.dll

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