a blog for those who code

Friday 28 May 2021

Comparing files in Visual Studio and Visual Studio Code

In this short tutorial, we will see how we can compare two files in Visual Studio and Visual Studio Code. I usually work in Visual Studio for my C# backend code whereas Visual Studio Code is being used for Angular or any frontend project. There were a lot of instances where there was a need to compare two files and thus with both Visual Studio and Visual Studio Code we can achieve it.


Let's go through Visual Studio first. There are multiple ways we can compare two files in Visual Studio, let's go through them one by one.

1. Using Developer Command Prompt - From Visual Studio 2015, we have Visual Studio Developer Command Prompt which allows us to use command-line for visual studio. So thus using this developer command prompt we can open Visual Studio and compare two files. Let's say I have two files file1.cs and file2.cs and I would like to compare these two files.

I will open the Developer Command Prompt for VS 2017 from the start menu and go to the location where my file is and run the following command:

devenv /diff file1.cs file2.cs

This command will open the Visual Studio and will show the differences between the two files as shown below.


2. Using File Differ extension: Using this extension we can directly compare two files which are in Solution explorer.

3. Using Command Window: The command window will be opened from View -> Other Windows -> Command Window.


Then using the command Tools.DiffFiles Program.cs Program2.cs we can get the difference between two files.


Now let's go through Visual Studio Code. Just like in Visual Studio we have multiple ways to compare two files in Visual Studio Code too, let's go through them one by one.

1. If you have two files open in Visual Studio Code, you can right-click on one and click "Select for Compare".


And then right-click on the 2nd file and click compare with selected, which will open the difference between two files.


2. Just like Visual Studio, we can also use the command line to compare two files. For that, we will run the command, 

code --diff file.cs file2.cs

This will open a new Visual Studio Code instance and show the difference between the two files.

No comments:

Post a Comment