Generating .coverage file programmatic way with Visual Studio 2010

Posted by prosseek on Stack Overflow See other posts from Stack Overflow or by prosseek
Published on 2011-02-11T02:51:08Z Indexed on 2011/02/11 7:25 UTC
Read the original article Hit count: 338

I need to generate .coverage file programmatic way. This post explains a C# code to do it as follows.

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Microsoft.VisualStudio.Coverage;
using Microsoft.VisualStudio.Coverage.Analysis;

// You must add a reference to Microsoft.VisualStudio.Coverage.Monitor.dll

namespace Microsoft.VisualStudio
{
       class DumpProgram
       {
              static void Main(string[] args)
              {
                    Process p = new Process();
                    StringBuilder sb = new StringBuilder("/COVERAGE ");
                    sb.Append("hello.exe");
                    p.StartInfo.FileName = "vsinstr.exe";
                    p.StartInfo.Arguments = sb.ToString();
                    p.Start();
                    p.WaitForExit();
                     // TODO: Look at return code – 0 for success
                     // A guid is used to keep track of the run
                    Guid myrunguid = Guid.NewGuid();
                    Monitor m = new Monitor();
                    m.StartRunCoverage(myrunguid, "hello.coverage");
                    // Complete the run
                    m.FinishRunCoverage(myrunguid);

Unfortunately, when I compile this code, I get the following error.

bin2xml.cs(26,22): error CS0246: The type or namespace name 'Monitor' could not be found (are you
        missing a using directive or an assembly reference?)
bin2xml.cs(26,38): error CS0246: The type or namespace name 'Monitor' could not be found (are you
        missing a using directive or an assembly reference?)

As this post says, there are some changes between VS2008 and VS2010, I think the Monitor class is in some different namespace.

What might be wrong? How can I generate the .coverage file programmatically with Visual Studio 2010?

ADDED

I added using System.Threading to run to get the following error. I run the command csc bin2xml.cs /r:Microsoft.VisualStudio.Coverage.Analysis.dll.

bin2xml.cs(28,21): error CS0723: Cannot declare a variable of static type 'System.Threading.Monitor'
bin2xml.cs(28,33): error CS0712: Cannot create an instance of the static class
        'System.Threading.Monitor'
bin2xml.cs(29,23): error CS1061: 'System.Threading.Monitor' does not contain a definition for
        'StartRunCoverage' and no extension method 'StartRunCoverage' accepting a first argument of
        type 'System.Threading.Monitor' could be found (are you missing a using directive or an
        assembly reference?)
bin2xml.cs(31,23): error CS1061: 'System.Threading.Monitor' does not contain a definition for
        'FinishRunCoverage' and no extension method 'FinishRunCoverage' accepting a first argument
        of type 'System.Threading.Monitor' could be found (are you missing a using directive or an
        assembly reference?)

ADDED2

I compiled the code with the following command.

csc bin2xml.cs /r:Microsoft.VisualStudio.Coverage.Analysis.dll /r:Microsoft.VisualStudio.Coverage.Monitor.dll

Then, I got these error messages. Monitor m = new Monitor(); is at the line 27.

bin2xml.cs(27,21): error CS0246: The type or namespace name 'Monitor' could not be found (are you
        missing a using directive or an assembly reference?)
bin2xml.cs(27,37): error CS0246: The type or namespace name 'Monitor' could not be found (are you
        missing a using directive or an assembly reference?)

© Stack Overflow or respective owner

Related posts about visual-studio-2010

Related posts about code-coverage