Compare Files (2), With Visual Studio Built In Tool

One usually has the need to compare two files or two folders, or two branches, especially as a frequent user of Visual Studio, one hopes to find a way to use Visual Studio to complete the comparison job in Visual Studio with somewhat the way like Source Control or Version Control software for comparison.

This article will cover this topic and related issues (we use the current version of 2019 16.11.0).

It seems this article is a little bit too long, I split it into two: one with native Visual Studio Tool, another is Visual Studio with Extension or Non-Visual Studio.

After splitting the article into two, they are still too long, I split it as three,

Introduction

Contents of this article series,

  • Compare Files (1),  with TFS and Git GUI in Visual Studio
    • TFS
    • Git
  • Compare Files (2),  with Visual Studio Built-in Tool
    • Command-line in Visual Studio
    • Visual Studio Developer Command Prompt 
    • Windows Prompt
  • Compare Files (3),  with Visual Studio Extension or Non-Visual Studio Apps
    • A: Tools outside of Visual Studio for Files Comparison
      • Online comparison, such like diff
      • Other Applications, such like VS Code or Word
    • B: Visual Studio Extention for Comparison Tools from Third Parties:
      • Code Compare in Visual Studio --- devart.com
      • Compare Files VS2019 --- marketplace.visualstudio.com

The Visual Studio only supports GUI Compare Files feature to TFS or Git, the source control tools.  We will discuss these features in the first article of this article series; and then as an alternative, we invoke the Visual Studio built-in Comparison Tools from command line that we will discuss in the second article in this article series, and finally, we discuss the alternative ways to compare files without Visual Studio in the third article.

Compare Files (2),  with Visual Studio Built-in Tool

 

1, Command-Line in Visual Studio

To display the Command window within Visual Studio, choose View => Other Windows => Command Window.

Using the command,

Tools.DiffFiles FileName1 FileName2

can do the diff job, like this,

It can also auto-complete paths to the files - making it easier to have the right, you type in the file name, the IntelliSense (auto-completion) will show all files with the name within Visual Studio solution, such like,

It can also compare files that are not within Visual Studio by full file path, while different from the files within Solution, you have to type the full path of the file name, you will get IntelliSense from your path like this,

2, Visual Studio Developer Command Prompt

Notice, that Visual Studio 2019 includes two command-line shells for developers,

  • Visual Studio Developer Command Prompt
    A standard command prompt with certain environment variables is set to make using command-line developer tools easier. Available since Visual Studio 2015.

  • Visual Studio Developer PowerShell 
    More powerful than a command prompt. For example, you can pass the output of one command (known as a cmdlet) to another cmdlet. This shell has the same environment variables set as Developer Command Prompt. Available since Visual Studio 2019.

To open Developer Command Prompt or Developer PowerShell from within Visual Studio,

Starting in Visual Studio 2019 version 16.5, Visual Studio includes an integrated terminal that can host either of these shells (Developer Command Prompt and Developer PowerShell). You can also open multiple tabs of each shell. The Visual Studio terminal is built on top of Windows Terminal. To open the terminal in Visual Studio, choose View > Terminal.

You can choose either Developer Command Prompt OR Developer PowerShell,

In each and every one of them, we can do the job, such as Commands below,

devenv

Open the Visual Studio.

devenv FileName1 FileName2

Open Visual Studio with two named files in it.

devenv /diff FileName1 FileName2

Open Visual Studio with two named files in it with a comparison mode.

3, Windows Prompt

You will not access Visual Studio command devenv from Windows Prompt command line, however, if you use the full path of the executable file devenv.exe, then you can do the same job as Visual Studio Developer Command Prompt or Visual Studio Developer PowerShell did,

The command,

"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\devenv",

Opened the Visual Studio.

The same is true, if we use \diff to compare two files. With Visual Studio Developer Command Prompt and Visual Studio Developer PowerShell, it seems not necessary to do so in Windows Prompt. However, if we wanted to make a shortcut to Compare Files, this would be very useful. We make a batch command file, VS_FileCompare.cmd,  

@echo off
setlocal
set vspath=C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE
start "Compare files" /B /MIN "%vspath%\devenv.exe" /diff %2 %1 First:'%2' Second:'%1'

and then put a shortcut to Desktop or SendTo folder, then Drag and Drop the two compared files into the compared command file in DeskTop,

or SendTo the Compared command file (how to get into the folder?) shortcut,

Then we can get the comparison Files in Visual Studio,

Finally, how can we get the vspath for the VS_FileCompare.cmd? This path is dependent on the different version of Visual Studio, we can get it from Visual Studio properties like this.

Right Click on the shortcut of Visual Studio => properties.

The target will give us the vspath.

Note, In this section, most work is from the article Compare two files in Visual Studio, (thank for them the great works) some useful details or some more features, we can get them from there. 

Summary

In this article, we discussed the features to compare files, folders, or branches with Visual Studio built-in tools through the command line. Although through command line, we can get the Visual Studio GUI invoked like we do the job from Visual Studio.

The disadvantage of these methods, they have to be invoked from Command-Line.

Reference


Similar Articles