DEV Community

Benedict Hazel
Benedict Hazel

Posted on

dnenv.py: A Basic Tool to Manage Local .NET SDK Version

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 global.json file 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.

{
  "sdk": {
    "version": "6.0.424"
  }
}
Enter fullscreen mode Exit fullscreen mode

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

Introducing dnenv.py

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

dnenv.py --list
Enter fullscreen mode Exit fullscreen mode

to list all the available versions of the .NET SDK on your computer. To see what the current version in use is run:

dnenv.py --get
Enter fullscreen mode Exit fullscreen mode

Assuming there is no global.json file further up the file system hierarchy it will very probably be the latest version installed.

To set the version for the current directory run the following, using an SDK version as shown when using the --list option. For example, if you want to use version 7.0.304 run:

dnenv.py --set 7.0.304
Enter fullscreen mode Exit fullscreen mode

This creates a global.json file in the current directory set for .NET 7.0.304. The command will not do anything if a global.json file already exists.

To clear the specified .NET SDK version and restore to the default, or another higher-level global.json, simply run:

dnenv.py --clear
Enter fullscreen mode Exit fullscreen mode

This command basically deletes the global.json file!

Get dnenv.py and Where Next?

I have uploaded the script onto my personal-scripts 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!

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.

Top comments (0)