23 May 2007

How to detect version of MS Visual Studio or compiler in compilation time

To do this you should use _MSC_VER define like in this code:
#if _MSC_VER >= 1400 // this is Visual C++ 2005
#elif _MSC_VER >= 1310 // this is Visual C++ .NET 2003
#elif _MSC_VER > 1300 // this is Visual C++ .NET 2002


Anonymous said...

but this code does not work with C#
as with #if preprocessor directive we can use a bool variable.

Anonymous said...

for VS package,
add envDTE as reference and use the following code:


DTE dteObj = Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;

string strVSVersion = dteObj.Version;