0

I'd like to get the version of Visual Studio used to build a C# project without having to resort to C++/CLI. When I use the following in C# the compiler complains.

#if (_MSC_VER == 1500)
   // ... Do VC9/Visual Studio 2008 specific stuff
#elif (_MSC_VER == 1600)
   // ... Do VC10/Visual Studio 2010 specific stuff
#elif (_MSC_VER == 1700)
   // ... Do VC11/Visual Studio 2012 specific stuff
#endif

Is there a way to do this? Thanks

Gregor J
  • 31
  • 7
  • Since you care only about ancient stuff https://stackoverflow.com/questions/19532942/which-version-of-c-sharp-am-i-using is probably of some use for you... Not really sure what you trying to achieve (and more importantly why) so not suggesting as duplicate. – Alexei Levenkov Dec 03 '20 at 05:15
  • Note that usually C# code get compiled without VS at least for production releases... so make sure you actually want to know version of VS and not something else. – Alexei Levenkov Dec 03 '20 at 05:16
  • [`_MSC_VER`](https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-160) reflects the version of the C/C++ compiler, not of Visual Studio, and the two can be (very) different when using non-default C/C++ toolsets. Also, `_MSC_VER` is not available to C#, and I don't see how it could be of any use if it were. – dxiv Dec 03 '20 at 05:30
  • In C#, `#if` is equivalent to C++'s `#ifdef`. The only thing you can do with a `#if` in C# is test for the definition of a symbol (for example, `#if DEBUG`); you can't use an expression like `(_MSC_VER == 1500)`. You could do it yourself by defining a symbol like VSVER1500 on the compiler command line and then using `#if` to test it. What are you trying to achieve? – Flydog57 Dec 03 '20 at 05:41
  • It would help immensely to know what would be appearing in the commented sections and thus what they actually depend upon - C# language version, .NET Framework version, less believably, Visual Studio version. – Damien_The_Unbeliever Dec 03 '20 at 08:12

1 Answers1

1

One alternative would be to define symbols for conditional compilation within the project properties. For instance, you could add, DevEnv17 for VS2022 and DevEnv16 for VS2019.

Ensure you define the right symbols in project properties

<Choose>
    <When Condition=" '$(VisualStudioVersion)' == '17.0' "> 
        <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|Release|AnyCPU' ">
            <DefineConstants>DevEnv17</DefineConstants>
        </PropertyGroup>
    </When>
    <When Condition=" '$(VisualStudioVersion)' == '16.0' "> 
        <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|Release|AnyCPU' ">
            <DefineConstants>DevEnv16</DefineConstants>
        </PropertyGroup>
    </When>
</Choose>

Then you could use C# preprocessor directives as below

#if DevEnv17
    //Do VS2022 specific stuff
#elif DevEnv16
    //Do VS2019 specific stuff
#else
    //Do other VS IDE version stuff
#endif

Visual Studio 2022:

VS2022

Visual Studio 2019:

VS2019

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77