Using Visual Studio Code for .NET Framework Projects in C#

As Visual Studio Code is the only completely free member of Visual Studio family now that Visual Studio Express is gone (and no, Community edition doesn’t count as a proper replacement), I wanted to have some of my projects debuggable from within it. While .NET Core works out of box, it takes a bit of effort to setup a full .NET Framework project.

First step is, of course, hunt on Stack Overflow. If you are lucky you might find a guide how to do it that actually works perfectly once you do a few modifications to your project. However, depending on your project, you might not need to go that far.

To start, destination computer will require something that can build your projects. Since installing Visual Studio makes the whole exercise a bit pointless, go and download Build Tools for Visual Studio 2019.

To your Visual Studio Code workspace add familiar .vscode directory and add tasks.json that will build your project. Based on a few of my .NET Framework projects, I found the following common denominator to do wonders (of course, change name of solution and exact path to your source and binaries):

tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"dependsOn": [ "clean" ],
"type": "process",
"windows": {
"command": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\MSBuild\\Current\\Bin\\amd64\\msbuild.exe",
"args": [
"-p:Configuration=Debug;DebugType=portable;PlatformTarget=x64",
"Solution.sln"
]
},
"options": { "cwd": "${workspaceFolder}/Source/" },
"group": "build"
},
{
"label": "clean",
"type": "shell",
"windows": {
"command": "DEL",
"args": [ ".\\Binaries\\*" ]
}
}
]
}

Here you can already see the trick – we’re adding parameters as argument to MSBuild instead of modifying the project file. I found that most projects (definitely all I tried) are only missing portable debug type and x64 as a platform target (I am still sucker for Any CPU). If it doesn’t work for you, go and check the original article to see if something else is missing.

And the last step is placing launch.json into .vscode directory (again, adjust names):

launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Framework Launch",
"type": "clr",
"request": "launch",
"preLaunchTask": "build",
"program": "Program.exe",
"args": [],
"cwd": "${workspaceFolder}/Binaries/",
"stopAtEntry": false
}
]
}

This should be sufficient to get you debugging in a pinch.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.