<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Benedict Hazel</title>
    <description>The latest articles on DEV Community by Benedict Hazel (@bwhazel).</description>
    <link>https://dev.to/bwhazel</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1810134%2F9666921d-234a-47e4-8b3b-8c34db3b846a.jpeg</url>
      <title>DEV Community: Benedict Hazel</title>
      <link>https://dev.to/bwhazel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bwhazel"/>
    <language>en</language>
    <item>
      <title>dnenv.py: A Basic Tool to Manage Local .NET SDK Version</title>
      <dc:creator>Benedict Hazel</dc:creator>
      <pubDate>Sat, 27 Jul 2024 12:31:03 +0000</pubDate>
      <link>https://dev.to/bwhazel/dnenvpy-a-basic-tool-to-manage-local-net-sdk-version-12ma</link>
      <guid>https://dev.to/bwhazel/dnenvpy-a-basic-tool-to-manage-local-net-sdk-version-12ma</guid>
      <description>&lt;p&gt;One of the powers of modern .NET is the ability to have multiple SDK versions running side-by-side: I can happily have .NET 6 and .NET 8 projects on my local machine and the correct SDK gets used!  One of the ways to do this is via a &lt;a href="https://learn.microsoft.com/en-us/dotnet/core/versions/selection" rel="noopener noreferrer"&gt;&lt;em&gt;global.json&lt;/em&gt; file&lt;/a&gt; in the root directory of a project such as shown below.  In fact, the file can be placed in any directory and will set the SDK version for that directory and all its children.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"sdk"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"6.0.424"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the directory and its children will use version &lt;em&gt;6.0.424&lt;/em&gt; of the .NET SDK.  It is a quick way to set the version but can we get some additional developer convenience?&lt;/p&gt;

&lt;h2&gt;
  
  
  Introducing &lt;em&gt;dnenv.py&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;This is a basic Python script I wrote to manage the .NET SDK version for a directory by wrapping some .NET CLI functionality and &lt;em&gt;global.json&lt;/em&gt; file management as a simple CLI tool.  Simply &lt;code&gt;cd&lt;/code&gt; into the directory you want to set the .NET SDK for and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dnenv.py &lt;span class="nt"&gt;--list&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to list all the available versions of the .NET SDK on your computer.  To see what the current version in use is run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dnenv.py &lt;span class="nt"&gt;--get&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Assuming there is no &lt;em&gt;global.json&lt;/em&gt; file further up the file system hierarchy it will very probably be the latest version installed.&lt;/p&gt;

&lt;p&gt;To set the version for the current directory run the following, using an SDK version as shown when using the &lt;code&gt;--list&lt;/code&gt; option.  For example, if you want to use version 7.0.304 run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dnenv.py &lt;span class="nt"&gt;--set&lt;/span&gt; 7.0.304
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a &lt;em&gt;global.json&lt;/em&gt; file in the current directory set for .NET 7.0.304.  The command will not do anything if a &lt;em&gt;global.json&lt;/em&gt; file already exists.&lt;/p&gt;

&lt;p&gt;To clear the specified .NET SDK version and restore to the default, or another higher-level &lt;em&gt;global.json&lt;/em&gt;, simply run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dnenv.py &lt;span class="nt"&gt;--clear&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command basically deletes the &lt;em&gt;global.json&lt;/em&gt; file!&lt;/p&gt;

&lt;h2&gt;
  
  
  Get &lt;em&gt;dnenv.py&lt;/em&gt; and Where Next?
&lt;/h2&gt;

&lt;p&gt;I have uploaded the script onto my &lt;a href="https://github.com/BHazel/personal-scripts" rel="noopener noreferrer"&gt;personal-scripts&lt;/a&gt; repository on GitHub alongside some other scripts and tools I have made for my local computer setup.  Feel free to go ahead and download it, have a play and let me know your thoughts in the comments!&lt;/p&gt;

&lt;p&gt;As to further development, writing it in C# and integrating it as a tool to the .NET CLI would add even more developer convenience and remove the dependency on Python.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>python</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
