How to get .NET version programmatically?

G

Guest

Hi,

Is there a programmatic way to get the "general" or "overall" version of a
currently running instance of the Visual Studio .NET compiler? What I mean
by "general" or "overall" version is, I want to programmatically get the
version string that I see in the registry underneath the
HKLM\SOFTWARE\Microsoft\VisualStudio key, such as "7.0" or "7.1" (for .NET
2002 or .NET 2003, respectively). My registry contains both 7.0 and 7.1
keys, so I want to know which one I'm currently running. Note that I am not
interested in getting the CLR version (that is, the version that is returned
by the GetCORVersion() call).

Thanks!
 
C

Carlos J. Quintero [MVP]

Your question is a bit intriguing, because:

- At coding time, you know which VS.NET version you are using.

- At run-time, the executable can be running with .NET Framework 1.0 or 1.1,
depending on its configuration and the .NET Version on the target machine,
but it seems that you are not interested in the .NET Framework version
(returned by System.Environment.Version() )

The only relationship of the running executable with VS.NET is that maybe it
has attached the VS.NET debugger: your code can guess if it is beeing
debugged through System.Diagnostics.Debugger.IsAttached(), but I think that
you can not know which the debugger is.

So, although the goal of your question is beyond me, the only way that I can
think is guessing the running VS.NET through indirect ways, assuming that
you don´t have VS.NET 2002 and 2003 running side by side. For example:

- You can get the processes with the name "devenv.exe" and from there try to
get the path to the file and its version.

- You can get if there are COM objects running with the ProgIDs
"VisualStudio.DTE.7" (VS.NET 2002) or "VisualStudio.DTE.7.1" (VS.NET 2003).
In VB6 you would use the GetObject("MyProgID") function.

--

Carlos J. Quintero (Visual Developer - .NET MVP)

FAQs, Knowledge Base, Files, Docs, Articles, Utilities, etc. for .NET
addins:
http://groups.yahoo.com/group/vsnetaddin/ (free join)
 
G

Guest

Hi Carlos,

Thanks for the quick reply! Actually, it's a Custom Tool that runs during a
build. Its "Register for COM Interop" property is set to True, and it has
functions utilizing the [ComRegisterFunction] and [ComUnregisterFunction]
attributes that register and unregister the tool as a custom code generator
with the current version of .NET. Thus, I want the tool to be able to detect
which version of the compiler is being used when the build is occurring, so
that it knows with which .NET version to register before it runs. I
appreciate your solution about getting the file version of the current
running instance of devenv.exe, and that would probably work for us, but I'd
be surprised if there isn't another, more .NET-centric way of getting the
"7.0" or "7.1" strings. Any thoughts?

Whitney Kew
 
C

Carlos J. Quintero [MVP]

2 more thoughts:

- That kind of things (pre/post build actions) can be done with .NET
add-ins, which offer a direct way of getting the .NET version where they are
hosted (DTE.Version, DTE.Edition, etc.)

- If you are using a custom tool, external to VS.NET, I think that the only
way is to guess the running devenv.exe and get its version as I told you, or
pass the version to the custom tool somehow: for example, external tools can
receive the Solution and Project file names, and since the internal format
of those files is different in 7.0 and 7.1, parsing them you can guess the
target IDE. By the way, how do you call the custom tool when a build is
performed?


--

Carlos J. Quintero (Visual Developer - .NET MVP)

FAQs, Knowledge Base, Files, Docs, Articles, Utilities, etc. for .NET
addins:
http://groups.yahoo.com/group/vsnetaddin/ (free join)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top