Comparing files using Visual Studio

 
 
  • Gérald Barré

Visual Studio's diff tool is excellent for comparing files. You can use it to compare two versions of the same file, or even two files that are not part of any solution.

#Using the command line

  1. Open the developer command prompt from the start menu, so devenv.exe is already in the path
  2. Use the following command line:
Shell
devenv /diff file1.cs file2.cs

#Using the Command Window in Visual Studio

If Visual Studio is already open, you can use the Command Window to diff files.

  1. Open the Command Window via Quick Launch or press Ctrl+W, A

  2. Run the Tools.DiffFiles command with the two files to compare:

    Shell
    Tools.DiffFiles "file1.cs" "file2.cs"

The result is the same as using the command line:

#Using the File Differ extension

The File Differ extension lets you compare files directly from the Solution Explorer:

#Using Visual Studio as a git difftool and git mergetool

git difftool is a Git command that lets you compare files between revisions using a diff tool of your choice, such as VS Code. You can configure Visual Studio as your Git diff and merge tool directly from within Visual Studio.

  1. Open Team Explorer (View / Team Explorer)

  2. Click on the "Settings" button

  3. Select "Global Settings"

  4. Click the "Use Visual Studio" buttons

Here is the .gitconfig file after clicking the buttons:

INI
[diff]
    tool = vsdiffmerge
[merge]
    tool = vsdiffmerge
[mergetool]
    prompt = true
[mergetool "vsdiffmerge"]
    cmd = \"C:\\Program Files (x86)\\Microsoft Visual Studio\\Preview\\Community\\Common7\\IDE\\CommonExtensions\\Microsoft\\TeamFoundation\\Team Explorer\\vsdiffmerge.exe\" \"$REMOTE\" \"$LOCAL\" \"$BASE\" \"$MERGED\" //m
    keepBackup = false
    trustExitCode = true
[difftool]
    prompt = true
[difftool "vsdiffmerge"]
    cmd = \"C:\\Program Files (x86)\\Microsoft Visual Studio\\Preview\\Community\\Common7\\IDE\\CommonExtensions\\Microsoft\\TeamFoundation\\Team Explorer\\vsdiffmerge.exe\" \"$LOCAL\" \"$REMOTE\" //t
    keepBackup = false

This post is part of the series 'Visual Studio Tips and Tricks'. Be sure to check out the rest of the blog posts of the series!

Do you have a question or a suggestion about this post? Contact me!

Follow me:
Enjoy this blog?