1

How do I output the version of C# an application is using from with C# code (whether it be an MVC app, website or console app).

I have spent a long time searching for this - I just can't find anything on Google - it's strange.

I searched for: "output C# version in code" "how to get C# version of current application" and many others.

Is this even possible?

I want it to output something like "C# version 7" or "5" etc.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
niico
  • 11,206
  • 23
  • 78
  • 161
  • 3
    Why would you want to do that, even if you could? The language that was used to generate the IL doesn't matter – Panagiotis Kanavos May 16 '17 at 09:18
  • @PanagiotisKanavos For example - I'm currently struggling to fix an issue using the new C# 7 tuples syntax because an app I think is using C# 5. – niico May 16 '17 at 09:19
  • 2
    @niico which has nothing to do with the *compiled* code. – Panagiotis Kanavos May 16 '17 at 09:19
  • @MarleenSchilt It looks like you can't do it. I'm using .net 4.7 in a "website" type project - yet I'm getting an error saying I'm using C# 5 in a website project - and the new tuples syntax isn't working. – niico May 16 '17 at 09:21
  • http://stackoverflow.com/questions/19532942/which-version-of-c-sharp-am-i-using – Bhuban Shrestha May 16 '17 at 09:21
  • 3
    @niico if you want to chekc *your project's* target language version, go to the Build tab of Project Properties. It's the last button in the dialog box. At runtime there is no C# language. Just the IL – Panagiotis Kanavos May 16 '17 at 09:21
  • OK So you can't do what I need - all you can do is look at the .net framework version and infer the C# version then? Yet in a website project using .net framework 4.7 - I'm getting a c# 5 error. (but this doesn't happen in an MVC app using the same code) - I think there may be a bug in website projects?! – niico May 16 '17 at 09:22
  • 2
    @niico web site, as in *not a web application but a web site*? That's completely different. It *is* a duplicate question, and you *can* change the target language of a web site – Panagiotis Kanavos May 16 '17 at 09:22
  • 1
    @niico no, you need to stop looking for ghostly bugs and ask the correct question. You have a `Web Site` project. These don't get compiled on your machine, they are compiled on the server. You have to specify the target language in your configuration file – Panagiotis Kanavos May 16 '17 at 09:23
  • PS You did add the System.ValueTuple package? – Panagiotis Kanavos May 16 '17 at 09:25
  • @PanagiotisKanavos I don't need to add System.ValueTuple because I'm using 4.7 - but I also added it and it didn't help – niico May 16 '17 at 09:27
  • @niico check the duplicate. If you want to use C# 7 in a web site project, you have to specify the target language in the config file – Panagiotis Kanavos May 16 '17 at 09:28
  • How can I specify C# 7 in a website project? – niico May 16 '17 at 09:28
  • I have done everything in the duplicate (note there is no advanced option in a website project) - none of it helps - tuples still don't work. – niico May 16 '17 at 09:29
  • @niico Did you? The duplicate DOESN'T say anything about `Advanced`, it talks about the *config file*. – Panagiotis Kanavos May 16 '17 at 09:30
  • "Advanced Build Options" are in Advanced - and not available in a website project - but this is just one point. Yes I did - I was already aware of this duplicate question. – niico May 16 '17 at 09:31
  • Anyway - my question is how can I confirm which version of C# is being used? I guess you have to infer that, right?! – niico May 16 '17 at 09:34
  • I'm getting an error like "tuples won't work in C# 5". I'd like to just confirm that I'm "using C# 5" when the code executes. – niico May 16 '17 at 09:35
  • OK after creating another new website, what finally worked was installing "Microsoft.CodeDom.Providers.DotNetCompilerPlatform" (it was not installed already). This didn't work on a legacy project for some reason - but did in a new one, so the answer should be there somewhere. Thanks. – niico May 16 '17 at 09:51

2 Answers2

1

You can get your project targeted .NET framework version. Which has been answered in this SO

which version of C# am I using

Just in case

static string version()
    {
        string[] v = typeof(string).Assembly.ImageRuntimeVersion.Replace("v","").Split('.');
            string ver = v[0]+"."+v[1];
            switch(ver)
            {
                case "1.0":
                    return "C# 1.0";
                break;
                case "1.1":
                    return "C# 1.2";
                break;
                case "2.0":
                    return "C# 2.0";
                break;
                case "3.5":
                    return "C# 3.0";
                break;
                case "4.0":
                    return "C# 4.0";
                break;
                case "4.5":
                    return "C# 5.0";
                break; 
                case "4.6":
                    return "C# 6.0";
                break; 
                case "4.6.2":
                    return "C# 7.0";
                break;
                default:
                 return "unknown";
    }
public static void Main(string[] args)
{
  string ver = version();
  Console.Write(ver);
}
Community
  • 1
  • 1
Bhuban Shrestha
  • 1,304
  • 11
  • 27
-2

Do yo mean assembly file?

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.FileVersion;
Hasan Alizada
  • 591
  • 4
  • 13