title: Generating .coverage file programmatic way with Visual Studio 2010
body: <p>I need to generate .coverage file programmatic way. <a href="http://blogs.msdn.com/b/ms_joc/archive/2005/04/08/406608.aspx" rel="nofollow">This post</a> explains a C# code to do it as follows. </p>
  
  <pre><code>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);
  </code></pre>
  
  <p>Unfortunately, when I compile this code, I get the following error.</p>
  
  <pre><code>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?)
  </code></pre>
  
  <p>As <a href="http://blogs.msdn.com/b/phuene/archive/2009/12/01/programmatic-coverage-analysis-in-visual-studio-2010.aspx" rel="nofollow">this post</a> says, there are some changes between VS2008 and VS2010, I think the Monitor class is in some different namespace.</p>
  
  <p>What might be wrong? How can I generate the .coverage file programmatically with Visual Studio 2010?</p>
  
  <h2>SOLVED</h2>
  
  <ol>
  <li><p>Copy the Microsoft.VisualStudio.Coverage.Monitor.dll from Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\DataCollectors\x86</p></li>
  <li><p>Add <code>using Microsoft.VisualStudio.CodeCoverage;</code> in the source code</p></li>
  <li>Run <code>csc bin2xml.cs /r:Microsoft.VisualStudio.Coverage.Analysis.dll /r:Microsoft.VisualStudio.Coverage.Monitor.dll</code>.</li>
  </ol>