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.
答案 0 :(得分: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);
}
答案 1 :(得分:-2)
Do yo mean assembly file?
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.FileVersion;