How can I programatically get the version of a NuGet package being used in the solution?
<p>I'm working on a project, and we need to know the version of a NuGet package we are using, and use it in the code.</p>
<p>One way I tried was to read from the <code>packages.config</code> file, parse it for the package I want, and parse the line for the version. Something like:</p>
<pre><code>var lines = System.IO.File.ReadAllLines(@&quot;..\..\packages.config&quot;);
foreach(var line in lines)
{
    if (line.Contains(&quot;My.Package&quot;))
    {
         foreach(var part in line.split(' '))
         {
             if (part.Contains(&quot;version&quot;))
             {
                 return part.Split('&quot;')[1];
             }
         }
    }
}
</code></pre>
<p>Is there a better way to do this programmatically?</p>
<p>Note: The above code will not work, as <em>packages.config</em> is not deployed with the application.</p>